Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cans/python-jsonlogging
A JSON log formatter (Yes, another one !)
https://github.com/cans/python-jsonlogging
Last synced: 19 days ago
JSON representation
A JSON log formatter (Yes, another one !)
- Host: GitHub
- URL: https://github.com/cans/python-jsonlogging
- Owner: cans
- License: gpl-2.0
- Created: 2019-11-24T20:25:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-25T07:36:31.000Z (about 5 years ago)
- Last Synced: 2024-11-14T20:18:30.234Z (3 months ago)
- Language: Python
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: COPYING
Awesome Lists containing this project
README
==============================
Yet another JSON log formatter
==============================A python module to emit log entries as serialized JSON objects.
Features
""""""""- the ability to rename standard LogRecord attributes in
the json output:.. code-block:: Python
:emphasize-lines: 7import logging
import jsonlogging
import syshandler = logging.StreamHandler(sys.stdout)
handler.setFormatter(jsonlogging.Formatter('%(asctime)s - %(levelname)s - %(message)s',
keymap={'asctime': 'timestamp'})
logger = logging.getLogger('some-logger')
logger.propagate = False
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)logger.debug('This is a detail')
logger.info('This is an information')
logger.warning('This is suspicious')
logger.error('This is a problem')- the ability to use the format style you prefer (prevent
format mismatches whether you want JSON, text, ...);
- structured stack traces, not just a blob of text;
- support for global extras, like an application name,
anything you would normally "hardcode" in the log format
string, e.g. ``'%(asctime)s - %(levelname)s - myapplication - $(message)s'``;
- excellent test coverage;Example YAML configuration
""""""""""""""""""""""""""Below is an example of a configuration file you can use with this library.
A configured below, JSON logs will be sent to the file ``/tmp/jsonlogging.log``Note that we use the same format for both log formatters. The JSON formatter
simply ignores the extraneous text between log record attribute names (here
``asctime``, ``level`` & ``message``).. code-block:: YAML
---
logformat: &logfmt
fmt: '%(asctime)s - %(level)s - %(message)s'
logging: # Below is a standard Python logging config
version: 1
formatters:
json:
(): jsonlogging.Formatter
<<: *logfmt
keymap:
asctime: timestamp
trunc_path: true
console:
class: logging.Formatter
<<: *logfmt
handlers:
console:
class: logging.StreamHandler
stream: ext://sys.stdout
propagate: false
syslog:
class: logging.FileHandler
path: /tmp/jsonlogging.log
propagate: false
loggers:
myapplication:
level: INFO
verboselib:
level: ERROR
root:
level: DEBUG
handlers:
- console
- syslogNote the use of the ``()`` key, in the yaml file, to specify the
formatter class to instanciate. You have to `use this syntax `_
so the configuration function will pass arguments other than ``fmt``,
``datefmt`` and ``style`` to the class constructor. If you use the
``class`` key (see below), only those 3 arguments are passed... code-block::
---
logging: # Below is a standard Python logging config
version: 1
formatters:
json:
class: jsonlogging.Formatter
fmt: '%(message)s'
datefmt: '%Y%m%d %H%M%S.%f'
# These arguments will be ignored
keymap:
asctime: timestamp
trunc_path: trueTo use the configuration file in your application, you can do as follows:
.. code-block:: Python
def main(config_path):
with open(config_path, 'r') as config_file:
config = yaml.safe_load(config_file)logging.config.dictConfig(config['logging']) # and your set !
logger = logging.getLogger('myapplication')
logger.debug('Should not appear')
logger.info('Should appear in your console and in /tmp/jsonlogging.log').. _user: https://docs.python.org/3/library/logging.config.html#user-defined-objects