Home  >  Article  >  PHP Framework  >  How to write logs in Yii framework?

How to write logs in Yii framework?

angryTom
angryTomOriginal
2020-01-21 18:59:423337browse

During development, in order to facilitate us to debug the program and record information, we will write the necessary information into a file, which is the log file. The yii framework provides us with a good method of writing logs. Let's take a look at it together. Take a look.

How to write logs in Yii framework?

#How does the yii framework write logs?

1. To use log with Yii, you first need to modify the configuration file:

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
        [
            'class' => 'yii\log\FileTarget',
            'levels' => ['error', 'warning'],
            'logVars' => ['_GET'],
        ],
    ],
],

targets parameter explanation:

● class specifies the use of files to save logs

● Levels specifies saving error and warning logs

● logVars specifies saving get parameters

If logVars is not specified, GET POST COOKIE will be saved SESSION SERVER and other parameters, which will affect our viewing, we can modify it to

'logVars' => ['*'], // 只记录message

2, use the following:

Yii::error($message);
Yii::warning($message);

Write the log to Different files

Method 1: First assign the log file address where the log needs to be recorded, and then write the log

Yii::$app->log->targets[0]->logFile = Yii::getAlias('@runtime').DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'app2.log';
Yii::warning($message);

Method 2 (recommended): Modify the configuration file main.php

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
        [
            'class' => 'yii\log\FileTarget',
            'levels' => ['error', 'warning'],
            'logVars' => ['*'],
            //'categories' => ['application'],
            //'logFile' => '@runtime/logs/app.log',
        ],
        [
            'class' => 'yii\log\FileTarget',
            'categories' => ['pay'],
            'levels' => ['error', 'warning'],
            'logVars' => ['*'],
            'logFile' => '@runtime/logs/pay.log',
        ],
        [
            'class' => 'yii\log\FileTarget',
            'categories' => ['order'],
            'levels' => ['error', 'warning'],
            'logVars' => ['*'],
            'logFile' => '@runtime/logs/order.log',
        ],
    ],
],

Use:

Yii::warning($message,'pay')

The message here will be recorded in pay.log, and of course it will also be recorded in the default In

in app.log, you can uncomment this code: //'categories' => ['application'],, so that it will only be recorded in the respective logs. .

But this will also cause some error information not to be recorded in app.log.

The above is the detailed content of How to write logs in Yii framework?. 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