borgend/loggers.py

Sun, 28 Jan 2018 17:54:14 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 28 Jan 2018 17:54:14 +0000
changeset 86
2fe66644c50d
parent 81
7bcd715f19e3
child 89
51cc2e25af38
permissions
-rw-r--r--

Can use logging.getLogger directly now after proper packageisation

#
# Loggers
#

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)

#
# Routine for obtaining sub-loggers
#

def get(name):
    #return mainlogger.getChild(name)
    return logging.getLogger(name)

mercurial