lazylog package

Module contents

class lazylog.ColorFormatter(fmt, datefmt=None, color=True, splitLines=True, pretty=False, colors=None, styles=None)[source]

Bases: logging.Formatter

Color formatter. This class is working as expected atm but it can be tidied up to support blinking text and other useless features :)

BLACK = 0

Bash Colors

Bash Styles

BLUE = 4

Bash Colors

BOLD = 1

Bash Styles

COLORS = {'CRITICAL': 3, 'DEBUG': 7, 'ERROR': 1, 'INFO': 6, 'WARNING': 3}

Default color values for each log-level

COLOR_SEQ = '\x1b[1;%dm'

Escape seq. + color integer placeholder

CON = 8

Bash Styles

CYAN = 6

Bash Colors

ESC = '\x1b['

Escape sequence for color

FILEDEFAULTS = {'color': False, 'level': 20, 'pretty': False, 'splitLines': True}

File default settings

GREEN = 2

Bash Colors

INV = 7

Bash Styles

ITAL = 3

Bash Styles

MAGENTA = 5

Bash Colors

NORM = 0

Bash Styles

RED = 1

Bash Colors

RESET_SEQ = '\x1b[0m'

Reset color sequence

STYLES = {'CRITICAL': 1, 'DEBUG': 0, 'ERROR': 1, 'INFO': 0, 'WARNING': 0}

Each log-level’s default character style

TERMDEFAULTS = {'color': True, 'level': 10, 'pretty': True, 'splitLines': True}

Terminal default settings

UNDER = 4

Bash Styles

UNK = 2

Bash Styles

UNK2 = 6

Bash Styles

WHITE = 7

Bash Colors

YELLOW = 3

Bash Colors

format(record)[source]

Override format function

static parseSpecs(specs, defaults)[source]

Merge the user specs with the defaults given (terminal or file). Also ensure that if pretty is set, splitLines is also set (implied)

class lazylog.JSONFormatter(fields, datefmt=None)[source]

Bases: logging.Formatter

Format messages as JSON

Lots of ideas from:

FIELDS = ['created', 'process', 'thread', 'levelname', 'module', 'filename', 'funcNamelineno', 'message']
RESERVED_ATTRS = ('args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'msg', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName')
format(record)[source]

Override format function

class lazylog.Logger(name, level=0)[source]

Bases: logging.Logger

Core logger for all apps and scripts. This logger adds the following features to the default:

  • Colors!
  • Cooler line format
  • Function is included in the log format
  • File log
  • File rotation every 5M with 100 files kept
BACKUPCOUNT = 20

Default number of backup files to keep

DATEFORMAT = '%d-%m-%Y %H:%M:%S'

Date output format

LOGDIR = '/var/log'

Log directory, it will be created if it does not exist

LOGFORMAT = '%(asctime)s.%(msecs)03d %(process)s:%(thread)u %(levelname)-8s %(module)15.15s %(lineno)-4s: %(message)s'

Default log format for all handlers. This can change in init()

MAXBYTES = 10000000

Default maximum file size allowed, after that rotation is taking place

classmethod addFileLogger(specs)[source]

Add a file logger with the given specs. Specs:

{
    'filename': Filename (under LOGDIR)
    'level': Logging level for this file
    'format': [ 'json' | 'console' | 'default' ] # TODO: CSV
    'backupCount': Number of files to keep
    'maxBytes': Maximum file size
}

If format is set to “console”, then ColorFormatter options are also supported.

classmethod init(folder='/var/log/lazylog', termSpecs={}, fileSpecs=None, fmt=None, datefmt=None)[source]

Initialize logging based on the requested fileName. This will create one logger (global) that writes in both file and terminal

Parameters:
  • fileSpecs (str) – A dict with ‘filename’, ‘level’, etc. See addFileLogger for details
  • termSpecs (int) – A dict with boolean values for ‘color’ and ‘splitLines’
static logFun()[source]

Print one message in each level to demo the colours

static mockHandler(index)[source]
static restoreHandler(index)[source]
static setConsoleLevel(level)[source]

In this logger, by convention, handler 0 is always the console halder.

static setFileLevel(filenum, level)[source]

Set a file logger log level. Filenum is 1,2,3,.. in the order the files have been added.

lazylog.merge_dicts(source, destination)[source]

Full credit to: https://stackoverflow.com/a/20666342/3727050

>>> a = { 'first' : { 'all_rows' : { 'pass' : 'dog', 'number' : '1' } } }
>>> b = { 'first' : { 'all_rows' : { 'fail' : 'cat', 'number' : '5' } } }
>>> merge_dicts(b, a) == { 'first' : { 'all_rows' : { 'pass' : 'dog', 'fail' : 'cat', 'number' : '5' } } }
True
lazylog.mkdir_p(path)[source]

Does the same as ‘mkdir -p’ in linux

lazylog.pretty(sth)[source]

Format objects, tuples, lists and dicts for pretty printing

lazylog.pretty_recursive(value, htchar='\t', lfchar='\n', indent=0)[source]

Recursive pretty printing of dict, list and tuples - full creadit to:

http://stackoverflow.com/questions/3229419/pretty-printing-nested-dictionaries-in-python