码迷,mamicode.com
首页 > 其他好文 > 详细

框架原理之日志类(七)

时间:2020-03-11 19:34:55      阅读:37      评论:0      收藏:0      [点我收藏+]

标签:sage   idt   none   ted   存储   lib   app   init   highlight   

目录结构:

技术图片

日志类 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‘);

框架原理之日志类(七)

标签:sage   idt   none   ted   存储   lib   app   init   highlight   

原文地址:https://www.cnblogs.com/xiaobingch/p/12464653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!