Last active
April 5, 2018 14:10
-
-
Save mazhar-ansari-ardeh/75cbb52fd3b33667b5eff9d3f538ca24 to your computer and use it in GitHub Desktop.
A simple program that shows usage of the jsonlogger package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import pythonjsonlogger.jsonlogger | |
import datetime | |
def testJsonFormatter(): | |
supported_keys = [ | |
'asctime', | |
'created', | |
'filename', | |
'funcName', | |
'levelname', | |
'levelno', | |
'lineno', | |
'module', | |
'msecs', | |
'message', | |
'name', | |
'pathname', | |
'process', | |
'processName', | |
'relativeCreated', | |
'thread', | |
'threadName' | |
] | |
log_format = lambda x: ['%({0:s})'.format(i) for i in x] | |
custom_format = ' '.join(log_format(supported_keys)) | |
fr = jsonlogger.JsonFormatter(custom_format) | |
logHandler.setFormatter(fr) | |
# msg = "testing logging format" # This also works. | |
# In the following message, default json encoder will be used for encoding datetime objects | |
msg = { "message": "You've got message", | |
"text": "testing logging", "num": 1, 5: "9", | |
"adate": datetime.datetime(1999, 12, 31, 23, 59), | |
"otherdate": datetime.date(1789, 7, 14), | |
"otherdatetime": datetime.datetime(1789, 7, 14, 23, 59), | |
"otherdatetimeagain": datetime.datetime(1900, 1, 1), | |
"nested": {"more": "data"} | |
} | |
extra = {"text": "testing logging with an extra", "num": 1, 5: "five", "nested": {"some more": "data"}} | |
logger.info(msg, extra = extra) | |
# Testing with unknown format key: | |
fr = jsonlogger.JsonFormatter('%(unknown_key)s %(message)s') | |
logHandler.setFormatter(fr) | |
msg = "testing unknown logging format" | |
logger.info(msg) | |
# The log output will be: {"unknown_key": null, "message": "testing unknown logging format"} | |
try: | |
raise Exception('test') | |
except Exception: | |
logger.exception("hello") | |
# Using a custom encoder for encoding non-standard objects into json | |
def custom(o): | |
return "very custom" | |
fr = jsonlogger.JsonFormatter(json_default=custom) | |
logHandler.setFormatter(fr) | |
# In the following message, the 'custom' function above will be called for the 'adate' field. | |
msg = {"message": "The message", | |
"adate": datetime.datetime(1999, 12, 31, 23, 59), | |
"normal": "value", | |
"int":5, | |
"list": [1, 2]} | |
logger.info(msg) | |
# Custom logic: Adding fields to the output message. | |
class CustomJsonFormatter(jsonlogger.JsonFormatter): | |
def process_log_record(self, log_record): | |
log_record["custom"] = "a custom value" | |
# Old Style "super" since Python 2.6's logging.Formatter is old | |
# style | |
return jsonlogger.JsonFormatter.process_log_record(self, log_record) | |
logHandler.setFormatter(CustomJsonFormatter()) | |
logger.info("A message with a custom json formatter") | |
logHandler.setFormatter(jsonlogger.JsonFormatter(prefix='{')) | |
logger.info("A prefixed message") | |
if __name__ == '__main__': | |
logger = logging.getLogger('logging-test') | |
logger.setLevel(logging.DEBUG) | |
logHandler = logging.FileHandler("log.json") | |
logger.addHandler(logHandler) | |
testFormatKeys() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jsonlogger
allows standard python logging mechanism to output log data as json objects. With JSON logs will be more readable by machines.The package is hosted on GitHub: https://github.com/madzak/python-json-logger/ .