【问题标题】:Override format log覆盖格式日志
【发布时间】:2016-12-30 19:19:46
【问题描述】:

我需要覆盖使用 Laravel 5.3 制作的项目的格式日志

我使用 post 作为指导,但不起作用。

首先创建一个bootstrap/ConfigureLogging.php 类

<?php
namespace Bootstrap;

use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger as Monolog;
use Monolog\Formatter\LineFormatter;
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\StreamHandler;
use Carbon\Carbon;

class ConfigureLogging extends BaseConfigureLogging
{
     /**
     * Configure the Monolog handlers for the application.
     * @param  \Illuminate\Contracts\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureDailyHandler(Application $app, Writer $log)
    {
        $config = $app->make('config');
        $maxFiles = $config->get('app.log_max_files');

        // Formatting
        // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
        $logFormat = "[%datetime%] [%level_name%]: %message% %context% %extra%\n";
        $formatter = new LineFormatter($logFormat);
        //$logStreamHandler->setFormatter($formatter); // Here I'm lost


        $log->useDailyFiles(
            $app->storagePath().'/logs/cprsync.log', is_null($maxFiles) ? 5 : $maxFiles, $config->get('app.log_level', 'debug')
        );
}

我的应用程序的 laravel 日志的此更改(覆盖)名称。 但是当我尝试为覆盖格式添加代码时,我迷路了,原始帖子上的代码不起作用。

我看到 Monolog 的代码,我看到我需要发送我的格式,但我不知道。

【问题讨论】:

    标签: laravel-5.3 monolog


    【解决方案1】:

    据我所知,您所指的链接应该有效。你不见了pushHandler

    protected function configureDailyHandler(Application $app, Writer $log) {
        $config   = $app->make('config');
        $maxFiles = $config->get('app.log_max_files');
        $path     = $app->storagePath().'/logs/cprsync.log';
    
        // Daily handler
        $handler = new RotatingFileHandler($path, is_null($maxFiles) ? 5 : $maxFiles,
            $config->get('app.log_level', 'debug'));
    
        // Formatting
        $logFormat = "%datetime% [%level_name%] (%channel%): %message% %context% %extra%\n";
        $formatter = new LineFormatter($logFormat);
        $handler->setFormatter($formatter);
    
        // Push handler
        $log->getMonolog()->pushHandler($handler);
    }
    

    【讨论】:

    • 非常感谢。通过您的示例,您可以很好地了解推送处理程序的机制。并且工作正常。
    • 好吧,当日志上没有特殊信息时,我使用$formatter = new LineFormatter($logFormat,null,true,true);删除行尾的额外括号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多