Status handler

A log handler that examines every log and does some statistics on it. It generates a log message in regular intervals.

Metrics that are collected:

  • the number of messages per log level, available in formatter as:

    • %(DEBUG)d
    • %(INFO)d
    • %(WARN)d
    • %(ERROR)d
    • %(FATAL)d
  • the size of messages per log level, available in formatter as

    • %(DEBUG-SIZE)d
    • %(INFO-SIZE)d
    • %(WARN-SIZE)d
    • %(ERROR-SIZE)d
    • %(FATAL-SIZE)d
  • custom values examples:

    from starlog import inc
    logger.info('one more log line', extra=inc('requests').inc('2xx'))
    logger.info('bad request', extra=inc('4xx').update(error='Invalid input'))
    
    • %(requests)d
    • %(2xx)d
    • %(4xx)d

Use case: write out status logs every some seconds to standard out and sent full logs to syslog or a file.

class starlog.StatusHandler(interval='5s', logger='starlog.status')

StatusHandler is a log handler that aggregates log messages and generates a status log message in regular intervals.

Parameters:interval (str or int) – the log status interval. Values must be of the format Xs (seconds), Xm (minutes) or Xh (hours), where X is a positive integer value. If you pass an integer instead of a string, then this value is taken as the number of seconds. Examples: 15s, 5m, 1h

Example:

import logging
import starlog
import sys
import time


formatter = logging.Formatter('Seen info messages: %(INFO)d')
handler = starlog.StatusHandler('1s')
logging.root.addHandler(handler)
logging.root.setLevel(logging.DEBUG)

stdout = logging.StreamHandler(sys.stdout)
stdout.setFormatter(formatter)
logging.getLogger('starlog.status').addHandler(stdout)

logging.root.info('you will not see this message - just the status')

time.sleep(5)

# output is:
>>> Seen info messages: 1
>>> Seen info messages: 0
>>> Seen info messages: 0
>>> Seen info messages: 0
>>> Seen info messages: 0