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-%d{Y-m-d}.log',  // 2.0.6 支持日志按时间切割
        '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'),
        ],
    ]
]; 

This configuration is also the default configuration file for the framework. The application logs are written to two different files at the log level.

Swoft 2.0.3 levels modified into strings for developers to override the frame default configuration

Log format

lineFormatter configuration log format:

  • Format log to file format
  • dateFormat log output time format

Log processor

noticeHandler and applicationHandler processors, the application log can be configured with multiple processors, and the processor can output logs to files, mailboxes, and third-party systems.

Configure detailed parameters:

  • Class configuration of which type, the framework provides files by default, users can extend other types by themselves
  • logFile output log file path, support alias
    • 2.0.6 Support log is cut by time, format: %d{Ymd} , the format in { } applies to PHP date format
  • The formatter log output uses the log format, which is the previously configured log format.
  • Levels support log levels for log output

Log configuration

logger log configuration:

  • Name name
  • flushInterval log output frequency default 1
  • flushRequest Whether each request ends the output log open, default false
  • Enable Whether to enable logging, default false
  • Json whether to output in JSON format, default false
  • Items other data, configuration is an array collection

use

The developer prints the log directly through the static method of the Log class. The application logs are divided into the following different log records according to different requirements:

  • 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