borgend/loggers.py

Wed, 07 Feb 2018 20:39:01 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 07 Feb 2018 20:39:01 +0000
changeset 113
6993964140bd
parent 90
8f84b3e60336
permissions
-rw-r--r--

Time snapshot fixes.
Python's default arguments are purely idiotic (aka. pythonic): generated
only once. This makes sense in a purely functional language, which Python
lightyears away from, but severely limits their usefulness in an imperative
language. Decorators also seem clumsy for this, as one would have to tell
the number of positional arguments for things to work nice, being able to
pass the snapshot both positionally and as keyword. No luck.
So have to do things the old-fashioned hard way.

#
# Borgend by Tuomo Valkonen, 2018
#
# This file configures the logging module for Borgend
#

import os
import logging
import logging.handlers
import atexit

from .fifolog import FIFOHandler
from . import branding
from . import locations

#
# Logging configuration
#

loglevel=logging.INFO
logfmt="%(asctime)s:%(levelname)s:%(name)s:%(message)s"
fifo_capacity=1000
fifo_fmt="%(asctime)s:%(levelname)s:%(message)s"
fileslog_config={
    'when': 'midnight',
    'interval': 1,
    'backupCount': 7
    }


#
# Setting up the main logger with fifo, stderr, and rotating files output
#

mainlogger=logging.getLogger(branding.appname)
mainlogger.setLevel(loglevel)
mainlogger.propagate=True

mainlogger.handlers.clear()

# Internal FIFO history
fifo=FIFOHandler(fifo_capacity)
fifo.setFormatter(logging.Formatter(fifo_fmt))
mainlogger.addHandler(fifo)

# stderr
stderrlog=logging.StreamHandler()
stderrlog.setLevel(logging.WARNING)
mainlogger.addHandler(stderrlog)

# Rotating files
if not os.path.isdir(locations.logs_dir):
    os.makedirs(locations.logs_dir)

fileslog=logging.handlers.TimedRotatingFileHandler(
    os.path.join(locations.logs_dir, branding.appname+'.log'),
    **fileslog_config)
fileslog.setFormatter(logging.Formatter(logfmt))
mainlogger.addHandler(fileslog)

atexit.register(logging.shutdown)

mercurial