loggers.py

Sun, 28 Jan 2018 11:38:01 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 28 Jan 2018 11:38:01 +0000
changeset 79
b075b3db3044
permissions
-rw-r--r--

Cleaned up module organisation to simplify borgend.py and not have to import it in other modules.

#
# Loggers
#

import os
import logging
import logging.handlers
import atexit

from fifolog import FIFOHandler
import branding
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"

#
# 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'),
    when='D', interval=1)
fileslog.setFormatter(logging.Formatter(logfmt))
mainlogger.addHandler(fileslog)

atexit.register(logging.shutdown)

#
# Routine for obtaining sub-loggers
#

def get(name):
    return mainlogger.getChild(name)

mercurial