playmolecule.jsonlogger module#
Copyright (c) 2011, Zakaria Zajac All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This library is provided to allow standard python logging to output log data as JSON formatted strings
Taken from madzak/python-json-logger
- class playmolecule.jsonlogger.JsonEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)#
Bases:
JSONEncoder
A custom encoder extending the default JSONEncoder
- default(obj)#
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- format_datetime_obj(obj)#
- class playmolecule.jsonlogger.JsonFormatter(*args, **kwargs)#
Bases:
Formatter
A custom formatter to format logging records as json strings. Extra values will be formatted as str() if not supported by json default encoder
- add_fields(log_record, record, message_dict)#
Override this method to implement custom logic for adding fields.
- format(record)#
Formats a log record and serializes to json
- jsonify_log_record(log_record)#
Returns a json string of the log record.
- parse()#
Parses format string looking for substitutions
This method is responsible for returning a list of fields (as strings) to include in all log messages.
- process_log_record(log_record)#
Override this method to implement custom logic on the possibly ordered dictionary.
- playmolecule.jsonlogger.enable_json_logger(outfile=None)#
- playmolecule.jsonlogger.merge_record_extra(record, target, reserved)#
Merges extra attributes from LogRecord object into target dictionary
- Parameters:
record – logging.LogRecord
target – dict to update
reserved – dict or list with reserved keys to skip