框架原理之日志类(七)

目录结构:

qq%e5%9b%be%e7%89%8720170525001049

日志类 core\lib\log.php

<?php

namespace core\lib;


class log
{

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

文件日志类 \core\lib\drive\log\file.php

<?php

namespace core\lib\drive\log;
use core\lib\conf;

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);
    }
}

日志配置文件 core\config\log.php

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

写入日志

\core\lib\log::init();
\core\lib\log::log('test');

猜你喜欢

转载自www.cnblogs.com/xiaobingch/p/12464653.html