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": "2020-01-10T00:06:24.855159", "level": "INFO", "message": "this is info."}
{ "timestamp": "2020-01-10T00:06:24.855159", "level": "WARNING", "message": "this is warn."}
{ "timestamp": "2020-01-10T00:06:24.855159", "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: WARNING.
- 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.warn('this is warn.', { 'warnData': 'this is a extra payload for warning.'})
logging.error('this is error.', { 'errData': 'this is a extra payload for error.'})
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": "WARNING", "@message": "this is warn"}
{"errData": "this is a extra payload for error.", "@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.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8588ceb231e01647c93c9e0cdac8916914b688940f37b7685a78b8a249a912d |
|
MD5 | f4a2bc576fa1a6afa42aafad582c3457 |
|
BLAKE2b-256 | 117f588d99ed36c27b52670049ca36d39bb61932d7890d5234c5b333cf0c0696 |