日志

webman使用 monolog/monolog 处理日志。

使用

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. use support\Log;
  5. class Foo
  6. {
  7. public function index(Request $request)
  8. {
  9. Log::info('log test');
  10. return response('hello index');
  11. }
  12. }

提供的方法

  1. Log::log($level, $message, array $context = [])
  2. Log::debug($message, array $context = [])
  3. Log::info($message, array $context = [])
  4. Log::notice($message, array $context = [])
  5. Log::warning($message, array $context = [])
  6. Log::error($message, array $context = [])
  7. Log::critical($message, array $context = [])
  8. Log::alert($message, array $context = [])
  9. Log::emergency($message, array $context = [])

等价于

  1. $log = Log::channel('default');
  2. $log->log($level, $message, array $context = [])
  3. $log->debug($message, array $context = [])
  4. $log->info($message, array $context = [])
  5. $log->notice($message, array $context = [])
  6. $log->warning($message, array $context = [])
  7. $log->error($message, array $context = [])
  8. $log->critical($message, array $context = [])
  9. $log->alert($message, array $context = [])
  10. $log->emergency($message, array $context = [])

配置

  1. return [
  2. // 默认日志通道
  3. 'default' => [
  4. // 处理默认通道的handler,可以设置多个
  5. 'handlers' => [
  6. [
  7. // handler类的名字
  8. 'class' => Monolog\Handler\RotatingFileHandler::class,
  9. // handler类的构造函数参数
  10. 'constructor' => [
  11. runtime_path() . '/logs/webman.log',
  12. Monolog\Logger::DEBUG,
  13. ],
  14. // 格式相关
  15. 'formatter' => [
  16. // 格式化处理类的名字
  17. 'class' => Monolog\Formatter\LineFormatter::class,
  18. // 格式化处理类的构造函数参数
  19. 'constructor' => [ null, 'Y-m-d H:i:s', true],
  20. ],
  21. ]
  22. ],
  23. ],
  24. ];

多通道

monolog支持多通道,默认使用default通道。如果想增加一个log2通道,配置类似如下:

  1. return [
  2. // 默认日志通道
  3. 'default' => [
  4. // 处理默认通道的handler,可以设置多个
  5. 'handlers' => [
  6. [
  7. // handler类的名字
  8. 'class' => Monolog\Handler\RotatingFileHandler::class,
  9. // handler类的构造函数参数
  10. 'constructor' => [
  11. runtime_path() . '/logs/webman.log',
  12. Monolog\Logger::DEBUG,
  13. ],
  14. // 格式相关
  15. 'formatter' => [
  16. // 格式化处理类的名字
  17. 'class' => Monolog\Formatter\LineFormatter::class,
  18. // 格式化处理类的构造函数参数
  19. 'constructor' => [ null, 'Y-m-d H:i:s', true],
  20. ],
  21. ]
  22. ],
  23. ],
  24. // log2通道
  25. 'log2' => [
  26. // 处理默认通道的handler,可以设置多个
  27. 'handlers' => [
  28. [
  29. // handler类的名字
  30. 'class' => Monolog\Handler\RotatingFileHandler::class,
  31. // handler类的构造函数参数
  32. 'constructor' => [
  33. runtime_path() . '/logs/log2.log',
  34. Monolog\Logger::DEBUG,
  35. ],
  36. // 格式相关
  37. 'formatter' => [
  38. // 格式化处理类的名字
  39. 'class' => Monolog\Formatter\LineFormatter::class,
  40. // 格式化处理类的构造函数参数
  41. 'constructor' => [ null, 'Y-m-d H:i:s', true],
  42. ],
  43. ]
  44. ],
  45. ],
  46. ];

使用log2通道时用法如下:

  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. use support\Log;
  5. class Foo
  6. {
  7. public function index(Request $request)
  8. {
  9. $log = Log::channel('log2');
  10. $log->info('log2 test');
  11. return response('hello index');
  12. }
  13. }