Yii framework implements the method of recording logs to custom files

巴扎黑
Release: 2023-03-15 06:04:01
Original
1112 people have browsed it

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
Copy after login

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',
    ),
Copy after login

Add the following code where order errors need to be recorded:


Yii::log('your message', 'error', 'orders');
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!