Yii Framework implements a method for logging to a custom file

Source: Internet
Author: User
This article mainly introduced the YII framework to implement the logging to the custom file method, combined with the case form analysis of YII framework logging principle and custom log records related configuration and implementation skills, the need for friends can refer to the next

The example in this paper describes how the Yii framework implements logging to a custom file. Share to everyone for your reference, as follows:

By default, Yii::log($msg, $level, $category) logs are logged to the Runtime/application.log file

The log format is as follows:

[Time]-[level]-[category]-[content]


2013/05/03 17:33:08 [ERROR] [application] Test

Sometimes, however, you need to put certain logs in a particular file, such as a log of a failed transaction, and separate records from other logs.

In Yii, it can be solved by configuring different clogrouter.

You need to know the logging mechanism of Yii First, Yii's log function has Clogger and clogrouter two parts,

Where Clogger is responsible for logging the log data in memory, and Clogrouter decides how to process the log data, such as logging to a file or database, or sending mail, etc.

The cfilelogroute is used to process log data in the form of files. Naturally, by configuring different Cfilelogroute, logs can be logged to different log files.

The specific configuration is as follows:


' Log ' = = Array (  ' class ' = ' Clogrouter ',  ' routes ' = = Array (      ' class ' = ') ' Cfilelogroute ',      ' levels ' = ' Error, warning ', ', '    Array (      ' class ' = ' Cfilelogroute ',      ' Levels ' + ' ERROR, warning ',      ' categories ' = ' orders.* ',      ' logFile ' = ' orders.log ',    

Add the following code where you need to record an order error:


Yii::log (' Your message ', ' Error ', ' orders ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.