【问题标题】:How to create and use a custom daily log file in Laravel?如何在 Laravel 中创建和使用自定义的每日日志文件?
【发布时间】:2020-03-03 22:06:09
【问题描述】:

在 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',
],

【问题讨论】:

  • 也许您不想要“堆栈”驱动程序而是“日常”驱动程序?
  • @lagbox 注意我选择“stack”的原因是因为我需要“daily”驱动程序和“syslog”驱动程序
  • 您说要使用stack 驱动程序并不意味着它正在使用您定义的stack 通道......这是一个名为“stack”的通道,恰好使用“堆栈”驱动程序
  • nice catch @lagbox 我已经添加了频道,但仍未创建自定义日志文件。在下面的 mrhn 的回答中查看我的评论。
  • 请记住,daily 驱动程序会在 days 键下设置的天数之后删除日志。要永久保留它们,只需将其设置为 0

标签: php laravel logging


【解决方案1】:

您需要在配置logger.php 中包含频道,请参阅here。堆栈驱动的重点是向多个通道报告。

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

【讨论】:

  • 我将“通道”添加到配置中,现在我不再收到错误消息,但未创建 mycustomlog-2019-11-07.log,并且我的错误正在记录到laravel-2019-11-07.log 代替。这是因为我指定了“每日”频道吗?
  • 是的,因为名为“daily”的频道被配置为以这种方式工作......您需要创建自己的使用“daily”驱动程序的频道并进行配置
  • @lagbox 完美,谢谢。我会用解决方案更新我的问题。
  • 有人能回答这个问题吗? ::stackoverflow.com/questions/64478010/…
【解决方案2】:

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

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

然后按照您自己尝试的方式调用它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2015-03-14
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多