OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
4 # | 4 # |
5 # This file is part of logilab-common. | 5 # This file is part of logilab-common. |
6 # | 6 # |
7 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 7 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
8 # the terms of the GNU Lesser General Public License as published by the Free | 8 # the terms of the GNU Lesser General Public License as published by the Free |
9 # Software Foundation, either version 2.1 of the License, or (at your option) an
y | 9 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
10 # later version. | 10 # later version. |
11 # | 11 # |
12 # logilab-common is distributed in the hope that it will be useful, but WITHOUT | 12 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | 14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
15 # details. | 15 # details. |
16 # | 16 # |
17 # You should have received a copy of the GNU Lesser General Public License along | 17 # You should have received a copy of the GNU Lesser General Public License along |
18 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. | 18 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. |
19 """Extends the logging module from the standard library.""" | 19 """Extends the logging module from the standard library.""" |
20 | 20 |
21 __docformat__ = "restructuredtext en" | 21 __docformat__ = "restructuredtext en" |
22 | 22 |
23 import os | 23 import os |
24 import sys | 24 import sys |
25 import logging | 25 import logging |
26 | 26 |
27 from six import string_types | |
28 | |
29 from logilab.common.textutils import colorize_ansi | 27 from logilab.common.textutils import colorize_ansi |
30 | 28 |
31 | 29 |
32 def set_log_methods(cls, logger): | 30 def set_log_methods(cls, logger): |
33 """bind standard logger's methods as methods on the class""" | 31 """bind standard logger's methods as methods on the class""" |
34 cls.__logger = logger | 32 cls.__logger = logger |
35 for attr in ('debug', 'info', 'warning', 'error', 'critical', 'exception'): | 33 for attr in ('debug', 'info', 'warning', 'error', 'critical', 'exception'): |
36 setattr(cls, attr, getattr(logger, attr)) | 34 setattr(cls, attr, getattr(logger, attr)) |
37 | 35 |
38 | 36 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 handler = logging.StreamHandler() | 105 handler = logging.StreamHandler() |
108 elif logfile is None: | 106 elif logfile is None: |
109 if syslog: | 107 if syslog: |
110 from logging import handlers | 108 from logging import handlers |
111 handler = handlers.SysLogHandler() | 109 handler = handlers.SysLogHandler() |
112 else: | 110 else: |
113 handler = logging.StreamHandler() | 111 handler = logging.StreamHandler() |
114 else: | 112 else: |
115 try: | 113 try: |
116 if rotation_parameters is None: | 114 if rotation_parameters is None: |
117 if os.name == 'posix' and sys.version_info >= (2, 6): | 115 handler = logging.FileHandler(logfile) |
118 from logging.handlers import WatchedFileHandler | |
119 handler = WatchedFileHandler(logfile) | |
120 else: | |
121 handler = logging.FileHandler(logfile) | |
122 else: | 116 else: |
123 from logging.handlers import TimedRotatingFileHandler | 117 from logging.handlers import TimedRotatingFileHandler |
124 handler = TimedRotatingFileHandler( | 118 handler = TimedRotatingFileHandler( |
125 logfile, **rotation_parameters) | 119 logfile, **rotation_parameters) |
126 except IOError: | 120 except IOError: |
127 handler = logging.StreamHandler() | 121 handler = logging.StreamHandler() |
128 return handler | 122 return handler |
129 | 123 |
130 def get_threshold(debug=False, logthreshold=None): | 124 def get_threshold(debug=False, logthreshold=None): |
131 if logthreshold is None: | 125 if logthreshold is None: |
132 if debug: | 126 if debug: |
133 logthreshold = logging.DEBUG | 127 logthreshold = logging.DEBUG |
134 else: | 128 else: |
135 logthreshold = logging.ERROR | 129 logthreshold = logging.ERROR |
136 elif isinstance(logthreshold, string_types): | 130 elif isinstance(logthreshold, basestring): |
137 logthreshold = getattr(logging, THRESHOLD_MAP.get(logthreshold, | 131 logthreshold = getattr(logging, THRESHOLD_MAP.get(logthreshold, |
138 logthreshold)) | 132 logthreshold)) |
139 return logthreshold | 133 return logthreshold |
140 | 134 |
141 def _colorable_terminal(): | 135 def get_formatter(logformat=LOG_FORMAT, logdateformat=LOG_DATE_FORMAT): |
142 isatty = hasattr(sys.__stdout__, 'isatty') and sys.__stdout__.isatty() | 136 isatty = hasattr(sys.__stdout__, 'isatty') and sys.__stdout__.isatty() |
143 if not isatty: | 137 if isatty and sys.platform != 'win32': |
144 return False | |
145 if os.name == 'nt': | |
146 try: | |
147 from colorama import init as init_win32_colors | |
148 except ImportError: | |
149 return False | |
150 init_win32_colors() | |
151 return True | |
152 | |
153 def get_formatter(logformat=LOG_FORMAT, logdateformat=LOG_DATE_FORMAT): | |
154 if _colorable_terminal(): | |
155 fmt = ColorFormatter(logformat, logdateformat) | 138 fmt = ColorFormatter(logformat, logdateformat) |
156 def col_fact(record): | 139 def col_fact(record): |
157 if 'XXX' in record.message: | 140 if 'XXX' in record.message: |
158 return 'cyan' | 141 return 'cyan' |
159 if 'kick' in record.message: | 142 if 'kick' in record.message: |
160 return 'red' | 143 return 'red' |
161 fmt.colorfilters.append(col_fact) | 144 fmt.colorfilters.append(col_fact) |
162 else: | 145 else: |
163 fmt = logging.Formatter(logformat, logdateformat) | 146 fmt = logging.Formatter(logformat, logdateformat) |
164 return fmt | 147 return fmt |
(...skipping 21 matching lines...) Expand all Loading... |
186 # map logilab.common.logger thresholds to logging thresholds | 169 # map logilab.common.logger thresholds to logging thresholds |
187 THRESHOLD_MAP = {'LOG_DEBUG': 'DEBUG', | 170 THRESHOLD_MAP = {'LOG_DEBUG': 'DEBUG', |
188 'LOG_INFO': 'INFO', | 171 'LOG_INFO': 'INFO', |
189 'LOG_NOTICE': 'INFO', | 172 'LOG_NOTICE': 'INFO', |
190 'LOG_WARN': 'WARNING', | 173 'LOG_WARN': 'WARNING', |
191 'LOG_WARNING': 'WARNING', | 174 'LOG_WARNING': 'WARNING', |
192 'LOG_ERR': 'ERROR', | 175 'LOG_ERR': 'ERROR', |
193 'LOG_ERROR': 'ERROR', | 176 'LOG_ERROR': 'ERROR', |
194 'LOG_CRIT': 'CRITICAL', | 177 'LOG_CRIT': 'CRITICAL', |
195 } | 178 } |
OLD | NEW |