框架原理之日志类

Posted xiaobingch

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了框架原理之日志类相关的知识,希望对你有一定的参考价值。

目录结构:

技术图片

日志类 coreliblog.php

<?php

namespace corelib;


class log
{

    static $class;
    /*
     * 1.确定日志存储方式
     * 2.写日志
     */
    static public function init()
    {
        //确定存储方式
        $drive = conf::get(‘DRIVE‘,‘log‘);
        $class = ‘corelibdrivelog‘.$drive;
        self::$class = new $class;
    }
    static public function log($message,$file = ‘log‘)
    {
        self::$class->log($message,$file);
    }
}

文件日志类 corelibdrivelogfile.php

<?php

namespace corelibdrivelog;
use corelibconf;

class file
{
    protected $path;
    public function __construct()
    {
        $conf = conf::get(‘OPTION‘,‘log‘);
        $this->path = $conf[‘PATH‘];
    }
    public function log($message,$file = ‘log‘)
    {
        /*
         * 1.确定文件存储位置是否存在
         * 新建目录
         * 2.写入日志
         */
        if(!is_dir($this->path))
        {
           mkdir($this->path,‘0777‘,true);
        }
        file_put_contents($this->path.$file.‘.php‘,date(‘Y-m-d H:i:s‘).$message.PHP_EOL,FILE_APPEND);
    }
}

日志配置文件 coreconfiglog.php

<?php
return array(
    ‘DRIVE‘=>‘file‘,
    ‘OPTION‘=>array(
        ‘PATH‘=>MYFRAME.‘/log/‘
    )
);

写入日志

coreliblog::init();
coreliblog::log(‘test‘);

以上是关于框架原理之日志类的主要内容,如果未能解决你的问题,请参考以下文章