CLogger utilise un formatter custom et est timezone-aware maintenant.
This commit is contained in:
parent
48573c47d4
commit
786eaca90e
1 changed files with 39 additions and 1 deletions
|
@ -6,8 +6,12 @@
|
||||||
# License : GPLv3
|
# License : GPLv3
|
||||||
# Date : 27/04/2014
|
# Date : 27/04/2014
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import pytz
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
TZ = pytz.timezone('Europe/Paris')
|
||||||
|
|
||||||
class CLogger(logging.Logger):
|
class CLogger(logging.Logger):
|
||||||
"""
|
"""
|
||||||
Crans logger
|
Crans logger
|
||||||
|
@ -27,7 +31,7 @@ class CLogger(logging.Logger):
|
||||||
self.fh.setLevel(self.fhlevel)
|
self.fh.setLevel(self.fhlevel)
|
||||||
|
|
||||||
# Creates formatter
|
# Creates formatter
|
||||||
self.formatter = logging.Formatter('%%(asctime)s - %%(name)s - %(service)s - %%(levelname)s - %%(message)s' % {'service': service})
|
self.formatter = CFormatter('%%(asctime)s - %%(name)s - %(service)s - %%(levelname)s - %%(message)s' % {'service': service}, "%Y-%m-%dT%H:%M:%S.%f%z")
|
||||||
|
|
||||||
# Adds formatter to FileHandler
|
# Adds formatter to FileHandler
|
||||||
self.fh.setFormatter(self.formatter)
|
self.fh.setFormatter(self.formatter)
|
||||||
|
@ -41,3 +45,37 @@ class CLogger(logging.Logger):
|
||||||
|
|
||||||
# Adds FileHandler to Handlers
|
# Adds FileHandler to Handlers
|
||||||
self.addHandler(self.fh)
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue