Analysis of log usage in YII2 framework

Source: Internet
Author: User
Tags yii
This article mainly introduces the use of logs in the YII2 framework, compares the common methods of yii1.x analysis YII2 log operations and related use techniques, the need for friends can refer to the following

This article describes how logs are used in the YII2 framework. Share to everyone for your reference, as follows:

The difference between YII2 and yii1.x

Yii2 the use of logs in the same way and Yii 1.x is not the same,

In Yii 1.x, the logging method is


Yii::log ($message, $level, $category); Yii::trace ($message, $category);

The latter logs only in debug mode.

The log method here is a static method of Yiibase.

In Yii2, the object-oriented design is carried out more thoroughly, the logging function is transferred to the logger class, and multiple output targets (Targets) are supported.

How to use logs in Yii2

In order to log, you first need to get the logger class singleton and then call the public logging method of the class:


Yii::getlogger ()->log ($msg, $level, $category)

Examples are as follows:


Yii::getlogger ()->log ("Your site has been hacked", logger::level_error)//default category is application that is the application-level log

In addition, YII2 provides some quick ways to:

Yii::trace()For logging when developing debugging, you need to set Yii_debug to true.
Yii::error()Used to log unrecoverable errors
Yii::warning()Some warning messages
Yii::info()Some system behavior records such as Administrator action tips

YII2 Custom log Output target

To customize a goal, such as to log files and send messages while an unrecoverable error occurs, you can customize the following:


[' bootstrap ' = [' log '],//ensure logger gets loaded before application starts ' Components ' and [' log ' = ' = ' targets ' + [' file ' + = ' class ' = ' Yii\log\fileta          Rget ', ' levels ' = [' trace ', ' info '], ' categories ' = [' yii\* '], ' email ' = [            ' Class ' = ' yii\log\emailtarget ', ' levels ' = [' Error ', ' warning '], ' message ' = [ ' to ' = = [' admin@techbrood.com ', ' support@techbrood.com '], ' subject ' + ' New example.com log Messa GE ',],],], [],], 

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.