Home >PHP Framework >YII >What does yii2 log do?

What does yii2 log do?

(*-*)浩
(*-*)浩Original
2019-12-30 11:54:351926browse

What does yii2 log do?

yii provides a powerful logging framework that is highly customizable and extensible. Using this framework, you can easily log various types of messages, filter them, and collect them to different targets, such as files, databases, and emails.

Using Yii logging framework involves the following steps:                                                                                                                                                                                                                                                                             # Record log messages in various places;Filter and export log messages by configuring log targets in the application configuration;

Check filtered log messages exported by different targets (for example: Yii debugger ).

Log message

Logging a log message is as simple as calling the following logging method:

Yii::trace( ): Record a message to track how a piece of code runs. This is mainly used during development. Yii::info(): Record a message to convey some useful information.

Yii::warning(): Record a warning message to indicate that some accident has occurred.

Yii::error(): Records a fatal error, which should be checked as soon as possible.

Main usage:

'log' => [
    'traceLevel' => YII_DEBUG ? 3 : 0,
    'targets' => [
        'error' => [
            'class' => yii\log\FileTarget::class,
            'levels' => ['error', 'warning'],
            'enableRotation' => false,
            'logVars' => [],
        ],
        'article' => [
            'class' => 'yii\log\FileTarget',
            'categories' => ['pay'],
            'levels' => ['error', 'warning','info'],
            'logVars' => ['*'],
            'logFile' => '@runtime/logs/'.date('Y-m-d').'_article.log',
            'prefix' => function ($message) {
                $user = Yii::$app->has('user', true) ? Yii::$app->get('user') : null;
                $userID = $user ? $user->getId(false) : '-';
                return "[$userID]";
            },
            'enabled' => true
        ],
        'order' => [
            'class' => 'yii\log\FileTarget',
            'categories' => ['order'],
            'levels' => ['error', 'warning'],
            'logVars' => ['*'],
            'logFile' => '@runtime/logs/order.log',
        ],
    ],
],
\Yii::info('start calculating average revenue', 'pay');

The above is the detailed content of What does yii2 log do?. 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