Application log

Configuration

Configure the parameters of the application log in bean.php:

return [
    'lineFormatter'      => [
        'format'     => '%datetime% [%level_name%] [%channel%] [%event%] [tid:%tid%] [cid:%cid%] [traceid:%traceid%] [spanid:%spanid%] [parentid:%parentid%] %messages%',
        'dateFormat' => 'Y-m-d H:i:s',
    ],
    'noticeHandler'      => [
        'class'     => FileHandler::class,
        'logFile'   => '@runtime/logs/notice.log',
        'formatter' => \bean('lineFormatter'),
        'levels'    => 'notice,info,debug,trace',
    ],
    'applicationHandler' => [
        'class'     => FileHandler::class,
        'logFile'   => '@runtime/logs/error.log',
        'formatter' => \bean('lineFormatter'),
        'levels'    => 'error,warning',
    ],
    'logger'             => [
        'flushRequest' => false,
        'enable'       => false,
        'handlers'     => [
            'application' => \bean('applicationHandler'),
            'notice'      => \bean('noticeHandler'),
        ],
    ]
];

此配置也是框架默认的配置文件,把应用日志按日志级别分别写到两个不同的文件里面。

swoft 2.0.3 levels 修改成字符串,方便开发者覆盖框架默认配置

日志格式

lineFormatter 配置日志格式:

  • format 日志输到文件格式
  • dateFormat 日志输出时间格式

日志处理器

noticeHandlerapplicationHandler 处理器,应用日志可以配置多个处理器,处理器可以把日志输出到文件、邮箱、第三方系统。

配置详细参数:

  • class 配置采用哪种类型的,框架默认提供文件,用户可以自己扩展其它类型
  • logFile 输出日志文件路径,支持别名
  • formatter 日志输出使用日志格式,就是之前配置的日志格式
  • levels 支持日志输出的日志级别

日志配置

logger 日志配置:

  • name 名称
  • flushInterval 日志输出频率默认 1
  • flushRequest 是否每个请求结束输出日志开,默认 false
  • enable 是否开启日志,默认 false
  • json 是否 JSON 格式输出,默认 false
  • items 日志其它数据,配置是一个数组集合

使用

开发者直接通过 Log 类静态方法,打印日志。应用日志根据不同的需求分为如下不同的日志记录:

  • emergency
  • debug
  • alert
  • info
  • warning
  • error

// Tag start
Log::profileStart('tagName');

Log::debug('this %s log', 'debug');
Log::info('this %s log', 'info');
Log::warning('this %s log', 'warning');
Log::error('this %s log', 'error');
Log::alert('this %s log', 'alert');
Log::emergency('this %s log', 'emergency');

// Pushlog
Log::pushLog('key', 'value');
Log::pushLog('key', ['value']);
Log::pushLog('key', 'value');

// Tag end
Log::profileEnd('tagName');

// Counting
Log::counting('mget', 1, 10);
Log::counting('mget', 2, 10);

// 2019-05-11 06:57:27 [info] [swoft] [request] [tid:4] [cid:4] [traceid:] [spanid:] [parentid:] trace[LogController.php:29,App\Http\Controller\LogController->test] info message
// 2019-05-11 06:57:27 [debug] [swoft] [request] [tid:4] [cid:4] [traceid:] [spanid:] [parentid:] trace[LogController.php:35,App\Http\Controller\LogController->test] this debug log
// 2019-05-11 06:57:27 [info] [swoft] [request] [tid:4] [cid:4] [traceid:] [spanid:] [parentid:] trace[LogController.php:36,App\Http\Controller\LogController->test] this info log
// 2019-05-11 06:57:27 [warning] [swoft] [request] [tid:4] [cid:4] [traceid:] [spanid:] [parentid:] trace[LogController.php:37,App\Http\Controller\LogController->test] this warning log
// 2019-05-11 06:57:27 [error] [swoft] [request] [tid:4] [cid:4] [traceid:] [spanid:] [parentid:] trace[LogController.php:38,App\Http\Controller\LogController->test] this error log
// 2019-05-11 06:57:27 [notice] [swoft] [request] [tid:4] [cid:5] [traceid:] [spanid:] [parentid:] [8.52(ms)] [27(MB)] [/log/test] [key=value key=["value"] key=value] profile[tagName=5.02(ms)/2] counting[mget=3/20]

The log level can be defined according to its own business. The debug log level is only output when APP_DEBUG is enabled APP_DEBUG default. notice log level, each request end frame is automatically added with a log, which will record the request, frame and Details of business operations, such as database operation records, cache operation records, and request information. The log method can pass parameters like the sprintf() function, and the underlying is automatically encapsulated.

In addition to the common log level, the framework also provides a pushLog profile

  • pushLog used to record the parameters during the execution of the request. The notice log can be viewed inside.
  • profile used, you need to define a tag. The tag starts and ends. This method can record the running time between tags. The notice log can be viewed.
/docs/2.x/en/log/log.html
progress-bar