如何在Laravel中创建和使用自定义的每日日志文件?

时间:2019-11-07 10:33:33

标签: php laravel logging

在Laravel中,我在/config/logger.php中定义了一个自定义日志文件:

'mycustomlog' => [
  'driver' => 'stack',
  'path' => storage_path('logs/mycustomlog.log'),
  'level' => 'info',
],

这是我用于上下文的堆栈驱动程序:

'stack' => [
  'driver' => 'stack',
  'channels' => ['daily', 'syslog'],
  'ignore_exceptions' => false,
],

我的称呼如下:

Log::channel('mycustomlog')->info($e);


我希望发生的事情:

创建(每日)日志文件,并记录异常。即mycustomlog-2019-11-07.log

实际发生的情况:

未创建任何日志文件,但以下错误记录到laravel.log

[2019-11-07 10:25:31] laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (ErrorException(code: 0): Undefined index: channels at /var/www/vendor/laravel/framework/src/Illuminate/Log/LogManager.php:232)

解决方案:

'mycustomlog' => [
   'driver' => 'daily',
   'channels' => ['syslog'],
   'path' => storage_path('logs/mycustomlog.log'),
   'level' => 'info',
],

2 个答案:

答案 0 :(得分:2)

您将需要在配置logger.php中拥有频道,请参见here。堆栈驱动程序的目的是向多个通道报告。

'mycustomlog' => [
    'driver' => 'stack',
    'channels' => ['daily'],
    'path' => storage_path('logs/mycustomlog.log'),
    'level' => 'info',
],

答案 1 :(得分:0)

如果您不希望它位于堆栈中,则可以使您的配置指向这样的单个驱动程序

'mycustomlog' => [
  'driver' => 'single',
  'path' => storage_path('logs/mycustomlog.log'),
  'level' => 'info',
]

然后用您自己尝试的方式来称呼它。

Log::channel('mycustomlog')->info($e);
相关问题