blob: d0bdce6be9765446d170662f64f798fce69f7ad6 [file] [log] [blame]
Zack Williams41132132018-10-19 12:14:45 -07001multistructlog
2==============
3
4This module is a thin wrapper around Structlog that sets and provides defaults
5for sending logs to one or more logging destinations with individual formatting
6per destination.
7
8The API consists of a single function: ``create_logger()``.
9
10Args:
Zack Williams940ff792018-11-02 11:11:20 -070011 logging_config (dict): Input to logging.config.dictConfig
12
13 level(logging.loglevel): Overrides logging level for all loggers (not handlers!)
Zack Williams41132132018-10-19 12:14:45 -070014
15Returns:
Zack Williams940ff792018-11-02 11:11:20 -070016 log: structlog Logger object
Zack Williams41132132018-10-19 12:14:45 -070017
18It can be invoked as follows:
19
20 logging_config = ...
21
22 log = multistructlog.create_logger(config, level=logging.INFO)
Zack Williams940ff792018-11-02 11:11:20 -070023
Zack Williams41132132018-10-19 12:14:45 -070024 log.info('Entered function', foo='bar')
25
26To create a ``logging_config`` dictionary, see these docs:
27
Zack Williams940ff792018-11-02 11:11:20 -070028- https://docs.python.org/2.7/library/logging.config.html#logging.config.dictConfig
29- http://www.structlog.org/en/stable/standard-library.html#rendering-using-structlog-based-formatters-within-logging
Zack Williams41132132018-10-19 12:14:45 -070030
31There are no required arguments to `create_logger()` - any missing parts of the
32config will be filled in with defaults that print structured logs to the
33console.
34
35If you don't specify a ``formatters`` section in your config, three will be
36created which can be used in handlers:
37
Zack Williams940ff792018-11-02 11:11:20 -070038- ``json``: renders one JSON dictionary per message
39- ``structured``: prints structured logs with the ``structlog.dev.ConsoleRenderer``
40- ``structured-color``: same as ``structured`` but with forced color output
Zack Williams41132132018-10-19 12:14:45 -070041
42If you don't specify a ``handlers`` section, a handler will be added that logs
43to console with ``logging.StreamHandler`` with format ``structured`` at level
44``DEBUG``.
45
46If you don't specify a ``loggers`` section, a default logger (empty string)
47will be created with all items in ``handlers`` added to it, with a level of
48``NOTSET`` (every level printed).
49
50When setting log level, the higher of ``logging_config['loggers'][*]['level']``
51and ``logging_config['handlers'][*]['level']`` is used. The ``level`` parameter
Zack Williams940ff792018-11-02 11:11:20 -070052overrides the ``loggers`` value of level, not the ``handlers`` level.
Zack Williams41132132018-10-19 12:14:45 -070053
54If the handler's level is set to ``DEBUG`` but the logger's level is set to
Zack Williams940ff792018-11-02 11:11:20 -070055``ERROR``, the handler will be overridden and only log ``ERROR`` level messages.
Zack Williams41132132018-10-19 12:14:45 -070056
57Multistructlog also adds a ``TRACE`` log level (integer level 5) that is below
58"DEBUG" to both standard library ``Logger`` and Structlog ``BoundLogger``.
59
60List of standard logging levels:
61 https://docs.python.org/2.7/library/logging.html#logging-levels
Zack Williams940ff792018-11-02 11:11:20 -070062
63Changelog
64---------
651.x versions
66
67- legacy
68
692.0.0
70
71- Substantial refactor/rewrite
72- Cache logger objects
73- Add trace levels
74
752.1.0
76
77- Force color on when using structured-color formatter
78- Print timestamp and loglevel by default
79- Fix issues with reinitialziaton of logger