Swoft 日志

Swoft日志依赖monolog/monolog组件进行扩展,并兼容PSR-3规范。

Swoft日志系统由两部分组成:

  • 日志主体:Swoft\Log\Logger
    通过Swoft\Log\LogLogger进行了一层封装,使调用更加方便。
  • 日志输出:Swoft\Log\FileHandler

开发调试

若在Swoft项目中使用var_dump等PHP原生打印,此时直接访问Swoft HTTP服务器时,在前端Web中是无法查看到数据的,应该怎么办呢?

$ docker-machine ls
$ docker-machine ssh default
docker@default $# docker ps -a
docker@default $# docker logs myswoft -ft

关于docker日志帮助

# 获取容器日志
docker@default $# docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)

docker logs --help 参数配置

  • --since 指定输出了输出日志开始时间,也就是只输出指定日期之后的日志。
  • -f 查看实时日志,跟踪日志输出。
  • -t 显示时间戳
  • --tail=10 查看最后10条日志

日志等级

  • debug 调试日志
  • trace 跟踪日志
  • error 错误日志
  • info 信息打印日志
  • warning 警告日志
  • notice 请求日志
    每个请求会生成唯一一条notice,用于记录整个请求相关的所有信息,方便问题定位。

日志配置

日志环境配置

$ vim .env
LOG_ENABLE=true

日志属性配置

$ vim config/beans/log.php
 [
        'name'          => APP_NAME,
        // 是否开启日志,默认为true,如果为false则不会有任何日志输出。
        'enable'        => env('LOG_ENABLE', false),
        //日志累计到多少条后再同一刷新写入磁盘
        'flushInterval' => 100,
        //是否每个请求刷新一次写入磁盘,过于频繁对性能有损耗。
        'flushRequest'  => true,
        //定义日志输出方式,系统默认配置到文件。
        'handlers'      => [
            '${noticeHandler}',
            '${applicationHandler}',
        ],
    ],
    //notice日志处理器
    'noticeHandler'      => [
        'class'     => \Swoft\Log\FileHandler::class,
        'logFile'   => '@runtime/logs/notice.log',
        //日志格式器
        'formatter' => '${lineFormatter}',
        'levels'    => [
            \Swoft\Log\Logger::NOTICE,
            \Swoft\Log\Logger::INFO,
            \Swoft\Log\Logger::DEBUG,
            \Swoft\Log\Logger::TRACE,
        ],
    ],
    //application日志处理器
    'applicationHandler' => [
        'class'     => \Swoft\Log\FileHandler::class,
        'logFile'   => '@runtime/logs/error.log',
        'formatter' => '${lineFormatter}',
        'levels'    => [
            \Swoft\Log\Logger::ERROR,
            \Swoft\Log\Logger::WARNING,
        ],
    ],
];

日志使用

例如:在HTTP控制器类的动作方法中使用日志记录

App::info("this is info log");
App::error("this is error log");
App::debug("this is debug log");

会在系统\runtime\logs目录下生成error.lognotice.log两个日志文件。

使用App::info方法和App::debug方法记录的日志会出现在notice.log日志文件中

2019-04-27 11:14:07 [info] [swoft] [logid:5cc3c8ff9c37b] [spanid:0] trace[HandlerAdapterMiddleware.php:41,Swoft\Http\Server\Middleware\HandlerAdapterMiddleware->process] this is info log
2019-04-27 11:14:07 [debug] [swoft] [logid:5cc3c8ff9c37b] [spanid:0] trace[HandlerAdapterMiddleware.php:41,Swoft\Http\Server\Middleware\HandlerAdapterMiddleware->process] this is debug log

使用App::error方法记录的日志会出现在error.log日志文件中

2019-04-27 11:14:07 [error] [swoft] [logid:5cc3c8ff9c37b] [spanid:0] trace[HandlerAdapterMiddleware.php:41,Swoft\Http\Server\Middleware\HandlerAdapterMiddleware->process] this is error log

你可能感兴趣的:(Swoft 日志)