logjson
Project description
logjson
Goal: easily generate structured JSON logging. logstash mode is optional.
import logging
import logjson
logger = logging.getLogger('blah')
handler = logging.StreamHandler()
handler.setFormatter(
logjson.JSONFormatter(pretty=True)
)
logger.addHandler(handler)
logger.info('hi %s %s!', 'you', 'there')
Output:
{
"name": "blah",
"msg": "hi %s %s!",
"args": [
"you",
"there"
],
"levelname": "INFO",
"levelno": 20,
"pathname": "<snip>",
"filename": "test_main.py",
"module": "test_main",
"exc_text": null,
"stack_info": null,
"lineno": 17,
"funcName": "test_main",
"created": 1511750128.6285746,
"msecs": 628.5746097564697,
"relativeCreated": 23.08201789855957,
"thread": 139929130264384,
"threadName": "MainThread",
"processName": "MainProcess",
"process": 18460,
"message": "hi you there!",
"created_iso": "2017-11-27T02:35:28.628575+00:00"
}
Logstash mode is only one param away:
logger = logging.getLogger('ls')
handler = logging.StreamHandler()
handler.setFormatter(
logjson.JSONFormatter(pretty=True, logstash_mode=True)
)
logger.addHandler(handler)
logger.info('logstash test')
Output:
{
"@message": "logstash test",
"@source_host": "localhost.localdomain",
"@timestamp": "2017-11-27T02:35:28.631275+00:00",
"@fields": {
"name": "ls",
"msg": "logstash test",
"args": [],
"levelname": "INFO",
"levelno": 20,
"pathname": "<snip>",
"filename": "test_main.py",
"module": "test_main",
"exc_text": null,
"stack_info": null,
"lineno": 42,
"funcName": "test_logstash",
"created": 1511750128.631275,
"msecs": 631.274938583374,
"relativeCreated": 25.782346725463867,
"thread": 139929130264384,
"threadName": "MainThread",
"processName": "MainProcess",
"process": 18460
}
}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
logjson-2018.5.1.tar.gz
(8.5 kB
view hashes)
Built Distribution
Close
Hashes for logjson-2018.5.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 952e25295744fbd971dc0dee56fef4ac9c65975df118428a8f68b6ab9236a84c |
|
MD5 | 9a126b20d470bb8ea16bada85272841f |
|
BLAKE2b-256 | 4ad2760403b53354556cd885513c4cbf910ff7d6b6b937d3d854ec3f8a8c2838 |