配置与使用

配置

config/beans/log.php

  1. return [
  2. 'noticeHandler' => [
  3. 'class' => \Swoft\Log\FileHandler::class,
  4. 'logFile' => '@runtime/logs/notice.log',
  5. 'formatter' => '${lineFormatter}',
  6. 'levels' => [
  7. \Swoft\Log\Logger::NOTICE,
  8. \Swoft\Log\Logger::INFO,
  9. \Swoft\Log\Logger::DEBUG,
  10. \Swoft\Log\Logger::TRACE,
  11. ],
  12. ],
  13. 'applicationHandler' => [
  14. 'class' => \Swoft\Log\FileHandler::class,
  15. 'logFile' => '@runtime/logs/error.log',
  16. 'formatter' => '${lineFormatter}',
  17. 'levels' => [
  18. \Swoft\Log\Logger::ERROR,
  19. \Swoft\Log\Logger::WARNING,
  20. ],
  21. ],
  22. 'logger' => [
  23. 'name' => APP_NAME,
  24. 'enable' => true,
  25. 'flushInterval' => 100,
  26. 'flushRequest' => true,
  27. 'handlers' => [
  28. '${noticeHandler}',
  29. '${applicationHandler}',
  30. ],
  31. ],
  32. ];
  • enable 是否开启日志,默认true,如果为false则不会有任何日志输出
  • flushInterval 定义日志累计到了多少条,再统一刷新一次写入到磁盘
  • flushRequest 是否每个请求刷新一次写入到磁盘,过于频繁对性能会有损耗
  • handlers 定义日志输出方式,系统默认配置的文件,用户可扩展其它输出方式,在这里配置即可
  • handler 每一个日志处理器,都可以配置,处理日志的格式集合,可以把日志输出到多个地方
  • formatter 日志格式器,默认使用lineFormatter(即\Monolog\Formatter\LineFormatter),此处可自定义格式器,需继承\Monolog\Formatter\NormalizerFormatter,并实现 LineFormatter 中对应的公有方法,修改常量SIMPLE_FORMATformat()方法即可对输出的日志格式做自定义,需要注意的是,格式器需定义为一个Bean

实例

  1. // 标记开始
  2. App::profileStart("tag");
  3. // 直接输出异常
  4. App::error(new \Exception("error exception"));
  5. App::error("this errro log");
  6. App::info("this errro log");
  7. // 数组出
  8. App::error(['name' => 'boy']);
  9. App::debug("this errro log");
  10. // 标记结束
  11. App::profileEnd("tag");
  12. // 统计缓存命中率
  13. App::counting("cache", 1, 10);
  • profileStart/profileEnd 用于统计两个标记点时间
  • counting 统计缓存命中率

日志格式

  1. 2017/08/06 03:11:00 [error] [swoft] [logid:598688c45cbbf] [spanid:0] trace[IndexController.php:123,app\controllers\IndexController->actionLog] Exception: error exception in /home/worker/data/www/swoft/app/controllers/IndexController.php:123 Stack trace: #0 /home/worker/data/www/swoft/src/base/Controller.php(77): app\controllers\IndexController->actionLog() #1 /home/worker/data/www/swoft/src/base/Controller.php(58): swoft\base\Controller->runActionWithParams('log', Array) #2 /home/worker/data/www/swoft/src/base/Controller.php(41): swoft\base\Controller->runAction('log', Array) #3 /home/worker/data/www/swoft/src/web/Application.php(248): swoft\base\Controller->run('log', Array) #4 /home/worker/data/www/swoft/src/web/Application.php(140): swoft\web\Application->runControllerWithFilters(Object(swoft\web\Request), Object(swoft\web\Response), Object(app\controllers\IndexController), 'log', Array) #5 {main}
  2. 2017/08/06 03:11:00 [error] [swoft] [logid:598688c45cbbf] [spanid:0] trace[IndexController.php:124,app\controllers\IndexController->actionLog] this errro log
  3. 2017/08/06 03:11:00 [error] [swoft] [logid:598688c45cbbf] [spanid:0] trace[IndexController.php:126,app\controllers\IndexController->actionLog] {"name":"boy"}
  4. 2017/08/06 03:11:00 [info] [swoft] [logid:598688c45cbbf] [spanid:0] trace[IndexController.php:125,app\controllers\IndexController->actionLog] this errro log
  5. 2017/08/06 03:11:00 [debug] [swoft] [logid:598688c45cbbf] [spanid:0] trace[IndexController.php:127,app\controllers\IndexController->actionLog] this errro log
  6. 2017/08/06 03:11:00 [notice] [swoft] [logid:598688c45cbbf] [spanid:0] [168(ms)] [2(MB)] [/index/log] [] profile[app.route.match=7.52(ms)/1,app.route=7.53(ms)/1,tag=13.58(ms)/1] counting[cache=1/10]
  • 每个请求只会有一条notice日志,框架底层自动生成的,用户无需关心。