1
2
3 """Extends the logging module from the standard library.
4
5 :copyright: 2000-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
7 :license: General Public License version 2 - http://www.gnu.org/licenses
8 """
9 __docformat__ = "restructuredtext en"
10
11 import os
12 import sys
13 import logging
14
15 from logilab.common.textutils import colorize_ansi
16
17
19 """bind standard logger's methods as methods on the class"""
20 cls.__logger = logger
21 for attr in ('debug', 'info', 'warning', 'error', 'critical', 'exception'):
22 setattr(cls, attr, getattr(logger, attr))
23
24
26 if 'XXX' in record.message:
27 return 'cyan'
28
66
83
84
85 LOG_FORMAT = '%(asctime)s - (%(name)s) %(levelname)s: %(message)s'
86 LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
87
90 """init the log service"""
91 if os.environ.get('APYCOT_ROOT'):
92 logthreshold = logging.CRITICAL
93
94 handler = logging.StreamHandler(sys.stdout)
95 else:
96 if debug:
97 handler = logging.StreamHandler()
98 elif logfile is None:
99 if syslog:
100 from logging import handlers
101 handler = handlers.SysLogHandler()
102 else:
103 handler = logging.StreamHandler()
104 else:
105 try:
106 handler = logging.FileHandler(logfile)
107 except IOError:
108 handler = logging.StreamHandler()
109 if logthreshold is None:
110 logthreshold = logging.ERROR
111 elif isinstance(logthreshold, basestring):
112 logthreshold = getattr(logging, THRESHOLD_MAP.get(logthreshold,
113 logthreshold))
114
115 logger = logging.getLogger()
116 logger.setLevel(logthreshold)
117
118
119 logger.handlers = [handler]
120 isatty = hasattr(sys.__stdout__, 'isatty') and sys.__stdout__.isatty()
121 if debug and isatty and sys.platform != 'win32':
122 fmt = ColorFormatter(logformat, logdateformat)
123 def col_fact(record):
124 if 'XXX' in record.message:
125 return 'cyan'
126 if 'kick' in record.message:
127 return 'red'
128 fmt.colorfilters.append(col_fact)
129 else:
130 fmt = logging.Formatter(logformat, logdateformat)
131 handler.setFormatter(fmt)
132 return handler
133
134
135 THRESHOLD_MAP = {'LOG_DEBUG': 'DEBUG',
136 'LOG_INFO': 'INFO',
137 'LOG_NOTICE': 'INFO',
138 'LOG_WARN': 'WARNING',
139 'LOG_WARNING': 'WARNING',
140 'LOG_ERR': 'ERROR',
141 'LOG_ERROR': 'ERROR',
142 'LOG_CRIT': 'CRITICAL',
143 }
144