【问题标题】:How to change the default log in Laravel 5?如何更改 Laravel 5 中的默认日志?
【发布时间】:2015-05-14 15:56:48
【问题描述】:

Laravel 5 只有一个地方可以配置日志。您可以指定是否需要每日日志文件或单个日志文件。

我想使用我自己的独白实例,但我似乎无法弄清楚将它分配给 Laravel Log writer 的正确位置。

我希望能够继续使用 Log 门面。

在我的搜索中,我偶然发现了http://laravel.io/forum/02-11-2014-howto-log-with-mongodb,所以我对如何实现它有一个想法,但是论坛帖子是为 Laravel 4 编写的。在 Laravel 5 中,不再有 app/start/global.php,我正在寻找此代码的正确位置。

这是你作为中间件应该做的事情吗?

【问题讨论】:

    标签: php logging laravel-5


    【解决方案1】:

    看看Illuminate\Foundation\Bootstrap\ConfigureLogging。这是设置日志记录的地方。

    特别是registerLogger() 方法。注意它是如何设置$app['log']

    $app->instance('log', $log = new Writer(
        new Monolog($app->environment()), $app['events'])
    );
    

    所以我要做的是编辑您的AppServiceProvider(或创建一个新的LogServiceProvider,您的偏好)并覆盖它。

    $app->instance('log', new \Illuminate\Log\Writer(
        new Monolog\Logger(... however you want to setup Monolog ...)
    );
    

    Log 门面只是访问$app['log']。所以一旦你覆盖了它,你就准备好了。

    【讨论】:

    • 非常感谢。我已经使用自己的配置将日志记录移动到它自己的包中,因此 LogServiceProvider 现在似乎是显而易见的方法。感谢您指出如何做到这一点。 :)
    【解决方案2】:

    我通过覆盖 Illuminate\Foundation\Bootstrap\ConfigureLogging 类解决了这个问题。小写如何:Configuring Custom Logs in Laravel 5

    【讨论】:

    • PHP 致命错误:在 /home/vagrant/api/vendor/laravel/framework/src/Illuminate/Container/Container.php 中未捕获异常 'ReflectionException' 并带有消息“类日志不存在”: 741 堆栈跟踪:#0 /home/vagrant/api/vendor/laravel/framework/src/Illuminate/Container/Container.php(741):ReflectionClass->__construct('log') #1 /home/vagrant/api/ vendor/laravel/framework/src/Illuminate/Container/Container.php(631): Illuminate\Container\Container->build('log', Array)
    猜你喜欢
    • 2019-03-13
    • 2016-02-22
    • 1970-01-01
    • 2011-04-25
    • 2023-03-21
    • 2015-10-25
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多