Home  >  Article  >  PHP Framework  >  Logging system in Yii framework: recording application events

Logging system in Yii framework: recording application events

WBOY
WBOYOriginal
2023-06-21 15:42:301111browse

In the Yii framework, there is a complete logging system that can record application events so that developers can conduct debugging and performance analysis. This article will introduce the basic use of the logging system in the Yii framework and some practical techniques.

  1. Configuring the log component

The Yii framework uses files as the log storage method by default, and you can configure the log component in the configuration file. The following is a simple configuration example:

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
        [
            'class' => 'yiilogFileTarget',
            'levels' => ['error', 'warning'],
            'logFile' => '@runtime/logs/app.log',
        ],
    ],
],

Among them, traceLevel refers to the log level recorded by the Yii framework in debug mode. The higher the level, the more information is recorded. detailed. targets is an array that can configure multiple log targets. In this example, there is only one target, which is to write error and warning level logs@ runtime/logs/app.log file.

  1. Record log information

It is very simple to record log information in the code. You can use the shortcut function provided by the Yii framework, for example:

Yii::info('This is an info message.');
Yii::warning('This is a warning message.');
Yii::error('This is an error message.');

Shortcut function , info, warning and error respectively correspond to the three levels of the log. You can choose to use it according to the situation.

When recording logs, in addition to directly recording strings, you can also use replaceable data, represented by the placeholder {}. For example:

Yii::info('User {username} registered successfully.', ['username' => 'John']);

At this time, {username} will be replaced with 'John'. The advantage of this is that the log information is more detailed and easier to troubleshoot problems.

  1. Using context information

The logging system of the Yii framework supports using context information to record more detailed log information. Context information includes some additional data, such as current user information, request parameters, etc. When recording logs, you can add contextual information by setting the $context parameter, for example:

Yii::warning('Invalid user input', ['category' => 'appcontrollersMyController', 'action' => 'create', 'params' => $_POST]);

In the above code, we use 3 contextual information, which are the controllers to which they belong. Class, requested method and request parameters. This will provide a clearer understanding of where the logs occur and the specific request information.

  1. Filtering and classification

In actual applications, we may not want to record all log information. The logging system of the Yii framework provides a filtering and classification mechanism that can filter and classify log messages based on conditions.

In the configuration, you can set the categories attribute to classify log messages, for example:

'log' => [
    'targets' => [
        [
            'class' => 'yiilogFileTarget',
            'categories' => ['appcontrollersMyController'],
            'logFile' => '@runtime/logs/mycontroller.log',
        ],
    ],
],

In the above configuration, we only log messages from appcontrollersMyController Controller's log information and write them to the @runtime/logs/mycontroller.log file.

In addition to classification, we can also use functions to filter log messages, for example:

'log' => [
    'targets' => [
        [
            'class' => 'yiilogFileTarget',
            'levels' => ['error'],
            'logFile' => '@runtime/logs/app.log',
            'logVars' => [],
            'except' => [
                'yiiwebHttpException:404',
            ],
        ],
    ],
],

In the above configuration, we only record error level log information, and at the same time All contextual information is ignored. We use the except attribute to specify log messages to ignore. In this example, we ignore all 404 error messages.

  1. Customized log target

The logging system of the Yii framework allows us to customize the log target, such as sending log messages to email, databases or third-party log services, etc. . When we need to customize the log target, we need to inherit the yiilogTarget class and implement the export method. For example:

class EmailTarget extends yiilogTarget
{
    public $to;

    public function export()
    {
        foreach ($this->messages as $message) {
            mail($this->to, $message[0], $message[1]);
        }
    }
}

In the above code, we define a custom email target and implement the export method. In the export method, we use the mail function to send the log message to the specified mailbox.

In practical applications, we can write different log targets as needed to meet different log storage requirements.

Summary

The logging system of the Yii framework is a very practical component that can help developers track application events for better debugging and performance analysis. When using the logging system, we need to understand the basic configuration and usage, as well as some practical tips, such as using contextual information, classification and filtering, customizing log targets, and so on. Only by in-depth understanding and flexible use of the log system can we better solve log problems and improve the quality and performance of applications.

The above is the detailed content of Logging system in Yii framework: recording application events. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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