Logging

Generic tools for Python logging integration.

class dockerflow.logging.JsonLogFormatter(fmt=None, datefmt=None, style='%', logger_name='Dockerflow')[source]

Log formatter that outputs machine-readable json.

This log formatter outputs JSON format messages that are compatible with Mozilla’s standard heka-based log aggregation infrastructure.

Adapted from: https://github.com/mozilla-services/mozservices/blob/master/mozsvc/util.py#L106

convert_record(record)[source]

Convert a Python LogRecord attribute into a dict that follows MozLog application logging standard.

format(record)[source]

Format a Python LogRecord into a JSON string following MozLog application logging standard.

is_value_jsonlike(value)[source]

Return True if the value looks like JSON. Use only on strings.

class dockerflow.logging.SafeJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
default(o)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

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)
dockerflow.logging.safer_format_traceback(exc_typ, exc_val, exc_tb)[source]

Format an exception traceback into safer string. We don’t want to let users write arbitrary data into our logfiles, which could happen if they e.g. managed to trigger a ValueError with a carefully-crafted payload. This function formats the traceback using “%r” for the actual exception data, which passes it through repr() so that any special chars are safely escaped.