Yii 2.0 customizable log message fileTarget

1.0.0 2018-11-07 11:49 UTC

This package is auto-updated.

Last update: 2023-05-26 15:44:28 UTC


Customize the log messages

Sort and change the log message's units


Just run in your console:

composer require donsimon/yii2-log-target-file "*"

or add

"donsimon/yii2-log-target-file": "*"

to the require section of your composer.json file.


Add following line to your main configuration file (e.g. config/main.php),

    'bootstrap' => ['log'],


Add following lines to the components section in configuration file,

        'log' => [
        //          'traceLevel' => YII_DEBUG ? 3 : 0,
                    'targets' => [
                                'class' => 'donsimon\log\logTargetFile',  
                                'levels' => ['info','warning','error'],
                                'categories' => ['user','application'],
                                'logVars' => ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER'],
                                'logFile' => '@backend/runtime/logs/appAndUser.log',
                                'logMessageContainer' =>['timestamp','prefix','level','category','message'],

To write on log file client should call something like this,

Yii::info($message, $category);

Here $message and $category are variables.

your log will update.

What you can do here,

You can change log file name using logFile.

eg: @backend/runtime/logs/user-activities.log

You can add many categories.

eg: ['yii\web\HttpException:*','yii\base\ErrorException:*','user','application']

You can add many levels

eg: ['info','warning','error']

You can print vars using logVars.

Refer more


logMessageContainer is a array of log messages units ['timestamp','prefix','level','category','message'].

You can sort and change log messages units using logMessageContainer

prefixContainer is a array of prefix units ['ip','userId','sessionId'].

You can sort and change log messages prefix using prefixContainer