yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

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

Open zeleniy opened 1 year ago

zeleniy commented 1 year ago

No logs written while testing with codecept.

schmunk42 commented 1 year ago

Unit, Functional or Acceptance Tests?

zeleniy commented 1 year ago

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));
    ...
DplusG commented 1 year ago

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

zeleniy commented 1 year ago

@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
schmunk42 commented 1 year ago

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

zeleniy commented 1 year ago

Mostly part to files:

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

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.

zeleniy commented 1 year ago

No, i don't.

DplusG commented 1 year ago

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

SOHELAHMED7 commented 1 year ago

@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