# -*- coding: utf-8 -*-
-# Copyright (c) 2009, 2010, 2013, 2014, 2015 Jack Kaliko <kaliko@azylum.org>
+# Copyright (c) 2009, 2010, 2013, 2014, 2015 kaliko <kaliko@azylum.org>
#
# This file is part of sima
#
import logging
import sys
+from os import environ
+
DEBUG = logging.DEBUG
INFO = logging.INFO
ERROR = logging.ERROR
}
DATE_FMT = "%Y-%m-%d %H:%M:%S"
+TRACE_LEVEL_NUM = 5
+logging.addLevelName(TRACE_LEVEL_NUM, 'TRACE')
+def trace(self, message, *args, **kwargs):
+ # Yes, logger takes its '*args' as 'args'.
+ if self.isEnabledFor(TRACE_LEVEL_NUM):
+ self._log(TRACE_LEVEL_NUM, message, args, **kwargs)
-class LevelFilter(logging.Filter):
- """
- Enable logging between two log level by filtering everything < level.
- """
- def filter(self, record):
- """Defines loglevel"""
- return record.levelno <= ERROR
+logging.Logger.trace = trace
def set_logger(level='info', logfile=None):
logger:
level: in debug, info, warning,…
logfile: file to log to
-
"""
name = 'sima'
- user_log_level = getattr(logging, level.upper())
+ if environ.get('TRACE', False):
+ user_log_level = TRACE_LEVEL_NUM
+ else:
+ user_log_level = getattr(logging, level.upper())
if user_log_level > DEBUG:
log_format = LOG_FORMATS.get(INFO)
else:
return
# create console handler with a specified log level (STDOUT)
couth = logging.StreamHandler(sys.stdout)
- couth.addFilter(LevelFilter())
+ couth.addFilter(lambda record: record.levelno < ERROR)
# create console handler with warning log level (STDERR)
- cerrh = logging.StreamHandler()
+ cerrh = logging.StreamHandler(sys.stderr)
cerrh.setLevel(ERROR)
# add formatter to the handlers
# add the handlers to SIMA_LOGGER
logger.addHandler(couth)
- #logger.addHandler(cerrh) # Already added creating the handler‽ Still have to figure it out.
+ logger.addHandler(cerrh) # Already added creating the handler‽ Still have to figure it out.
# VIM MODLINE
# vim: ai ts=4 sw=4 sts=4 expandtab