1. 程式人生 > >lumen 自定義錯誤日誌文件

lumen 自定義錯誤日誌文件

參數 glib ace 寫入文件 handle cnblogs 可用 perm ror

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

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

lumen 自定義錯誤日誌文件