A simple logging in JSON
Project description
plain-json-logging is a simple logging for a JSON data.
Installation:
$ pip install plain-json-logging
Usage:
from plain_json_logging import PlainJsonLogging
logging = new PlainJsonLogging()
logging.info('this is info.')
logging.warn('this is warn.')
logging.error('this is error.)
The result is found in stderr like this:
{ "timestamp": , "level": "INFO", "message": "this is info."}
{ "timestamp": , "level": "WARNNING", "message": "this is warn."}
{ "timestamp": , "level": "ERROR", "message": "this is error."}
Options:
The following parameters can modify how the logging behaves:
- file:
the f for print(‘output to file’, file=f). Default: stderr.
- strftime:
the format. Defalut: datetime.datetime default.
- timedelta:
the timedelta. The minute is a unit.
- timestampname:
the name for timestamp. Default: timestamp.
- levelname:
the name for level. Default: level.
- messagename:
the name for message. Default: message.
- infoname:
the name for info. Default: INFO.
- warnname:
the name for warn. Default: WARNNING.
- errorname:
the name for error. Default: ERROR.
For example, AWS CloudWatch supports the discovered fields. Therefore, if timestamp name and messagename are @timestamp and @message, CloudWatch can discovere those fields. And more, .info(), .warn() and .error() methods can receives extra payload.
from plain_json_logging import PlainJsonLogging
logging = new PlainJsonLogging(
file=sys.stdout,
strftime='%Y-%m-%d %H:%M:%S.%f%z',
timedelta=+540, # 9 hours
timestampname='@timestamp',
levelname='level',
messagename='@message',
)
logging.info('this is info.', { 'infoData': 'this is a extra payload for info.'})
logging.warning('this is warn.', { 'warnData': 'this is a extra payload for warning.'})
logging.error('this is error.', { 'errData': 'this is a extra payload for warning.'})
The result is found in stdout like this:
{"infoData": "this is a extra payload for info.", "@timestamp": "2020-01-10 05:31:20.151462", "level": "INFO", "@message": "this is info"}
{"warnData": "this is a extra payload for warning.", "@timestamp": "2020-01-10 05:31:20.151462", "level": "WARNNING", "@message": "this is warn"}
{"errData": "this is a extra payload for warning.", "@timestamp": "2020-01-10 05:31:20.151462", "level": "ERROR", "@message": "this is error"}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for plain_json_logging-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b5644a8a98d93f102ea17cf00e819031d966d500f1759712f408553d052b252 |
|
MD5 | 5876f2f7399378733612b05bacca4c76 |
|
BLAKE2b-256 | b2b0f32130da58cf4ccd070a7a9055a9ce14ce232f8575c3c36179f98aab3792 |