]> kaliko git repositories - mpd-sima.git/blobdiff - sima/lib/logger.py
Releasing 0.17.0
[mpd-sima.git] / sima / lib / logger.py
index f1569334abddb4d04934ef5dd5a0c6b8fa79ed15..50785f12e9bbbd1f77e0ccee5983748618215f9c 100644 (file)
@@ -1,5 +1,5 @@
 # -*- 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
 #
@@ -26,6 +26,8 @@ Logging facility for sima.
 import logging
 import sys
 
+from os import environ
+
 DEBUG = logging.DEBUG
 INFO = logging.INFO
 ERROR = logging.ERROR
@@ -37,14 +39,14 @@ LOG_FORMATS = {
         }
 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):
@@ -52,10 +54,12 @@ 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:
@@ -88,10 +92,10 @@ def set_logger(level='info', logfile=None):
             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
@@ -100,7 +104,7 @@ def set_logger(level='info', logfile=None):
 
         # 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