pyzlog package

Module contents

Provides a drop in solution for logging json messages in a standard format. An example of what a json log entry will look like:

{
    "server_hostname": "localhost",
    "event_timestamp": "2015-10-31T01:01:01.42Z",
    "event_name": "foo.event",
    "log_level": "INFO",
    "application_name": "fizzbuzz",
    "fields": {
        "custom-field": 42,
        "other-custom-thing": "fizzle"
    }
}

server_hostname, event_timestamp, event_name, log_level, application_name, and fields will be present in every log entry.

class pyzlog.JsonFormatter(fmt=None, json_default=<function _default_json_default>, application_name='default', server_hostname=None, fields=None)[source]

Bases: logging.Formatter

JsonFormatter is used internally by the pyzlog package; you should never have to instantiate this yourself. Takes a logging.LogRecord and formats it into a standardized json format.

Parameters:
  • fmt (string) – passed to logging.Formatter
  • datefmt (string) – format for timestamp field. passed to logging.Formatter
  • application_name (string) – app name to add to each log entry
  • server_hostname (string) – hostname to add to each log entry
  • fields (dict) – whitelist of allowed fields for each log entry
format(record)[source]

formats a logging.Record into a standard json log entry

Parameters:record (logging.Record) – record to be formatted
Returns:the formatted json string
Return type:string
resolve_hostname(server_hostname=None)[source]
class pyzlog.LogTest[source]

Bases: object

Utility class to help testing applications that rely on pyzlog.

Intended to be a mixin for your test classes. If you don’t pass path and target to all the helper methods, be sure to set path and target properties on your class.

get_log_messages(path=None, target=None)[source]

fetch all log entries in the given file

Intended to be used to assert that the expected entries were written out to the correct log file. If path or target are not specified, will default to path and target properties on the object.

Parameters:
  • path (string) – path to find the log file
  • target (string) – name of the log file
init_logs(path=None, target=None, level=None, server_hostname=None, extra=None)[source]

Simple canned way to initialize pyzlog.

Initialize pyslog for tests. If path or target are not specified, will default to path and target properties on the object. leve will default to logging.DEBUG, server_hostname defaults to localhost, and extra defaults to {‘extra’: None}

Parameters:
  • path (string) – path to find the log file
  • target (string) – name of the log file
  • level (int) – log level for this instance
  • server_hostname (string) – hostname to put in each entry
  • extra (dict) – whitelist/defaults of extra fields to add to each entry
remove_log(path=None, target=None)[source]

remove the specified log file.

Generally called in setUp and tearDown methods to ensure isolation. If path or target are not specified, will default to path and target properties on the object.

Parameters:
  • path (string) – path to find the log file
  • target (string) – name of the log file
pyzlog.alert(*args, **kwargs)[source]

log with pyzlog level ALERT

pyzlog.critical(*args, **kwargs)[source]

log with pyzlog level CRITICAL

pyzlog.debug(*args, **kwargs)[source]

log with pyzlog level DEBUG

pyzlog.emergency(*args, **kwargs)[source]

log with pyzlog level EMERGENCY

pyzlog.error(*args, **kwargs)[source]

log with pyzlog level ERROR

exception info is added if it exists

pyzlog.info(*args, **kwargs)[source]

log with pyzlog level INFO

pyzlog.init_logs(path=None, target=None, logger_name='root', level=10, maxBytes=1048576, backupCount=5, application_name='default', server_hostname=None, fields=None)[source]

Initialize the zlogger.

Sets up a rotating file handler to the specified path and file with the given size and backup count limits, sets the default application_name, server_hostname, and default/whitelist fields.

Parameters:
  • path (string) – path to write the log file
  • target (string) – name of the log file
  • logger_name (string) – name of the logger (defaults to root)
  • level (int) – log level for this logger (defaults to logging.DEBUG)
  • maxBytes (int) – size of the file before rotation (default 1MB)
  • application_name (string) – app name to add to each log entry
  • server_hostname (string) – hostname to add to each log entry
  • fields (dict) – default/whitelist fields.
pyzlog.level_map = {'info': ('info', 'INFO'), 'notice': ('info', 'NOTICE'), 'warning': ('warning', 'WARNING'), 'critical': ('critical', 'CRITICAL'), 'emergency': ('critical', 'EMERGENCY'), 'error': ('error', 'ERROR'), 'debug': ('debug', 'DEBUG'), 'alert': ('info', 'ALERT')}

check out the level_map

pyzlog.notice(*args, **kwargs)[source]

log with pyzlog level NOTICE

pyzlog.warning(*args, **kwargs)[source]

log with pyzlog level WARNING