|
| 1 | +<h2 align="center">JSON formatter for logging</h2> |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | +<a href="https://pypi.org/project/logging_json/"><img alt="pypi version" src="https://img.shields.io/pypi/v/logging_json"></a> |
| 5 | +<a href="https://travis-ci.com/Colin-b/logging_json"><img alt="Build status" src="https://api.travis-ci.com/Colin-b/logging_json.svg?branch=master"></a> |
| 6 | +<a href="https://travis-ci.com/Colin-b/logging_json"><img alt="Coverage" src="https://img.shields.io/badge/coverage-100%25-brightgreen"></a> |
| 7 | +<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a> |
| 8 | +<a href="https://travis-ci.com/Colin-b/logging_json"><img alt="Number of tests" src="https://img.shields.io/badge/tests-0 passed-blue"></a> |
| 9 | +<a href="https://pypi.org/project/logging_json/"><img alt="Number of downloads" src="https://img.shields.io/pypi/dm/logging_json"></a> |
| 10 | +</p> |
| 11 | + |
| 12 | +This module provides a JSON formatter for the python [`logging`](https://docs.python.org/3/library/logging.html) module. |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +### Adding additional fields and values |
| 17 | + |
| 18 | +You can add fields to every message that is being logged. |
| 19 | +To do so, specify the `fields` parameter to the `logging_json.JSONFormatter` instance. |
| 20 | + |
| 21 | +It must be a dictionary where keys are the keys to be appended to the resulting JSON dictionary (if not already present) and the values can be one of the following: |
| 22 | +* An attribute of the logging record (non-exhaustive list can be found on [the python logging documentation](https://docs.python.org/3/library/logging.html#logrecord-attributes)). |
| 23 | +* If not found on the record, the value will be linked to the key. |
| 24 | + |
| 25 | +#### Logging exceptions, a specific case |
| 26 | + |
| 27 | +If an exception is loggued, the `exception` key will be appended to the resulting JSON dictionary. |
| 28 | + |
| 29 | +This dictionary will contains 3 keys: |
| 30 | +* `type`: The name of the exception class (useful when the message is blank). |
| 31 | +* `message`: The str representation of the exception (usually the provided error message). |
| 32 | +* `stack`: The stack trace, formatted as a string. |
| 33 | + |
| 34 | +### Logging with a dictionary |
| 35 | + |
| 36 | +This formatter allows you to log dictionary as in the following: |
| 37 | + |
| 38 | +```python |
| 39 | +import logging |
| 40 | + |
| 41 | +logging.info({"key": "value", "other key": "other value"}) |
| 42 | +``` |
| 43 | + |
| 44 | +The resulting JSON dictionary will be the one you provided (with the [additional fields](#adding-additional-fields-and-values)). |
| 45 | + |
| 46 | +### Logging with anything else (such as a string) |
| 47 | + |
| 48 | +Anything not logged using a dictionary will be handled by the standard formatter and it can result in one of the 2 output: |
| 49 | +* A JSON dictionary, if [additional fields](#adding-additional-fields-and-values) are set, with the message available in the `msg` key of the resulting JSON dictionary. |
| 50 | +* The formatted record, if no [additional fields](#adding-additional-fields-and-values) are set. |
| 51 | + |
| 52 | +This handles the usual string logging as in the following: |
| 53 | + |
| 54 | +```python |
| 55 | +import logging |
| 56 | + |
| 57 | +logging.info("This is my message") |
| 58 | +``` |
| 59 | + |
| 60 | +## Configuration |
| 61 | + |
| 62 | +You can create a formatter instance yourself as in the following or you can use a logging configuration. |
| 63 | + |
| 64 | +```python |
| 65 | +import logging_json |
| 66 | + |
| 67 | +formatter = logging_json.JSONFormatter(fields={ |
| 68 | + "level_name": "levelname", |
| 69 | + "thread_name": "threadName", |
| 70 | + "process_name": "processName" |
| 71 | +}) |
| 72 | +``` |
| 73 | + |
| 74 | +### Using logging.config.dictConfig |
| 75 | + |
| 76 | +You can configure your logging as advertise by python, by using the `logging.config.dictConfig` function. |
| 77 | + |
| 78 | +#### dict configuration |
| 79 | + |
| 80 | +```python |
| 81 | +import logging.config |
| 82 | +import logging_json |
| 83 | +import sys |
| 84 | + |
| 85 | +formatter = logging_json.JSONFormatter(fields={ |
| 86 | + "level_name": "levelname", |
| 87 | + "thread_name": "threadName", |
| 88 | + "process_name": "processName" |
| 89 | +}) |
| 90 | +handler = logging.StreamHandler(stream=sys.stdout) |
| 91 | +handler.setFormatter(formatter) |
| 92 | + |
| 93 | +logging.config.dictConfig({ |
| 94 | + "version": 1, |
| 95 | + "formatters": { |
| 96 | + "json": formatter |
| 97 | + }, |
| 98 | + "handlers": { |
| 99 | + "standard_output": handler, |
| 100 | + }, |
| 101 | + "loggers": { |
| 102 | + "my_app": {"level": "DEBUG"} |
| 103 | + }, |
| 104 | + "root": { |
| 105 | + "level": "INFO", |
| 106 | + "handlers": ["standard_output"] |
| 107 | + } |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +#### YAML logging configuration |
| 112 | + |
| 113 | +You can use YAML to store your logging configuration, as in the following sample: |
| 114 | + |
| 115 | +```python |
| 116 | +import logging.config |
| 117 | +import yaml |
| 118 | + |
| 119 | +with open("path/to/logging_configuration.yaml", "r") as config_file: |
| 120 | + logging.config.dictConfig(yaml.load(config_file)) |
| 121 | +``` |
| 122 | + |
| 123 | +Where `logging_configuration.yaml` can be a file containing the following sample: |
| 124 | + |
| 125 | +```yaml |
| 126 | +version: 1 |
| 127 | +formatters: |
| 128 | + json: |
| 129 | + '()': logging_json.JSONFormatter |
| 130 | + fields: |
| 131 | + level_name: levelname |
| 132 | + thread_name: threadName |
| 133 | + process_name: processName |
| 134 | +handlers: |
| 135 | + standard_output: |
| 136 | + class: logging.StreamHandler |
| 137 | + formatter: json |
| 138 | + stream: ext://sys.stdout |
| 139 | +loggers: |
| 140 | + my_app: |
| 141 | + level: DEBUG |
| 142 | +root: |
| 143 | + level: INFO |
| 144 | + handlers: [standard_output] |
| 145 | +``` |
| 146 | +
|
| 147 | +## How to install |
| 148 | +1. [python 3.6+](https://www.python.org/downloads/) must be installed |
| 149 | +2. Use pip to install module: |
| 150 | +```sh |
| 151 | +python -m pip install logging_json |
| 152 | +``` |
0 commit comments