Usage

By default pytest-logger does nothing in scope of logging. It’s only default action is to add new hooks and fixture. Terminal output, logfiles together with their directory structure, symlink appear only when explicitly requested by user via one of hooks or fixture.

Logging to stdout or files

Implement pytest-logger hooks in your conftest.py to direct logs to terminal or files. You can make up your own cmdline arguments relevant to your test environment for convenience.

Terminal output mixes with normal pytest’s output in graceful manner. File output is stored in logs directory (see logs dir layout and link to logs dir).

import logging
def pytest_logger_stdoutloggers(item):
    # handles foo logger at chosen level and bar at all levels
    return [('foo', logging.WARN), 'bar']

def pytest_logger_fileloggers(item):
    # handles root logger
    return ['']

Things to note:

  • stdout capturing: in order to see logs printed on terminal in real time during test execution, you need to disable output capturing by -s switch.
  • default root level: by default root logger (and all its children) has warning level threshold set. This can filter logs regardless of handler levels user gives via hooks. Be sure to set root logger level as NOTSET if you don’t want this to happen.
  • no handlers warning: if log wouldn’t get filtered, but there are no handlers added to logger, unwanted message is printed. Add NullHandler to such loggers.
  • default logging config: if root logger has no handlers, using module level logging functions will setup basic logging config. It makes no sense in combintion with this plugin. Be sure root logger has some handler (at least NullHandler) or just don’t use these functions.
  • pytest-xdist: stdout output is not printed to terminal in pytest-xdist runs. File output works as in single process mode.

The logs directory layout

Directory with logfiles is located
  • under test session’s basetemp directory named “logs”
tmp/
└── pytest-of-aurzenligl
    ├── pytest-0
    │   └── logs
    │       (...)
    ├── pytest-1
    │   └── logs
    │       (...)
    └── pytest-2
        └── logs
            (...)
or
  • under predefined location, if –logger-logsdir option or logger_logsdir entry in configuration file defined
<logger_logsdir>/
└── (...)

It has structure following pytest test item’s nodeid.

  • test directories are directories
  • test filenames are directories
  • test functions are directories (each parametrized testcase variant is distinct)
  • each registered logger is a file (root logger is called ‘root’)
logs/
├── classtests
│   └── test_y.py
│       └── TestClass.test_class
│           ├── daemon
│           └── setup
├── parametrictests
│   └── test_z.py
│       ├── test_param-2-abc
│       └── test_param-4.127-de
│           └── setup
└── test_p.py
    └── test_cat
        └── proc

The logdir fixture

Like pytest’s tmpdir it’s a py.path.local object which offers os.path methods. Points to logs subdirectory related to particular testcase. Directory is ensured to exist and custom log files can be written into it:

def test_foo(logdir):
    logdir.join('myfile.txt').write('abc')

API reference

class pytest_logger.plugin.LoggerHookspec
pytest_logger_fileloggers(item)

called before testcase setup. If implemented, given loggers will emit their output to files within logs temporary directory.

Parameters:item – test item for which handlers are to be setup.
Return list:List should contain logger name strings or tuples with logger name string and logging level.

called after cmdline options parsing. If implemented, symlink to logs directory will be created.

Parameters:config – pytest config object, holds e.g. options
Return string:Absolute path of requested link to logs directory.
pytest_logger_stdoutloggers(item)

called before testcase setup. If implemented, given loggers will emit their output to terminal output.

Parameters:item – test item for which handlers are to be setup.
Return list:List should contain logger name strings or tuples with logger name string and logging level.

Command line options

–logger-logsdir=<logsdir>: where <logsdir> is root directory where log files are created

Configuration file parameters

logger_logsdir=<logsdir>: where <logsdir> is root directory where log files are created