laravel 学习笔记之 日志

日志

配置

laravel 框架中日志系统配置位于 config/logging.php 配置文件中,默认情况下使用 stack 记录日志消息

构建日志堆栈

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['syslog', 'slack'],
    ],

    'syslog' => [
        'driver' => 'syslog',
        'level' => 'debug',
    ],

    'slack' => [
        'driver' => 'slack',
        'url' => env('LOG_SLACK_WEBHOOK_URL'),
        'username' => 'Laravel Log',
        'emoji' => ':boom:',
        'level' => 'critical',
    ],
],

stack 通过借助它的 channels 选项聚合了另外两个通道: syslog 和 slack

日志级别

  • debug (100): 详细的调试信息
  • info (200): 用户登录、SQL 日志等事件
  • notice (250): 普通但有意义的事件
  • warning (300): 非错误的异常,警告信息
  • error (400): 运行时的错误,不需要立即操作,但通常应该被记录和监视
  • critical (500): 至关重要的条件。示例:应用程序组件不可用,意外异常
  • alert (550): 必须立即采取行动。例如:整个网站宕机,数据库不可用等。这会触发短信提醒并唤醒你
  • emergency (600): 紧急情况:系统无法使用

以上日志级别逐级提高

Log::debug('An informational message.');

参照上面的例子,debug 级别的日志会触发 syslog 通道,但不会触发 slack 通道,因为 critical 的级别高于 debug

如果我们记录一条 emergency 消息,它将被发送给 syslog 和 slack,因为 emergency 的级别高于两个通道的最低级别限制

Log::emergency('The system is down!');

写入日志信息

Log::log('debug',$message);
Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);

//默认情况下,消息被写入到在 config/logging.php 配置文件中定义的默认日志通道
Log::log('info', 'Showing user profile for John');
Log::info('Showing user profile for John');
Log::info('User failed to login.', ['id' => $user->id]);
Log::channel('notice')->info('Something happened!'); //指定日志通道
Log::stack(['single', 'slack'])->info('Something happened!'); //多通道构成的按需记录的堆栈
posted @ 2020-10-28 18:01  黑夜的海  阅读(800)  评论(0编辑  收藏  举报