Zack Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 1 | multistructlog |
| 2 | ============== |
| 3 | |
| 4 | This module is a thin wrapper around Structlog that sets and provides defaults |
| 5 | for sending logs to one or more logging destinations with individual formatting |
| 6 | per destination. |
| 7 | |
| 8 | The API consists of a single function: ``create_logger()``. |
| 9 | |
| 10 | Args: |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 11 | logging_config (dict): Input to logging.config.dictConfig |
| 12 | |
| 13 | level(logging.loglevel): Overrides logging level for all loggers (not handlers!) |
Zack Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 14 | |
| 15 | Returns: |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 16 | log: structlog Logger object |
Zack Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 17 | |
| 18 | It can be invoked as follows: |
| 19 | |
| 20 | logging_config = ... |
| 21 | |
| 22 | log = multistructlog.create_logger(config, level=logging.INFO) |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 23 | |
Zack Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 24 | log.info('Entered function', foo='bar') |
| 25 | |
| 26 | To create a ``logging_config`` dictionary, see these docs: |
| 27 | |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 28 | - 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 Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 30 | |
| 31 | There are no required arguments to `create_logger()` - any missing parts of the |
| 32 | config will be filled in with defaults that print structured logs to the |
| 33 | console. |
| 34 | |
| 35 | If you don't specify a ``formatters`` section in your config, three will be |
| 36 | created which can be used in handlers: |
| 37 | |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 38 | - ``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 Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 41 | |
| 42 | If you don't specify a ``handlers`` section, a handler will be added that logs |
| 43 | to console with ``logging.StreamHandler`` with format ``structured`` at level |
| 44 | ``DEBUG``. |
| 45 | |
| 46 | If you don't specify a ``loggers`` section, a default logger (empty string) |
| 47 | will be created with all items in ``handlers`` added to it, with a level of |
| 48 | ``NOTSET`` (every level printed). |
| 49 | |
| 50 | When setting log level, the higher of ``logging_config['loggers'][*]['level']`` |
| 51 | and ``logging_config['handlers'][*]['level']`` is used. The ``level`` parameter |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 52 | overrides the ``loggers`` value of level, not the ``handlers`` level. |
Zack Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 53 | |
| 54 | If the handler's level is set to ``DEBUG`` but the logger's level is set to |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 55 | ``ERROR``, the handler will be overridden and only log ``ERROR`` level messages. |
Zack Williams | 4113213 | 2018-10-19 12:14:45 -0700 | [diff] [blame] | 56 | |
| 57 | Multistructlog also adds a ``TRACE`` log level (integer level 5) that is below |
| 58 | "DEBUG" to both standard library ``Logger`` and Structlog ``BoundLogger``. |
| 59 | |
| 60 | List of standard logging levels: |
| 61 | https://docs.python.org/2.7/library/logging.html#logging-levels |
Zack Williams | 940ff79 | 2018-11-02 11:11:20 -0700 | [diff] [blame] | 62 | |
| 63 | Changelog |
| 64 | --------- |
| 65 | 1.x versions |
| 66 | |
| 67 | - legacy |
| 68 | |
| 69 | 2.0.0 |
| 70 | |
| 71 | - Substantial refactor/rewrite |
| 72 | - Cache logger objects |
| 73 | - Add trace levels |
| 74 | |
| 75 | 2.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 |