Detailed introduction to the use of logs in the Yii2 framework

巴扎黑
Release: 2023-03-15 06:28:02
Original
1957 people have browsed it

This article mainly introduces the use of logs in the Yii2 framework, and compares the common methods and related usage techniques of Yii1. How to use logs in the framework. Share it with everyone for your reference. The details are as follows:

The difference between Yii2 and Yii1.xHow to use logs in Yii2 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);
Copy after login

The latter only records logs in debug mode.

The log method here is the static method of YiiBase.

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

How to use logs in Yii2In order to record logs, you first need to obtain a single instance of the Logger class, and then call the public log of this class Recording method:

Yii::getLogger()->log($msg, $level, $category)
Copy after login

Examples are as follows:

Yii::getLogger()->log(“your site has been hacked”, Logger::LEVEL_ERROR)
//默认category为application即应用程序级别的日志
Copy after login

In addition, Yii2 provides some shortcut methods:

Yii::trace()

Used to record logs during development and debugging. YII_DEBUG needs to be set to true. Yii::error()
Used to record unrecoverable errors ErrorYii::warning()
Some warning messagesYii::info()
Some system behavior records such as administrator operation prompts

Yii2 customized log output targetTo customize the target, for example, if you want to record files and send emails at the same time when an unrecoverable error occurs, you can customize it as follows:

[
  'bootstrap' => ['log'], // ensure logger gets loaded before application starts
  'components' => [
    'log' => [
      'targets' => [
        'file' => [
          'class' => 'yii\log\FileTarget',
          '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 message',
          ],
        ],
      ],
    ],
  ],
]
Copy after login

The above is the detailed content of Detailed introduction to the use of logs in the Yii2 framework. 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!