Skip to content

psr.log

PSR-3 (php standard recommendation) port for python 3.5+


This repository holds interfaces/classes related to PSR-3.

Note that this is not a logger of its own. It is merely an interface that describes a logger. See the specification for more details.

Architecture overview

Architecture overview

Notice

Since 2.x version psr.log does not supports PSR-3 and has not backward compatible code entities changes (see changelog for details).

If you would like to use contract definitions closest to PSR-3 you have to switch to 1.x version

Installation

pip install psr.log --index-url=https://source.md.land/python/

Usage

If you need a logger, you can use the interface like this:

import psr.log


class Foo:
    def __init__(self, logger: psr.log.LoggerInterface = None) -> None:
        self._logger = logger

    def do_something(self) -> None:
        if self._logger:
            self._logger.info('Doing work')

        try:
            self.do_something_else()
        except Exception as exception:
            if self._logger:
                self._logger.error('Oh no!', {'exception': exception})
            raise
        # do something useful

You can then pick one of the implementations of the interface to get a logger.

If you want to implement the interface, you can require this package and implement psr.log.LoggerInterface in your code. Please read the specification text for details.


Take a look for md.log — the first implementation.