logthing

Some useful tools for logging. First of is the SRotHandler, a rotating file handler with a size limit. Second is a series of utilities for configuring your logging

Installation

$ pip install logthing

Supports

logthing has been tested with Python 3.6, 3.7, and 3.8

For Python 2 compatible source use version 0.10.1

Log Handler

class logthing.srothandler.NullLogRecord
__init__()

Initialize a logging record with interesting information.

class logthing.srothandler.SizeRotatingFileHandler(filename, mode='a', maxBytes=0, encoding=None, debug=True, delay=0)

Rotating file handler that handles concurrent access without losing logs. When a size limit is hit the log is rotated, with the old log renamed based on timestamp.

Depending on the platform, the size limit may not be completely respected if several processes have it open simultaneously as it may need to wait for a process to let go.

This handler is based on the version 0.9.1 of the ConcurrentLogHandler by Lowell Alleman and with the exception of the removal of the “backup count” feature and how the files are renamed is a direct copy, the same caveats apply.

__init__(filename, mode='a', maxBytes=0, encoding=None, debug=True, delay=0)

Use the specified filename for streamed logging

acquire()

Acquire the I/O thread lock.

close()

Close log stream and stream_lock.

release()

Release the I/O thread lock.

Log Utils

logthing.utils.LOG_FORMAT_ESCAPED = '\x1b[1m%(funcName)s\x1b[0m: %(message)s'

Log formatting constant that uses vt100 escape sequences to produce highlighted function names and displays the message

logthing.utils.LOG_FORMAT_STANDARD = '%(asctime)s %(name)s.%(funcName)s: pid:tid=%(process)d:%(thread)d %(message)s'

Typical log formatting contsant shows date/time, function info, pid/tid and message

logthing.utils.configure_file_logger(name, log_dir, log_level=10)

Shortcut method that sets up logging to use the SizeRotatingFileHandler with the LOG_FORMAT_STANDARD display.

logthing.utils.configure_stdout_logger(log_level=10)

Shortcut method that sets up logging to use STDOUT and with the LOG_FORMAT_ESCAPED configuration.

logthing.utils.default_logging_dict(log_dir, handlers=['file'], filename='debug.log')

Returns a logging configuration dictionary with reasonable defaults. Defines two handlers: “default” goes to STDOUT using the LOG_FORMAT_ESCAPED configuration and “file” uses a SizeRotatingFileHandler with the LOG_FORMAT_STANDARDD configuration. Only the “file” hanlder is on by default.

Parameters
  • log_dir – Directory for logs to go into.

  • handlers – Which logging handlers to use. Defaults to only ‘file’

  • filename – Base name of the file to log to. Defaults to “debug.log”.

logthing.utils.silence_logging(method)

Context wrapper that on entry disables logging and turns it back on upon exit.

Often useful when testing if a test method is supposed to issue an error message which is confusing that the error shows for a successful test.

Indices and tables