Home > Backend Development > PHP Tutorial > PHP example-analysis of how to use logs in Yii2 framework

PHP example-analysis of how to use logs in Yii2 framework

微波
Release: 2023-03-11 19:16:01
Original
1457 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,

Object-oriented

design is implemented more thoroughly, the logging function is transferred to the Logger class, and supports multiple output targets (Targets).

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()

for development and debugging When recording logs, you need to set YII_DEBUG to true.Yii::error()
Used to record unrecoverable errorsYii::warning()
Some warnings InformationYii::info()
Some system behaviors records such as administrator operation prompts

Yii2 customized log output target To 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 PHP example-analysis of how to use logs in 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