Skip to content

j19sch/pytest-logfest

Repository files navigation

pytest-logfest

PyPI version PyPI - Downloads Python versions See Build Status on Travis CI See Build Status on AppVeyor MIT license

Pytest plugin providing three logger fixtures with basic or full writing to log files


Note: An improved rewrite of this plugin is pytest-instrument.

Features

Three logger fixtures, one of each scope: session, module and function.

Three options for writing log records to file: quiet, basic, full.

Requirements

  • Pytest
  • Pathlib2 (if using Python 2.7)

Installation

You can install "pytest-logfest" via pip from PyPI:

$ pip install pytest-logfest

Usage

Fixtures

The three logger fixtures exposed by this plugin are:

  • session_logger
  • module_logger
  • function_logger

They expose a Python Logger object, so you can use them as such, e.g. session_logger.INFO("This is a log record of level INFO.") The log nodes of these loggers match the path to the corresponding location or file.

Pytest's --log-cli-level=<level> will display these log records on stdout.

Log filenames

Writing the log records of the loggers to file can be controlled by the --logfest command-line option:

  • --logfest=quiet or option omitted: no log files are written.
  • --logfest=basic: one log file containing INFO and higher for passed tests, DEBUG and higher for setup errors or failed tests.
  • --logfest=full: in addition to the basic log file, all log records are written to a session log file and one log file per module.

Log file names and locations are as follows (directories will be created if needed):

  • basic log file in ./artifacts: session-<session timestmap>.log
  • session-level full log file in ./artifacts: <request.node.name | logfest-root-node>-<session timestmap>.log
  • module-level full log file in ./artifacts/<path-to-module>: <module_name>-<session timestmap>.log

logfest-root-node can be set in pytest.ini (see below). You can change the compostion of file names through hooks (see below).

pytest.ini

The following values in pytest.ini are relevant to this plugin:

  • logfest-root-node: name used as root log node and in log filenames; if not set, defaults to the session's request.node.name.
  • log-level: should be set to info or lower, so pytest captures all relevant log records.
  • log-format: the default format is not very convenient in combination with this plugin, suggestion: %(name)s - %(levelname)s - %(message)s

Hooks

There are three hooks to change the components of the log filenames:

  • pytest_logfest_log_file_name_basic
  • pytest_logfest_log_file_name_full_session
  • pytest_logfest_log_file_name_full_module

The expose a list that will be joined with the separator character - and appended with .log.

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure good test coverage before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-logfest" is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Acknowledgements

This pytest plugin was generated with Cookiecutter along with @hackebrot's cookiecutter-pytest-plugin template.

Thanks to my employer Mendix, for the crafting days in which I worked on this plugin, and for the permission to open-source it.