scripts/cranslib/clogger.py
Pierre-Elliott Bécue cb361615c4 Le dossier de stockage des logs est une variable globale
* Ça facilitera le passage en environnement de test
2015-02-20 17:26:41 +01:00

92 lines
3.3 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Basic logger, storing data in /var/log/clogger/*.log
# Author : Pierre-Elliott Bécue <becue@crans.org>
# License : GPLv3
# Date : 27/04/2014
import os
import datetime
import pytz
import logging
TZ = pytz.timezone('Europe/Paris')
LDIRPATH = '/home/becue/temp'
class CLogger(logging.Logger):
"""
Crans logger.
"""
def __init__(self, loggerName, service=None, level="info", debug=False):
"""
Initializes logger. The debug variable is useful to have a print to stdout (when debugging)
"""
super(CLogger, self).__init__(loggerName)
# When no service is specified, we don't put the reference in the format.
if service is None:
fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
else:
fmt = "%%(asctime)s - %%(name)s - %(service)s - %%(levelname)s - %%(message)s" % {'service': service}
# Computes the file handler name using service name.
file_handler_path = os.path.join(LDIRPATH, "%s.log" % (loggerName,))
# Creates FileHandler
self.fh = logging.FileHandler(file_handler_path)
# Catches appropriate level in logging.
self.fhlevel = getattr(logging, level.upper(), logging.INFO)
self.fh.setLevel(self.fhlevel)
# Creates formatter
self.formatter = CFormatter(fmt, "%Y-%m-%dT%H:%M:%S.%f%z")
# Adds formatter to FileHandler
self.fh.setFormatter(self.formatter)
if debug:
self.sh = logging.StreamHandler()
self.shlevel = logging.DEBUG
self.sh.setLevel(self.shlevel)
self.sh.setFormatter(self.formatter)
self.addHandler(self.sh)
# Adds FileHandler to Handlers
self.addHandler(self.fh)
class CFormatter(logging.Formatter):
"""
This Formatter subclasses the classic formatter to provide a
timezone-aware logging.
"""
converter = datetime.datetime.fromtimestamp
def formatTime(self, record, datefmt=None):
"""
Return the creation time of the specified LogRecord as formatted text.
This method should be called from format() by a formatter which
wants to make use of a formatted time. This method can be overridden
in formatters to provide for any specific requirement, but the
basic behaviour is as follows: if datefmt (a string) is specified,
it is used with time.strftime() to format the creation time of the
record. Otherwise, the ISO8601 format is used. The resulting
string is returned. This function uses a user-configurable function
to convert the creation time to a tuple. By default, time.localtime()
is used; to change this for a particular formatter instance, set the
'converter' attribute to a function with the same signature as
time.localtime() or time.gmtime(). To change it for all formatters,
for example if you want all logging times to be shown in GMT,
set the 'converter' attribute in the Formatter class.
"""
ct = self.converter(record.created, TZ)
ct = ct.replace(microsecond=int(record.msecs * 1000))
if datefmt:
s = ct.strftime(datefmt)
else:
s = ct.strftime("%Y-%m-%d %H:%M:%S.%f")
return s