pyexperiment.Logger

Provides a multiprocessing-safe logger with colored console output, rotating log files, and easy block-timing based on the logging module.

Logging made easy. Just use the module level functions debug, warning, info etc. to log messages. At some point, preferably at the start of your program, call init_main to initialize the logger and choose at which level you want to log to the console and or rotating log files.

Timing made easy. Just use the module level function timed in a ‘with’ block to log timing of the code in the block. You can also get a summary with statistics of all your timed blocks by calling print_timings.

Written by Peter Duerr, inspired by zzzeek’s and airmind’s examples on Stackoverflow (http://stackoverflow.com/a/894284/2481888, http://stackoverflow.com/a/384125/2481888).

pyexperiment.Logger.CONSOLE_FORMAT = u'[%(levelname)-19s] [%(relativeCreated)s] $BOLD%(message)s$RESET'[source]

The format used for logging to the console

pyexperiment.Logger.CONSOLE_STREAM_HANDLER = <logging.StreamHandler object at 0x7fb67c955e50>[source]

The stream handler for the console (can be mocked for testing)

class pyexperiment.Logger.ColorFormatter(msg, use_color=True)[source][source]

Formats logged messages with optional added color for the log level

format(record)[source][source]

Format the log

pyexperiment.Logger.FILE_FORMAT = u'[%(relativeCreated)10.5fs] [%(levelname)-1s] %(message)-50s (%(filename)s:%(lineno)d) %(processName)s - %(threadName)s'[source]

The format used for logging to file

pyexperiment.Logger.FILE_FORMAT_STD_MSG_LEN = 50[source]

How much space should be reserved for a normal message in the log file.

class pyexperiment.Logger.Logger(console_level=20, filename=None, file_level=10, no_backups=5)[source][source]

Implements a multiprocessing-safe logger with timing and colored console output.

close()[source][source]

Close the logger

class pyexperiment.Logger.MPRotLogHandler(filename, level=10, no_backups=0)[source][source]

Multiprocessing-safe handler for rotating log files

emit(record)[source][source]

Emits logged message by delegating it

setFormatter(formatter)[source][source]

Overload the setFormatter method

class pyexperiment.Logger.PreInitLogHandler[source][source]

Handles messages before the main logger is initialized.

emit(msg)[source][source]

Catch logs and store them for later

class pyexperiment.Logger.TimingLogger(console_level=20, filename=None, file_level=10, no_backups=5)[source][source]

Provides a logger with a timed context.

Calling code in the timed context will collect execution timing statistics.

close()[source][source]

Make sure the delegated calls are all done...

print_timings()[source][source]

Prints a summary of the timings collected with ‘timed’.

timed(*args, **kwds)[source][source]

Timer to be used in a with block. If the level is not None, logs the timing at the specified level. If save_result is True, captures the result in the timings dictionary.