PythonCK.logging package

Submodules

PythonCK.logging.filters module

class PythonCK.logging.filters.Blacklist(*whitelist)[source]

Bases: PythonCK.logging.filters.Whitelist

>>> filt = Blacklist('PythonCK')
>>> filt.filter(LogRecord('muon'))
True
>>> filt.filter(LogRecord('PythonCK.logging'))
False
__module__ = 'PythonCK.logging.filters'
filter(record)[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

class PythonCK.logging.filters.FuncFileNameRegexFilter(pattern)[source]

Bases: logging.Filter

Valid if re.search returns non-null.

>>> filt = FuncFileNameRegexFilter(r'tauh[13]')
>>> print(filt)
FuncFileNameRegexFilter: tauh[13]
>>> filt.filter(LogRecord('tauh1'))
True
>>> filt.filter(LogRecord('tauh3'))
True
>>> filt.filter(LogRecord('taumu', func=func0))
False
__init__(pattern)[source]

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

__module__ = 'PythonCK.logging.filters'
__str__() <==> str(x)[source]
filter(record)[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

class PythonCK.logging.filters.Whitelist(*whitelist)[source]

Bases: logging.Filter

Filter the log by name.

>>> filt = Whitelist('muon', 'tau')
>>> filt.filter(LogRecord('muon'))
True
>>> filt.filter(LogRecord('tau.h1'))
True
>>> filt.filter(LogRecord('123'))
False
__init__(*whitelist)[source]

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

__module__ = 'PythonCK.logging.filters'
filter(record)[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

PythonCK.logging.handlers module

Custom logging handler derived from RainbowLoggingHandler.

class PythonCK.logging.handlers.MyLoggingHandler(width=34)[source]

Bases: logging.StreamHandler

My custom log handler, with following touches - support extra field name_override, file_override - changeable column width

__init__(width=34)[source]

Initialize the handler.

If stream is not specified, sys.stderr is used.

__module__ = 'PythonCK.logging.handlers'
format(rec)[source]

Format the specified record.

If a formatter is set, use it. Otherwise, use the default formatter for the module.

PythonCK.logging.handlers.add_indent(lines, width_full, width_column)[source]

Given a paragraph, add indent on each line except first one.

PythonCK.logging.handlers.check_term_width()[source]

Try do determine current terminal width if possible, otherwise default to 120

PythonCK.logging.handlers.condense_two_strings(s1, s2, width)[source]

Given 2 strings (e.g., funcname & filename), return 2 strings pair such that the length is does not exceed given width, in order to maximize space in the left-column of logging.

>>> condense_two_strings('aa', 'bb', 8)
('aa', 'bb    ')
>>> condense_two_strings('aaaa', 'bbbb', 8)
('aaaa', 'bbbb')
>>> condense_two_strings('aaaaaaa', 'bbbb', 8)
('aa..', 'bbbb')
>>> condense_two_strings('aaa', 'bbbbbbb', 8)
('aaa', 'bbb..')
PythonCK.logging.handlers.has_rainbow()[source]

Return boolean whether the package is installed or not, and the enviroment is appropriate for usage or not (i.e., interactive shell).

PythonCK.logging.handlers.splittext(s, width)[source]

Given a string, split into list of substring of given width.

>>> for token in splittext('0123456789', 4):
...   print(token)
0123
4567
89

PythonCK.logging.logger module

class PythonCK.logging.logger.MyLogger(name, level=0)[source]

Bases: logging.Logger

DEBUG = 10
ERROR = 40
INFO = 20
WARNING = 30
__module__ = 'PythonCK.logging.logger'
capture(**kwds)[source]

Usage:

>>> logger = getfixture('logger')
>>> with logger.capture() as lines:
...   print('Inside capture')
...   logger.info("It should be silent here")
...   logger.warning("even if it's very loud.")
Inside capture
>>> for line in lines:
...   print(line)
INFO: <module> - It should be silent here
WARNING: <module> - even if it's very loud.
capture_purge()[source]
capture_start()[source]

Temporary halt the output to its handler, return instance of stringstream to use.

capture_stop()[source]

Undo the effect of capture above to normal behavior.

static extra(func)[source]

Return dict for extra by func, used with MyLoggingHandler above Use for workaround in case of logger in nested function not delegated…

USAGE: log.debug(‘val’, extra=log.extra(func))

PythonCK.logging.utils module

Module contents

Entry point to using my custom logging facility.

  • Colored log (via RainbowLoggingHandler).
  • Log capture contextmanager.
  • Flexible 2-columns width.
  • Regex filter.

REF:

PythonCK.logging.init(path='../../PythonCK/logging/conf.ini')[source]

Load & apply my custom configuration to logging system.

TODO: Accept ‘-v’ flag from argparse at runtime to change LEVEL