Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2: how to write logs in file while testing with Codeception?

I had set up log component with FileTarget in the main config and expected Yii::error() will write messages in the file when I will launch tests. But logging was catched up by Codeception\Lib\Connector\Yii2\Logger and log folder remains empty.

Is it possible to make Yii2 write logs in this situation?

like image 893
Mik Avatar asked Sep 15 '25 11:09

Mik


2 Answers

You can configure correct logger before test:

public function testSomething() {
    Yii::setLogger(Yii::createObject(\yii\log\Logger::class));
    Yii::$app->log->setLogger(Yii::getLogger());
    // log something
    Yii::getLogger()->flush();
    // test log file
}
like image 165
rob006 Avatar answered Sep 18 '25 04:09

rob006


<?php

namespace tests;

class SomeTest extends \yii\codeception\TestCase
{

    public function testLogMessage()
    {
        \Yii::error('something bad occurred');
    }

}

And this correctly logs error under runtime/log/app.log with message:

2018/05/05 03:42:04 [127.0.0.1] [error] [application] something bad occurred

like image 27
Azraar Azward Avatar answered Sep 18 '25 04:09

Azraar Azward