lumen 自定義錯誤日志文件


自定義錯誤日志文件,改造新的方法

<?php
namespace App;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;

class LogLib
{
    //define static log instance.
    protected static $_log_instance;
    /**
     * 獲取log實例
     *
     * @return obj
     * @author Sphenginx
     **/
    public static function getLogInstance()
    {
        if (static::$_log_instance === null) {
            static::$_log_instance = new Logger('NOTICE');
        }
        return static::$_log_instance;
    }
    /**
     * Handle dynamic, static calls to the object.
     *
     * @param  string  $method 可用方法: debug|info|notice|warning|error|critical|alert|emergency 可調用的方法詳見 Monolog\Logger 類
     * @param  array   $args 調用參數
     * @return mixed
     * @author Sphenginx
     */
    public static function __callStatic($method, $args)
    {
        $instance = static::getLogInstance();
        //組織參數信息
        $message = $args[0];
        //記錄上下文日志
        $context = isset($args[1]) ? $args[1] : [];
        //定義記錄日志文件
        $path    = isset($args[2]) ? $args[2] : '/notice/';
        //設置日志處理手柄,默認為寫入文件(還有mail、console、db、redis等方式,詳見Monolog\handler 目錄)
        $handler = new StreamHandler(storage_path($path) . date('Y-m-d').'.log', Logger::toMonologLevel($method), $bubble = true, $filePermission = 0777);
        //設置輸出格式LineFormatter(Monolog\Formatter\LineFormatter), ignore context and extra
        $handler->setFormatter(new LineFormatter(null, null, true, true));
        $instance->setHandlers([$handler]);
        $instance->$method($message, $context);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM