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.error('this is error.)
logging.warn('this is warn.')
logging.info(
'foo'
).info(
'bar'
).info(
'baz'
)
The result is found in stderr like this:
{ "timestamp": "2020-01-10T00:06:24.855159", "level": "ERROR", "message": "this is error."}
{ "timestamp": "2020-01-10T00:06:24.855159", "level": "WARN", "message": "this is warn."}
{ "timestamp": "2020-01-10T00:06:24.855159", "level": "INFO", "message": "foo"}
{ "timestamp": "2020-01-10T00:06:24.855159", "level": "INFO", "message": "bar"}
{ "timestamp": "2020-01-10T00:06:24.855159", "level": "INFO", "message": "baz"}
Options:
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',
messagename='@message',
levelname='lev',
levelinfo=0,
levelwarn=1,
levelerror=2,
)
The following parameters can modify how the logging behaves:
- file:
the writable file object. Default: sys.stderr.
- strftime:
the format. Defalut: datetime.datetime default.
- timedelta:
the timedelta. The minute is a unit. Default: 0.
- timestampname:
the name for timestamp. Default: timestamp.
- messagename:
the name for message. Default: message.
- levelname:
the name for level. Default: level.
- levelinfo:
the value for the level info. Default: INFO.
- levelwarn:
the value for the level warn. Default: WARN.
- levelerror:
the value for the level error. Default: ERROR.
API:
PlainJsonLogging has the following three methods:
info
warn
error
All the methods returns the PlainJsonLogging instance itself. Therefore, method chaining can be used for logging:
from plain_json_logging import PlainJsonLogging
logging = new PlainJsonLogging()
logging.info(
'foo'
).info(
'bar'
).info(
'baz'
)
Extra Payload:
All the 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',
messagename='@message',
levelname='lev',
levelinfo=0,
levelwarn=1,
levelerror=2,
)
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 warn.'})
logging.error('this is error.', { 'errData': 'this is a extra payload for error.'})
The result is found in stdout like this:
{"@timestamp": "2020-01-13 07:17:06.370000", "lev": 0, "@message": "this is info.", "infoData": "this is a extra payload for info."}
{"@timestamp": "2020-01-13 07:17:06.370000", "lev": 1, "@message": "this is warn.", "warnData": "this is a extra payload for warn."}
{"@timestamp": "2020-01-13 07:17:06.370000", "lev": 2, "@message": "this is error.", "errData": "this is a extra payload for 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 Distributions
Hashes for plain_json_logging-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27f75bb88055ece20e7e63ec7f61d85eaa1c7e2afbda0e62fc9e23698f604799 |
|
MD5 | 8e177844a6ea4bfe5cba7a0bc406dcf5 |
|
BLAKE2b-256 | 370f6307fd88318e5a83e57c9df95187a75f2f165fb06e4cee615bdbff0cb346 |
Hashes for plain_json_logging-0.2.0-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8c9d9814d8df8c40593e784e3db95874e1071f115e87246ecf3a156a958fdcb |
|
MD5 | 4a0b3e98bc9f928d446cb25a9537beb9 |
|
BLAKE2b-256 | f8481b631ce6bdbc96170e86b1f516a9f1c2f88e243dde6f785c7121eeedb367 |