Home >PHP Framework >ThinkPHP >Log changes in ThinkPHP6.0 version
The log analogy of 6.0 is much improved compared to the previous version. The main new features include:
·Log information formatting
·Multi-channel writing support
·Delay/real-time Write
·Log information processing event
·Log level specified channel writing
· Support closing logs/channels
The biggest change in the new version of the log class is that it supports multi-channel writing, which means you can write at the same time Or add additional log channel writing to some log types. For example, for some abnormal logs, you can choose to send error notifications to emails.
Other changes mainly include:
·WEB logs and CLI logs are merged and formatted into a unified format;
·Access information is no longer recorded by default, you can record it yourself if needed;
·Independent logs also support JSON format Recording;
The related configuration of the log can be set in the config/log.php file. This article mainly describes the changes in the new version of the log.
Log channel
The concept of log channel is equivalent to creating a log space for log information. The log information of each log channel adopts an independent writing mechanism or configuration. Parameters, you can specify the default log channel for log information, or you can dynamically switch channel writing, and support the same log information being written to multiple log channels at the same time.
You can define channels in the channels configuration of the log configuration file. By default, only one file channel is defined.
// 日志通道列表 'channels' => [ 'file' => [ // 日志记录方式 'type' => 'File', // 日志保存目录 'path' => '', // 单文件日志写入 'single' => false, // 独立日志级别 'apart_level' => [], // 最大日志文件数量 'max_files' => 0, // 使用JSON格式记录 'json' => false, // 日志输出格式化 'format' => '[%s][%s] %s', // 是否实时写入 'realtime_write' => false, ], // 其它日志通道配置 ],
Each log channel must specify the type parameter, which determines the writing method of the log. File means writing in the file log mode. If you need to specify other writing methods, you need to install additional extensions. . But you can also specify the same type for different log channels, but the other configuration parameters are different, such as different writing paths.
No matter what log writing type is used, the common configurations that each log channel can support are as follows:
The usage of the level parameter is consistent with the global log configuration. Except in the command line mode, the default log is written once after the request is completed. If you need to write log information in real time, you need to set it up.
'realtime_write'=>true,
If it is under the command line, the log will be automatically written in real time regardless of whether it is set.
Default channel
Define the default channel of the log by setting the default parameter, for example:
'default'=>'file',
Level channel
You can set different log levels and use different log channels.
'type_channel'=>[ // 对于error日志级别 同时写入file和email两个日志通道 'error'=>['file','email'], ]
After setting a level channel, logs at this level will not be recorded to the default channel, please note.
The log channel requires the cooperation of the log driver. You can also customize the log driver.
Switch channel
You can also manually switch the default writing channel of the current log, for example:
Log::channel('email')->info('这是日志信息');
Supports switching to use multiple channel recording , for example:
Log::channel(['email', 'file'])->info('这是日志信息');
Log processing
Log writing supports event monitoring, for example:
Event::listen('think\event\LogWrite', function($event) { if('file' == $event->channel) { $event->log['info'][] = 'test info'; } });
The parameter of the closure is a LogWrite event class object instance , you can get two attributes:
The log information is a two-dimensional array that contains all levels of log information under the current log channel (allowed to be recorded).
Format log information
The system provides two parameters for formatting log information. The first one is time_format for customizing the time display format. The second is to adjust the format parameter of the log output format.
'channels' => [ 'file' => [ 'type' => 'file', 'json' =>true 'file_size' => 1024*1024*10, 'time_format' => 'Y-m-d H:i:s', 'format' => '[%s][%s]:%s', ], ],
The first %s of the format parameter is the log recording time (the format of the log time is defined by the time_format parameter) The second %s is the log level The third %s is the log information, the order cannot be adjusted .
JSON format log
can support JSON format recording file logs, making it more convenient for some third-party log analysis tools to perform log analysis.
In the log configuration file, add
return [ 'default' => 'file', 'channels' => [ 'file' => [ 'type' => 'file', 'json' =>true 'file_size' => 1024*1024*10, ], ], ];
to enable JSON format logging. CLI command line logging is also valid.
和之前版本的区别在于,新版的每个日志信息都是一条JSON数据(旧版本是每个请求一条JSON数据)。
关闭日志
你可以通过调用close方法动态关闭日志写入。
// 关闭全局日志写入 Log::close(); // 关闭某个通道日志写入 Log::close('file');
如果调用close方法动态关闭日志,会自动调用clear方法清空日志。
清空日志
一旦执行save方法后,内存中的日志信息就会被自动清空,如果需要提前清空日志可以使用:
// 清空所有日志 Log::clear(); //清空通道日志可以使用 Log::clear('file');
在清空日志方法之前,你可以使用getLog方法获取内存中的日志。
// 获取(默认通道)日志 $logs = Log::getLog(); // 获取指定通道日志 $logs = Log::getLog('file');
日志清空仅仅是清空内存中的日志。
自定义驱动
日志通道需要自定义日志驱动,该日志驱动需要实现think\contract\LogHandlerInterface接口。
interface LogHandlerInterface { /** * 日志写入接口 * @access public * @param array $log 日志信息 * @return bool */ public function save(array $log): bool; }
众多ThinkPHP教程,尽在PHP中文网,欢迎在线学习!
本文转自:https://blog.thinkphp.cn/1186947
The above is the detailed content of Log changes in ThinkPHP6.0 version. For more information, please follow other related articles on the PHP Chinese website!