yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Logging not working when app run with ./vendor/bin/codecept

Open zeleniy opened this issue 1 year ago • 10 comments

No logs written while testing with codecept.

zeleniy avatar Apr 27 '23 10:04 zeleniy

Unit, Functional or Acceptance Tests?

schmunk42 avatar Apr 27 '23 11:04 schmunk42

Unit. When i run tests i see that \Codeception\Lib\Connector\Yii2\Logger used which produce no output. And i am trying to replace it, but in most cases this is doesn't help:

  protected function _before() {
    /*
     * Replace \Codeception\Lib\Connector\Yii2\Logger which supress messages with Yii2 standard logger.
     */
    \Yii::setLogger(\Yii::createObject(\yii\log\Logger::class));
    ...

zeleniy avatar Apr 27 '23 11:04 zeleniy

@zeleniy Try to add Yii::$app->log->setLogger(Yii::getLogger()); after \Yii::setLogger...

DplusG avatar Apr 27 '23 14:04 DplusG

@DplusG, no it doesn't help. If to modify BaseYii::info in such way:

public static function info($message, $category = 'application')
{
    (new Output([]))->debug([ __METHOD__, get_class(static::getLogger()) ]);
    static::getLogger()->log($message, Logger::LEVEL_INFO, $category);
}

It will produce next output:

  Array
  (
      [0] => yii\BaseYii::info
      [1] => yii\log\Logger
  )


  Array
  (
      [0] => yii\BaseYii::info
      [1] => yii\log\Logger
  )


  Array
  (
      [0] => yii\BaseYii::info
      [1] => Codeception\Lib\Connector\Yii2\Logger
  )


  Array
  (
      [0] => yii\BaseYii::info
      [1] => Codeception\Lib\Connector\Yii2\Logger
  )

  ...

on test run:

./vendor/bin/codecept run common/tests/unit/components/counting/HandlerBillTest.php

zeleniy avatar Apr 27 '23 14:04 zeleniy

Where do you expect the logs to be written (file, db, stdout, ...)?

schmunk42 avatar Apr 28 '23 08:04 schmunk42

Mostly part to files:

...
[
    'class' => 'yii\log\FileTarget',
    'categories' => ['1c-put-bill'],
    'logFile' => '@app_root/logs/1c/put-bill.log',
    'logVars' => [],
],
...

zeleniy avatar Apr 28 '23 10:04 zeleniy

Do you see any errors, warnings when running unit tests in STDOUT?

You might try an absolute path, in case aliases are not set-up properly.

schmunk42 avatar May 02 '23 10:05 schmunk42

No, i don't.

zeleniy avatar May 12 '23 08:05 zeleniy

Just for the test. Try to change category to default when log method is called. Yii::info('hi') and remove 'categories' => ['1c-put-bill'], I think that may not working with custom categories

DplusG avatar May 12 '23 12:05 DplusG

@zeleniy

Mostly part to files ...

Are you completely sure that this config is not ignored in test environment?

Also ensure file @app_root/logs/1c/put-bill.log is writable by OS user which run $ ./vendor/bin/codecept

SOHELAHMED7 avatar Jun 09 '23 11:06 SOHELAHMED7