Skip to content

Instantly share code, notes, and snippets.

@rija
Created May 6, 2020 14:08
Show Gist options
  • Save rija/ed41f43417287f52855977466558241b to your computer and use it in GitHub Desktop.
Save rija/ed41f43417287f52855977466558241b to your computer and use it in GitHub Desktop.
Yii2 logging in codeception

If your Yii2 app is set up with Codeception and its Yii2 module for automated tests, the regular log output of the app will show in the terminal when increasing the verbosity of codecept (at least two levels: -vv).

For example, if in a backend controller action MoveAction.php I add

Yii::error("I am an error");
Yii::warning("I am a warning");
Yii::info("I am an info");
Yii::debug("I am a debug");
Yii::trace("I am a trace");

When I run the tests (assuming an app using the Yii2-advanced template):

$ /app/vendor/bin/codecept -vv -c /app/backend run functional

Then it prints on the terminal:

...
I am bearer authenticated "(...)"
I send post "/accounts/move/421"
  [Request] POST /accounts/move/421 []
  [Request Headers] {"Authorization":"Bearer (...)"}
  [yii\web\Session::open] 'Session started'
  ...
  [application] 'I am an error'
  [application] 'I am a warning'
  [application] 'I am an info'
  [Page] /accounts/move/421
  [Response] 200
  [Request Cookies] []
  [Response Headers] {"vary":["Accept"],"content-type":["application/json; charset=UTF-8"]}
  [Response] (...)
 I see response code is 200
...

So your logging will show as long as they are of error, warning, or info levels (neither the levels setting in Yii2 config nor increasing verbosity of codecept command seem to alter this)

My tests/functional.suite.yml looks like so:

suite_namespace: backend\tests\functional
actor: FunctionalTester
modules:
    enabled:
        - Yii2
        - Asserts
        - REST:
            depends: Yii2
            part: Json

my config/codeception-local.php is:

<?php

return yii\helpers\ArrayHelper::merge(
    require dirname(dirname(__DIR__)) . '/common/config/codeception-local.php',
    require __DIR__ . '/main.php',
    require __DIR__ . '/main-local.php',
    require __DIR__ . '/test.php',
    require __DIR__ . '/test-local.php',
    [
    ]
);

and I have in my backend app's codeception.yml:

...
modules:
    config:
        Yii2:
            configFile: 'config/codeception-local.php'
..

and relevant snippets in ``config/main.php` are like in the Yii2 guide on logging:

$config =  [
...
    'bootstrap' => ['log'],
..
    'timeZone' => 'Europe/London',
    'components' => [
..
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning','info'],
                ],
            ],
        ],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment