How does the Yii framework implement logging to a custom file? 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 it. I hope to be helpful.
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 needed Put certain specific 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', ),
Where you need to record order errors, add the following code:
Yii::log('your message', 'error', 'orders');
Related recommendations:
Detailed explanation of properties in Yii
Detailed explanation of YII related query
The above is the detailed content of Detailed explanation of how the Yii framework implements logging to custom files. For more information, please follow other related articles on the PHP Chinese website!