This article mainly introduces the method of recording logs to custom files in the Yii framework. It analyzes the principles of Yii framework logging and the related configuration and implementation skills of custom logging in the form of examples. Friends in need can refer to the following
The example in this article describes how the Yii framework implements logging to a custom file. Share it with everyone for your reference, the details are as follows:
By default, Yii::log($msg, $level, $category)
will record the log to runtime/application.log The
log format in the file is as follows:
[Time] - [Level] - [Category] - [Content]
2013/05/03 17:33:08 [error] [application] test
But sometimes it is necessary to put certain logs into specific files, such as transaction failure logs, which need to be recorded separately from other logs.
This can be solved by configuring different CLogRouter in Yii.
You need to first understand Yii’s logging mechanism. Yii’s logging function consists of two parts: CLogger and CLogRouter.
CLogger is responsible for recording log data in memory, while CLogRouter decides how to process these logs. Data, such as recording to files or databases, or sending emails, etc.
The CFileLogRoute is used to process log data in the form of files. So naturally, logs can be recorded to different log files by configuring different CFileLogRoutes.
The specific configuration is as follows:
'log' => array( 'class' => 'CLogRouter', 'routes' => array( array( 'class' => 'CFileLogRoute', 'levels' => 'error, warning', ), array( 'class' => 'CFileLogRoute', 'levels' => 'error, warning', 'categories'=> 'orders.*', 'logFile'=> 'orders.log', ),
Add the following code where order errors need to be recorded:
Yii::log('your message', 'error', 'orders');
The above is the detailed content of Yii framework implements the method of recording logs to custom files. For more information, please follow other related articles on the PHP Chinese website!