A JSON logger for map colonies project
Project description
json-logger
Dependencies
install python-json-logger
pip3 install python-json-logger
Usage Example
NOTICE: the example assumes that logger.py is in the same directory as your calling module, else a different import method should be used
import os, sys
from logger import JSONLogger
# logging config yaml file path
LOG_CONFIG_PATH = os.environ.get('LOG_CONFIG_PATH', 'log.yaml')
APP_NAME = 'my-service'
# there should be a formatter that uses these fields in configuration yaml (e.g. format: '%(timestamp)s %(service)s')
additional_constant_fields = {'service': APP_NAME}
# NOTICE: there should be a matching logger name in configuration yaml (e.g. main-debug)
log = JSONLogger('main-info', LOG_CONFIG_PATH, additional_constant_fields).get_logger()
log.critical('system crashed')
log.error('incorrect input')
log.warn('not ready')
log.info('some info', extra={'action_id': 3}) # extra fields
log.debug('some info', extra={'action_id': 4}) # will not be logged because logger level set to INFO ('main-info')
Example Configuration YAML
version: 1
formatters:
brief:
format: '%(message)s'
json:
format: '%(timestamp)s %(service)s %(loglevel)s %(message)s'
class: logger.CustomJsonFormatter
handlers:
console:
class: logging.StreamHandler
formatter: json
stream: ext://sys.stdout
file:
class : logging.handlers.RotatingFileHandler
formatter: json
filename: logfile.log # log files full path
maxBytes: 5242880 # 5 MB
backupCount: 10
loggers:
main-debug:
handlers: [console, file]
level: DEBUG
main-info:
handlers: [console, file]
level: INFO
main-warning:
handlers: [console, file]
level: WARNING
main-error:
handlers: [console, file]
level: ERROR
main-critical:
handlers: [console, file]
level: CRITICAL
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for MapColoniesJSONLogger-0.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0d1d41ab07ac069822999e56af027c9492ea747e9fa14648d5dc00ce7281445 |
|
MD5 | 55a2037698f23dfc7c30192a0340de28 |
|
BLAKE2b-256 | b5a2a3006dd54a6f3ae702a3d5214cbf799a02eea4c76bfc7a665bb62126ab6a |
Close
Hashes for MapColoniesJSONLogger-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d08e52692faf3e3f50088688cbab0467136d510c3c16e77cf48fcd1473093151 |
|
MD5 | 74fd7b8ec28865e1c4f00782b63f11f4 |
|
BLAKE2b-256 | f5ea206688f093384651a38c571f07b826696c83f5e8dab0496a21f17aad74a8 |