fundamentals.logs

Logger setup for python projects

Author

David Young

Module Contents

Classes

ColorFormatter

GroupWriteRotatingFileHandler

rotating file handler for logging

emptyLogger

A fake logger object so user can set log=False if required

Functions

API

class fundamentals.logs.ColorFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source][source]

Bases: logging.Formatter

converter[source]

None

default_msec_format[source]

‘%s,%03d’

default_time_format[source]

‘%Y-%m-%d %H:%M:%S’

format[source][source]

‘%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)’

formatException(ei)[source]
formatMessage(record)[source]
formatStack(stack_info)[source]
formatTime(record, datefmt=None)[source]
setFormat(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)', dateFmt='%H:%M:%S')[source][source]
usesTime()[source]
class fundamentals.logs.GroupWriteRotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False, errors=None)[source][source]

Bases: logging.handlers.RotatingFileHandler

rotating file handler for logging

Initialization

Open the specified file and use it as the stream for logging.

By default, the file grows indefinitely. You can specify particular values of maxBytes and backupCount to allow the file to rollover at a predetermined size.

Rollover occurs whenever the current log file is nearly maxBytes in length. If backupCount is >= 1, the system will successively create new files with the same pathname as the base file, but with extensions “.1”, “.2” etc. appended to it. For example, with a backupCount of 5 and a base file name of “app.log”, you would get “app.log”, “app.log.1”, “app.log.2”, … through to “app.log.5”. The file being written to is always “app.log” - when it gets filled up, it is closed and renamed to “app.log.1”, and if files “app.log.1”, “app.log.2” etc. exist, then they are renamed to “app.log.2”, “app.log.3” etc. respectively.

If maxBytes is zero, rollover never occurs.

__class_getitem__[source]

‘classmethod(…)’

__repr__()[source]
acquire()[source]
addFilter(filter)[source]
close()[source]
createLock()[source]
doRollover()[source]

Override base class method to make the new log file group writable.

emit(record)[source]
filter(record)[source]
flush()[source]
format(record)[source]
get_name()[source]
handle(record)[source]
handleError(record)[source]
name[source]

‘property(…)’

namer[source]

None

release()[source]
removeFilter(filter)[source]
rotate(source, dest)[source]
rotation_filename(default_name)[source]
rotator[source]

None

setFormatter(fmt)[source]
setLevel(level)[source]
setStream(stream)[source]
set_name(name)[source]
shouldRollover(record)[source]
terminator[source]

‘\n’

fundamentals.logs.addLoggingLevel(levelName, levelNum, methodName=None)[source][source]

FROM https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility/35804945#35804945

Comprehensively adds a new logging level to the logging module and the currently configured logging class.

levelName becomes an attribute of the logging module with the value levelNum. methodName becomes a convenience method for both logging itself and the class returned by logging.getLoggerClass() (usually just logging.Logger). If methodName is not specified, levelName.lower() is used.

To avoid accidental clobberings of existing attributes, this method will raise an AttributeError if the level name is already an attribute of the logging module or if the method name is already present

Example

addLoggingLevel(‘TRACE’, logging.DEBUG - 5) logging.getLogger(name).setLevel(“TRACE”) logging.getLogger(name).trace(‘that worked’) logging.trace(‘so did this’) logging.TRACE 5

fundamentals.logs.console_logger(level='WARNING')[source][source]

Setup and return a console logger

Key Arguments

  • level – the level of logging required

Return

  • logger – the console logger

Usage

from fundamentals import logs
log = logs.console_logger(
    level="DEBUG"
)
log.debug("Testing console logger")
class fundamentals.logs.emptyLogger[source][source]

Bases: object

A fake logger object so user can set log=False if required

Usage

if log == False:
    from fundamentals.logs import emptyLogger
    log = emptyLogger()
critical(argu)[source][source]
debug(argu)[source][source]
error(argu)[source][source]
info(argu)[source][source]
print(argu)[source][source]
warning(argu)[source][source]
fundamentals.logs.setup_dryx_logging(yaml_file)[source][source]

setup dryx style python logging

Key Arguments

  • level – the level of logging required

Return

  • logger – the console logger

Usage

from fundamentals import logs
log = logs.setup_dryx_logging(
    yaml_file="/Users/Dave/.config/fundamentals/fundamentals.yaml"
)
log.error("test error")

Here is an example of the settings in the yaml file:

version: 1

logging settings:
    formatters:
        file_style:
            format: '* %(asctime)s - %(name)s - %(levelname)s (%(pathname)s > %(funcName)s > %(lineno)d) - %(message)s  '
            datefmt: '%Y/%m/%d %H:%M:%S'
        console_style:
            format: '* %(asctime)s - %(levelname)s: %(pathname)s:%(funcName)s:%(lineno)d > %(message)s'
            datefmt: '%H:%M:%S'
        html_style:
            format: '<div id="row" class="%(levelname)s"><span class="date">%(asctime)s</span>   <span class="label">file:</span><span class="filename">%(filename)s</span>   <span class="label">method:</span><span class="funcName">%(funcName)s</span>   <span class="label">line#:</span><span class="lineno">%(lineno)d</span> <span class="pathname">%(pathname)s</span>  <div class="right"><span class="message">%(message)s</span><span class="levelname">%(levelname)s</span></div></div>'
            datefmt: '%Y-%m-%d <span class= "time">%H:%M <span class= "seconds">%Ss</span></span>'
    handlers:
        console:
            class: logging.StreamHandler
            level: DEBUG
            formatter: console_style
            stream: ext://sys.stdout
        file:
            class: logging.handlers.GroupWriteRotatingFileHandler
            level: WARNING
            formatter: file_style
            filename: /Users/Dave/.config/fundamentals/fundamentals.log
            mode: w+
            maxBytes: 102400
            backupCount: 1
    root:
        level: WARNING
        handlers: [file,console]