laravel日志根据不同类型,进行不同文件存储

1,原因:因为涉及用到其他第三方的包,例如微信支付等,如果把日志还放在一起,很难查找

2,实现类

pushHandler(new RotatingFileHandler($file));
        // 将类别记录到日志中
        $monolog->pushProcessor(function ($record)use ($category){
            $record['level_name'] = "{$record['level_name']}.[{$category}]";
            return $record;
        });
        $monolog->$type($msg, $context);
    }

    /**
     * 获取日志存储路径
     * @param $category
     * @param bool $noDate
     * @return string
     */
    public static function getCommonLogDir($category, $noDate = false)
    {
        $log = storage_path('logs' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $category . '.log');
        if (!$noDate) {
            $date = date('Ymd');
            $log = storage_path('logs' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $category . $date . '.log');
        }
        return $log;
    }

    protected static function channel()
    {
        /** @var Application $app */
        $app = app();
        if ($app->bound('config') &&
            $channel = $app->make('config')->get('app.log_channel')) {
            return $channel;
        }

        return $app->bound('env') ? $app->environment() : 'production';
    }
}

3,类的使用例子:

 SimpleLogger::needSolve('需要解决的日志'.date('Y-m-d H:i:s',time()),['name'=>'张三','sex'=>'男']);

4,展示结果:laravel日志根据不同类型,进行不同文件存储_第1张图片

你可能感兴趣的:(php)