当前位置:Gxlcms > PHP教程 > yii2log输出到file及database

yii2log输出到file及database

时间:2021-07-01 10:21:17 帮助过:19人阅读

编辑config/web.php

首先log要开启

'bootstrap' => [
    'log'
],

[file]

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

默认输出到runtime/logs/app.log

注意webserver或控制台用户要有写入该文件的权限

[database]

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

默认输出db组件对应的数据库下{{%log}}表

yii2根目录下运行下面命令生成对应的table schema

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

注意config/console.php下也要有和web.php同样的配置,否则命令执行不成功。

还可以根据不同的环境配置不同的日志模式

'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'],
            ],
        ],
    ],
],

人气教程排行