changeset 1005:b4af31a8a4f2

core (logs): added formatting, name filter and outputs management: - formatting is inspired from, and use when possible, standard logging. "message", "levelname", and "name" are the only format managed, depending on backend more can be managed (standard backend formats are specified in official python logging doc) - name filter use regular expressions. It's possible to log only plugins with SAT_LOG_LOGGER="^sat.plugins". To log only XEPs 96 & 65, we can use SAT_LOG_LOGGER='(xep_0095|xep_0065)' - output management use a particular syntax: - output handler are name with "//", so far there are "//default" (most of time stderr), "//memory" and "//file" - options can be specified in parenthesis, e.g. "//memory(50)" mean a 50 lines memory buffer (50 is the current default, so that's equivalent to "//memory") - several handlers can be specified: "//file(/tmp/sat.log)//default" will use the default logging + a the /tmp/sat.log file - if there is only one handler, it use the file handler: "/tmp/sat.log" is the same as "//file(/tmp/sat.log)" - not finished, need more work for twisted and basic backends
author Goffi <goffi@goffi.org>
date Mon, 05 May 2014 18:58:34 +0200
parents 191f440d11b4
children 325fd230c15d
files src/core/constants.py src/core/log.py
diffstat 2 files changed, 127 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/constants.py	Thu May 01 11:14:25 2014 +0200
+++ b/src/core/constants.py	Mon May 05 18:58:34 2014 +0200
@@ -80,8 +80,23 @@
     LOG_BASE_LOGGER = 'root'
     LOG_OPT_PREFIX = 'log_'
     # (option_name, default_value) tuples
-    LOG_OPT_COLORS = ('colors', 'true')
+    LOG_OPT_COLORS = ('colors', 'true') # true for auto colors, force to have colors even if stdout is not a tty, false for no color
     LOG_OPT_LEVEL = ('level', 'info')
+    LOG_OPT_FORMAT = ('fmt', '%(message)s') # similar to logging format.
+    LOG_OPT_LOGGER = ('logger', '') # regex to filter logger name
+    LOG_OPT_OUTPUT_SEP = '//'
+    LOG_OPT_OUTPUT_DEFAULT = 'default'
+    LOG_OPT_OUTPUT_MEMORY = 'memory'
+    LOG_OPT_OUTPUT_MEMORY_LIMIT = 50
+    LOG_OPT_OUTPUT_FILE = 'file' # file is implicit if only output
+    LOG_OPT_OUTPUT = ('output', LOG_OPT_OUTPUT_SEP + LOG_OPT_OUTPUT_DEFAULT) # //default = normal output (stderr or a file with twistd), path/to/file for a file (must be the first if used), //memory for memory (options can be put in parenthesis, e.g.: //memory(500) for a 500 lines memory)
+    LOG_OPTIONS = (LOG_OPT_COLORS, LOG_OPT_LEVEL, LOG_OPT_FORMAT, LOG_OPT_LOGGER, LOG_OPT_OUTPUT)
+    LOG_LVL_DEBUG = 'DEBUG'
+    LOG_LVL_INFO = 'INFO'
+    LOG_LVL_WARNING = 'WARNING'
+    LOG_LVL_ERROR = 'ERROR'
+    LOG_LVL_CRITICAL = 'CRITICAL'
+    LOG_LEVELS = (LOG_LVL_DEBUG, LOG_LVL_INFO, LOG_LVL_WARNING, LOG_LVL_ERROR, LOG_LVL_CRITICAL)
 
 
     ## Misc ##
@@ -92,7 +107,7 @@
 
     ## ANSI escape sequences ##
     # XXX: used for logging
-    # XXX: there will be probably moved in a dedicated module in the future
+    # XXX: they will be probably moved in a dedicated module in the future
     ANSI_RESET = '\033[0m'
     ANSI_NORMAL_WEIGHT = '\033[22m'
     ANSI_FG_BLACK, ANSI_FG_RED, ANSI_FG_GREEN, ANSI_FG_YELLOW, ANSI_FG_BLUE, ANSI_FG_MAGENTA, ANSI_FG_CYAN, ANSI_FG_WHITE = ('\033[3%dm' % nb for nb in xrange(8))
--- a/src/core/log.py	Thu May 01 11:14:25 2014 +0200
+++ b/src/core/log.py	Mon May 05 18:58:34 2014 +0200
@@ -18,13 +18,14 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 """High level logging functions"""
-# XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. In addition additional feature like environment variable and color are also managed.
+# XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed.
 
 from sat.core.constants import Const as C
 from sat.core import exceptions
 
 _backend = None
 _loggers = {}
+_handlers = {}
 
 
 class Logger(object):
@@ -49,15 +50,85 @@
         print msg
 
 
