Package: logging-json
JSON formatter for loggingThis module provides a JSON formatter for the pythonloggingmodule that will format to JSON formatted string.Using this formatter allows to have the proper format for logging toSplunkorElasticSearch, but it can also be used for logging to stdout as a string is issued.FeaturesCustom fieldsdict loggingstr loggingConfigurationUsing dictConfigFeaturesAdding additional fields and valuesYou can add fields to every message that is being logged.
To do so, specify thefieldsparameter to thelogging_json.JSONFormatterinstance.It must be a dictionary where keys are the keys to be appended to the resulting JSON dictionary (if not already present) and the values can be one of the following:An attribute of the logging record (non-exhaustive list can be found onthe python logging documentation).If not found on the record, the value will be linked to the key.Logging exceptions, a specific caseIf an exception is logged, theexceptionkey will be appended to the resulting JSON dictionary.This dictionary will contain 3 keys:type: The name of the exception class (useful when the message is blank).message: The str representation of the exception (usually the provided error message).stack: The stack trace, formatted as a string.You can rename the exception field key by setting theexception_field_nameparameter with a new name for the key.
It is also possible to disable this behaviour by setting theexception_field_nameparameter toNoneor an empty stringLogging with a dictionaryThis formatter allows you to log dictionary as in the following:importlogginglogging.info({"key":"value","other key":"other value"})The resulting JSON dictionary will be the one you provided (with theadditional fields).Logging with anything else (such as a string)Anything not logged using a dictionary will be handled by the standard formatter, and it can result in one of the 2 output:A JSON dictionary, ifadditional fieldsare set or ifextraparameter is used while logging, with the message available in themessagekey of the resulting JSON dictionary.
Defaultmessagekey name can be changed bymessage_field_nameparameter of thelogging_json.JSONFormatterinstance.The formatted record, if noadditional fieldsare set.This handles the usual string logging as in the following:importlogginglogging.info("This is my message")Changing asctime representationYou can override the default representation of asctime (2003-07-08 16:49:45,896) based on two different scenarii:Without millisecondsSetdatefmtparameter.Settingdatefmtto%Y-%m-%dT%H:%M:%Swould result in2003-07-08T16:49:45.With millisecondsSetdefault_time_formatto something else than%Y-%m-%d %H:%M:%Sto change the representation part without milliseconds.
Setdefault_msec_formatto something else than%s,%03dto change the representation milliseconds.
Note that%sindefault_msec_formatis going to be replaced by the representation without milliseconds.Settingdefault_time_formatto%Y-%m-%dT%H:%M:%Sanddefault_msec_formatto%s.%03dwould result in2003-07-08T16:49:45.896.ConfigurationYou can create a formatter instance yourself as in the following, or you can use a logging configuration.importlogging_jsonformatter=logging_json.JSONFormatter(fields={"level_name":"levelname","thread_name":"threadName","process_name":"processName"})Using logging.config.dictConfigYou can configure your logging as advertise by python, by using thelogging.config.dictConfigfunction.dict configurationimportlogging.configlogging.config.dictConfig({"version":1,"formatters":{"json":{'()':'logging_json.JSONFormatter','fields':{"level_name":"levelname","thread_name":"threadName","process_name":"processName"}}},"handlers":{"standard_output":{'class':'logging.StreamHandler','formatter':'json','stream':'ext://sys.stdout'},},"loggers":{"my_app":{"level":"DEBUG"}},"root":{"level":"INFO","handlers":["standard_output"]}})YAML logging configurationYou can use YAML to store your logging configuration, as in the following sample:importlogging.configimportyamlwithopen("path/to/logging_configuration.yaml","r")asconfig_file:logging.config.dictConfig(yaml.load(config_file))Wherelogging_configuration.yamlcan be a file containing the following sample:version:1formatters:json:'()':logging_json.JSONFormatterfields:level_name:levelnamethread_name:threadNameprocess_name:processNamehandlers:standard_output:class:logging.StreamHandlerformatter:jsonstream:ext://sys.stdoutloggers:my_app:level:DEBUGroot:level:INFOhandlers:[standard_output]How to installpython 3.7+must be installedUse pip to install module:python-mpipinstalllogging_json