brennivin.logutils module

Useful stuff for working with the stdlib’s logging module.

See the Fmt class for commonly used format strings/formatters.

If you need a NullHandler, use the one from this package to add Python 2.6 compatibility.

There are various functions for working with timestamped log filenames (getting the timestamped name, cleaning up old versions, etc).

Use get_filenames_from_loggers() to get all the logging filenames currently registered.

Members

class brennivin.logutils.Fmt

Commonly used format strings and formatters. Formatter instances begin with ‘FMT’.

Attributes should be strings of letters that describe the format:

  • N: name
  • T: asctime
  • L: levelname
  • M: message
class brennivin.logutils.MultiLineIndentFormatter(fmt=None, datefmt=None, sep=' ')

Indents every newline character in a formatted logrecord to have the same indentation as the formatted record’s header.

brennivin.logutils.get_filenames_from_loggers(loggers=None, _loggingmodule=None)

Get the filenames of all log files from loggers. If not supplied, use all loggers from logging module.

brennivin.logutils.get_timestamped_logfilename(folder, basename=None, ext='.log', fmt='%Y-%m-%d-%H-%M-%S', timestruct=None, _getpid=<built-in function getpid>)

Using default keyword arguments return filename <folder>/<basename>_<timestamp>_<pid>.log in the app’s folder in ccptechart prefs.

Parameters:
  • folder – Folder to put file into.
  • basename – The prefix of the log filename. If None, use os.path.basename(folder).
brennivin.logutils.remove_old_files(root, namepattern='*', maxfiles=1)

Removes the oldest files that match namePattern inside of rootDir, so that only maxfiles of those matches remain.

Parameters:maxfiles – Number of files to keep. If 0, remove all files.
brennivin.logutils.timestamp(fmt, timestruct=None)

Return timestamp by calling time.strftime(fmt, timestruct()).

Parameters:
brennivin.logutils.timestamped_filename(filename, fmt='%Y-%m-%d-%H-%M-%S', timestruct=None, sep='_')

Given a filename, return a new filename ‘{head}_{formatted timestruct}.{ext}’.

Parameters:
  • filename – The filename.
  • fmt – The format string.
  • timestruct – A named tuple instance such as from time.localtime(). Defaults to time.gmtime().
  • sep – Separator between the filename and the time.
>>> timestamped_filename(r'C:lah.log', timestruct=(2010,9,8,7,6,5,4,3,0))
r'C:lah_2010-09-08-07-06-05.log'
brennivin.logutils.wrap_line(s, maxlines, maxlen=254, pfx='- ')
Parameters:
  • s – input string
  • maxlines – max amount of lines, or 0 for no limit
  • maxlen – max length of any one line
  • pfx – prefix for lines after first (counts towards line length)