-def _configureStdLogging(logging, level=None, colors=False, force_colors=False):
+class FilterName(object):
+
+    def __init__(self, name_re):
+        """Initialise name filter
+
+        @param name_re: regular expression used to filter names (using search and not match)
+        """
+        assert name_re
+        import re
+        self.name_re = re.compile(name_re)
+
+    def filter(self, record):
+        if self.name_re.search(record.name) is not None:
+            return 1
+        return 0
+
+def _manageOutputs(outputs_raw):
+    """ Parse output option in a backend agnostic way, and fill _backend consequently
+
+    @param outputs_raw: output option as enterred in environment variable or in configuration
+    """
+    if not outputs_raw:
+        return
+    outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP)
+    global _handlers
+    if len(outputs) == 1:
+        _handlers[C.LOG_OPT_OUTPUT_FILE] = outputs.pop()
+
+    for output in outputs:
+        if not output:
+            continue
+        if output[-1] == ')':
+            # we have options
+            opt_begin = output.rfind('(')
+            options = output[opt_begin+1:-1]
+            output = output[:opt_begin]
+        else:
+            options = None
+
+        if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, C.LOG_OPT_OUTPUT_MEMORY):
+            raise ValueError(u"Invalid output [%s]" % output)
+
+        if output == C.LOG_OPT_OUTPUT_DEFAULT:
+            # no option for defaut handler
+            _handlers[output] = None
+        elif output == C.LOG_OPT_OUTPUT_FILE:
+            if not options:
+                ValueError("%(handler)s output need a path as option" % {'handler': output})
+            _handlers[output] = options
+            options = None # option are parsed, we can empty them
+        elif output == C.LOG_OPT_OUTPUT_MEMORY:
+            # we have memory handler, option can be the len limit or None
+            try:
+                limit = int(options)
+                options = None # option are parsed, we can empty them
+            except (TypeError, ValueError):
+                limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT
+            _handlers[output] = limit
+
+        if options: # we should not have unparsed options
+            raise ValueError(u"options [%(options)s] are not supported for %(handler)s output" % {'options': options, 'handler': output})
+
+def _configureStdLogging(logging, level=None, fmt=C.LOG_OPT_FORMAT[1], output=C.LOG_OPT_OUTPUT[1], logger=None, colors=False, force_colors=False):
     """Configure standard logging module
+
     @param logging: standard logging module
+    @param level: one of C.LOG_LEVELS
+    @param fmt: format string, pretty much as in std logging. Accept the following keywords (maybe more depending on backend):
+        - "message"
+        - "levelname"
+        - "name" (logger name)
+    @param logger: if set, use it as a regular expression to filter on logger name.
+        Use search to match expression, so ^ or $ can be necessary.
     @param colors: if True use ANSI colors to show log levels
     @param force_colors: if True ANSI colors are used even if stdout is not a tty
     """
-    FORMAT = '%(message)s'
+    format_ = fmt
     if level is None:
-        level = logging.DEBUG
+        level = C.LOG_LVL_DEBUG
     import sys
     with_color = colors & (sys.stdout.isatty() or force_colors)
     if not colors and force_colors:
@@ -98,13 +169,27 @@
 
     root_logger = logging.getLogger()
     if len(root_logger.handlers) == 0:
-        hdlr = logging.StreamHandler()
-        formatter = SatFormatter(FORMAT)
-        hdlr.setFormatter(formatter)
-        root_logger.addHandler(hdlr)
-        root_logger.setLevel(level)
+        _manageOutputs(output)
+        formatter = SatFormatter(format_)
+        name_filter = FilterName(logger) if logger else None
+        for handler, options in _handlers.items():
+            if handler == C.LOG_OPT_OUTPUT_DEFAULT:
+                hdlr = logging.StreamHandler()
+            elif handler == C.LOG_OPT_OUTPUT_MEMORY:
+                import logging.handlers
+                hdlr = logging.handlers.BufferingHandler(options)
+            elif handler == C.LOG_OPT_OUTPUT_FILE:
+                import os.path
+                hdlr = logging.FileHandler(os.path.expanduser(options))
+            else:
+                raise ValueError("Unknown handler type")
+            hdlr.setFormatter(formatter)
+            root_logger.addHandler(hdlr)
+            root_logger.setLevel(level)
+            if name_filter is not None:
+                hdlr.addFilter(name_filter)
     else:
-        root_logger.warning(u"Handler already set on root logger")
+        root_logger.warning(u"Handlers already set on root logger")
 
 def configure(backend=C.LOG_BACKEND_STANDARD, **options):
     """Configure logging bejaviour
@@ -149,19 +234,22 @@
 
     @param options (dict): options with (key: name, value: string value)
     """
-    if 'colors' in options:
-        if options['colors'].lower() in ('1', 'true'):
-            options['colors'] = True
-        elif options['colors'] == 'force':
-            options['colors'] = True
+    COLORS = C.LOG_OPT_COLORS[0]
+    LEVEL = C.LOG_OPT_LEVEL[0]
+
+    if COLORS in options:
+        if options[COLORS].lower() in ('1', 'true'):
+            options[COLORS] = True
+        elif options[COLORS] == 'force':
+            options[COLORS] = True
             options['force_colors'] = True
         else:
-            options['colors'] = False
-    if 'level' in options:
-        level = options['level'].upper()
-        if level not in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'):
-            level = 'INFO'
-        options['level'] = level
+            options[COLORS] = False
+    if LEVEL in options:
+        level = options[LEVEL].upper()
+        if level not in C.LOG_LEVELS:
+            level = C.LOG_LVL_INFO
+        options[LEVEL] = level
 
 def satConfigure(backend=C.LOG_BACKEND_TWISTED):
     """Configure logging system for SàT, can be used by frontends
@@ -170,11 +258,10 @@
     """
     import ConfigParser
     import os
-    to_get = (C.LOG_OPT_COLORS, C.LOG_OPT_LEVEL)
     log_conf = {}
     config = ConfigParser.SafeConfigParser()
     config.read(C.CONFIG_FILES)
-    for opt_name, opt_default in to_get:
+    for opt_name, opt_default in C.LOG_OPTIONS:
         try:
             log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))]
         except KeyError: