Detailed explanation of how the Yii framework implements logging to custom files

*文
Release: 2023-03-19 07:34:01
Original
1500 people have browsed it

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

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

Where you need to record order errors, add the following code:

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

Related recommendations:

Detailed explanation of properties in Yii

Detailed explanation of YII related query

Form form in Yii framework

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!

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!