Yii2log output to file and database

Source: Internet
Author: User
Yii2log output to file and database edit config/web. php

First, log must be enabled.

'bootstrap' => [    'log'],

[File]

'components' => [    'log' => [        'targets' => [            [                'class' => 'yii\log\FileTarget',                'exportInterval' => 1,            ],        ],    ],

Default output to runtime/logs/app. log

Note that webserver or console users must have the permission to write the file

[Database]

'log' => [    'targets' => [        [            'class' => 'yii\log\DbTarget',            'levels' => ['error', 'warning', 'trace'],        ]    ]],

The {% log} table of the database corresponding to the db component is output by default.

Run the following command in the yii2 directory to generate the corresponding table schema

./yii migrate --migrationPath=@yii/log/migrations/

Note that the configuration in config/console. php must be the same as that in web. php. Otherwise, the command cannot be executed successfully.

You can also configure different log modes based on different environments.

'components' => [    'log' => [        'traceLevel' => YII_ENV == 'dev' ? 3 : 0,        'targets' => [            [                'class' => 'yii\log\DbTarget',                'levels' => YII_DEBUG ? ['error', 'warning', 'trace'] : ['error'],            ],            [                'class' => 'yii\log\FileTarget',                'levels' => YII_DEBUG ? ['error', 'warning', 'trace'] : ['error', 'warning'],            ],        ],    ],],

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.