summaryrefslogtreecommitdiff
path: root/logging_ext.py
diff options
context:
space:
mode:
authorSylvain Thenault <sylvain.thenault@logilab.fr>2008-12-04 18:00:49 +0100
committerSylvain Thenault <sylvain.thenault@logilab.fr>2008-12-04 18:00:49 +0100
commit28421846074c4c60f9ff2b4124664a1305450671 (patch)
treeb4aeac908459c2c2c91e85659329a8ba8279de26 /logging_ext.py
parentb0ec5e96981b5f397a53bd80472cff154afe7300 (diff)
downloadlogilab-common-28421846074c4c60f9ff2b4124664a1305450671.tar.gz
backport init_log/set_log_methods
Diffstat (limited to 'logging_ext.py')
-rw-r--r--logging_ext.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/logging_ext.py b/logging_ext.py
index b788c00..67b8342 100644
--- a/logging_ext.py
+++ b/logging_ext.py
@@ -8,10 +8,20 @@
"""
__docformat__ = "restructuredtext en"
+import os
+import sys
import logging
from logilab.common.textutils import colorize_ansi
+
+def set_log_methods(cls, logger):
+ """bind standart logger's methods as methods on the class"""
+ cls.__logger = logger
+ for attr in ('lldebug', 'debug', 'info', 'warning', 'error', 'critical', 'exception'):
+ setattr(cls, attr, getattr(logger, attr))
+
+
def xxx_cyan(record):
if 'XXX' in record.message:
return 'cyan'
@@ -70,3 +80,63 @@ def set_color_formatter(logger=None, **kw):
fmt = ColorFormatter(format_msg, **kw)
fmt.colorfilters.append(xxx_cyan)
logger.handlers[0].setFormatter(fmt)
+
+
+LOG_FORMAT = '%(asctime)s - (%(name)s) %(levelname)s: %(message)s'
+LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
+
+def init_log(debug=False, syslog=False, logthreshold=None, logfile=None,
+ logformat=LOG_FORMAT, logdateformat=LOG_DATE_FORMAT):
+ """init the log service"""
+ if os.environ.get('APYCOT_ROOT'):
+ logthreshold = logging.CRITICAL
+ # redirect logs to stdout to avoid apycot output parsing failure
+ handler = logging.StreamHandler(sys.stdout)
+ else:
+ if debug:
+ logthreshold = logging.DEBUG
+ handler = logging.StreamHandler()
+ elif logfile is None:
+ if syslog:
+ from logging import handlers
+ handler = handlers.SysLogHandler()
+ else:
+ handler = logging.StreamHandler()
+ else:
+ try:
+ handler = logging.FileHandler(logfile)
+ except IOError:
+ handler = logging.StreamHandler()
+ if logthreshold is None:
+ logthreshold = logging.ERROR
+ elif isinstance(logthreshold, basestring):
+ logthreshold = getattr(logging, THRESHOLD_MAP.get(logthreshold,
+ logthreshold))
+ # configure the root logger
+ logger = logging.getLogger()
+ logger.setLevel(logthreshold)
+ # only addHandler and removeHandler method while I would like a
+ # setHandler method, so do it this way :$
+ logger.handlers = [handler]
+ isatty = hasattr(sys.__stdout__, 'isatty') and sys.__stdout__.isatty()
+ if debug and isatty:
+ fmt = ColorFormatter(logformat, logdateformat)
+ def col_fact(record):
+ if 'XXX' in record.message:
+ return 'cyan'
+ if 'kick' in record.message:
+ return 'red'
+ fmt.colorfilters.append(col_fact)
+ else:
+ fmt = logging.Formatter(logformat, logdateformat)
+ handler.setFormatter(fmt)
+ return handler
+
+# map logilab.common.logger thresholds to logging thresholds
+THRESHOLD_MAP = {'LOG_DEBUG': 'DEBUG',
+ 'LOG_INFO': 'INFO',
+ 'LOG_NOTICE': 'INFO',
+ 'LOG_WARN': 'WARNING',
+ 'LOG_ERR': 'ERROR',
+ 'LOG_CRIT': 'CRITICAL',
+ }