lsst.log  13.0-2-g15de9a1+4
 All Classes Namespaces Files Functions Variables Macros Pages
logContinued.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2013 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 import logging
26 import inspect
27 import os
28 
29 from lsst.utils import continueClass
30 
31 from .log import Log
32 
33 TRACE = 5000
34 DEBUG = 10000
35 INFO = 20000
36 WARN = 30000
37 ERROR = 40000
38 FATAL = 50000
39 
40 @continueClass
41 class Log:
42  def trace(self, fmt, *args):
43  self._log(Log.TRACE, False, fmt, *args)
44 
45  def debug(self, fmt, *args):
46  self._log(Log.DEBUG, False, fmt, *args)
47 
48  def info(self, fmt, *args):
49  self._log(Log.INFO, False, fmt, *args)
50 
51  def warn(self, fmt, *args):
52  self._log(Log.WARN, False, fmt, *args)
53 
54  def error(self, fmt, *args):
55  self._log(Log.ERROR, False, fmt, *args)
56 
57  def fatal(self, fmt, *args):
58  self._log(Log.FATAL, False, fmt, *args)
59 
60  def tracef(self, fmt, *args, **kwargs):
61  self._log(Log.TRACE, True, fmt, *args, **kwargs)
62 
63  def debugf(self, fmt, *args, **kwargs):
64  self._log(Log.DEBUG, True, fmt, *args, **kwargs)
65 
66  def infof(self, fmt, *args, **kwargs):
67  self._log(Log.INFO, True, fmt, *args, **kwargs)
68 
69  def warnf(self, fmt, *args, **kwargs):
70  self._log(Log.WARN, True, fmt, *args, **kwargs)
71 
72  def errorf(self, fmt, *args, **kwargs):
73  self._log(Log.ERROR, True, fmt, *args, **kwargs)
74 
75  def fatalf(self, fmt, *args, **kwargs):
76  self._log(Log.FATAL, True, fmt, *args, **kwargs)
77 
78  def _log(self, level, use_format, fmt, *args, **kwargs):
79  if self.isEnabledFor(level):
80  frame = inspect.currentframe().f_back # calling method
81  frame = frame.f_back # original log location
82  filename = os.path.split(frame.f_code.co_filename)[1]
83  funcname = inspect.stack()[2][3]
84  if use_format:
85  msg = fmt.format(*args, **kwargs) if args or kwargs else fmt
86  else:
87  msg = fmt % args if args else fmt
88  self.logMsg(level, filename, funcname, frame.f_lineno, msg)
89 
90 
91 # Export static functions from Log class to module namespace
92 
93 
94 def configure(*args):
95  Log.configure(*args)
96 
97 
98 def configure_prop(properties):
99  Log.configure_prop(properties)
100 
101 
103  return Log.getDefaultLoggerName()
104 
105 
106 def pushContext(name):
107  Log.pushContext(name)
108 
109 
111  Log.popContext()
112 
113 
114 def MDC(key, value):
115  Log.MDC(key, str(value))
116 
117 
118 def MDCRemove(key):
119  Log.MDCRemove(key)
120 
121 
122 def MDCRegisterInit(func):
123  Log.MDCRegisterInit(func)
124 
125 
126 def setLevel(loggername, level):
127  Log.getLogger(loggername).setLevel(level)
128 
129 
130 def getLevel(loggername):
131  Log.getLogger(loggername).getLevel()
132 
133 
134 def isEnabledFor(logger, level):
135  Log.getLogger(logger).isEnabledFor(level)
136 
137 
138 def log(loggername, level, fmt, *args, **kwargs):
139  Log.getLogger(loggername)._log(level, False, fmt, *args)
140 
141 
142 def trace(fmt, *args):
143  Log.getDefaultLogger()._log(TRACE, False, fmt, *args)
144 
145 
146 def debug(fmt, *args):
147  Log.getDefaultLogger()._log(DEBUG, False, fmt, *args)
148 
149 
150 def info(fmt, *args):
151  Log.getDefaultLogger()._log(INFO, False, fmt, *args)
152 
153 
154 def warn(fmt, *args):
155  Log.getDefaultLogger()._log(WARN, False, fmt, *args)
156 
157 
158 def error(fmt, *args):
159  Log.getDefaultLogger()._log(ERROR, False, fmt, *args)
160 
161 
162 def fatal(fmt, *args):
163  Log.getDefaultLogger()._log(FATAL, False, fmt, *args)
164 
165 
166 def logf(loggername, level, fmt, *args, **kwargs):
167  Log.getLogger(loggername)._log(level, True, fmt, *args, **kwargs)
168 
169 
170 def tracef(fmt, *args, **kwargs):
171  Log.getDefaultLogger()._log(TRACE, True, fmt, *args, **kwargs)
172 
173 
174 def debugf(fmt, *args, **kwargs):
175  Log.getDefaultLogger()._log(DEBUG, True, fmt, *args, **kwargs)
176 
177 
178 def infof(fmt, *args, **kwargs):
179  Log.getDefaultLogger()._log(INFO, True, fmt, *args, **kwargs)
180 
181 
182 def warnf(fmt, *args, **kwargs):
183  Log.getDefaultLogger()._log(WARN, True, fmt, *args, **kwargs)
184 
185 
186 def errorf(fmt, *args, **kwargs):
187  Log.getDefaultLogger()._log(ERROR, True, fmt, *args, **kwargs)
188 
189 
190 def fatalf(fmt, *args, **kwargs):
191  Log.getDefaultLogger()._log(FATAL, True, fmt, *args, **kwargs)
192 
193 
194 def lwpID():
195  return Log.lwpID
196 
197 
198 class LogContext(object):
199  """Context manager for logging."""
200 
201  def __init__(self, name=None, level=None):
202  self.name = name
203  self.level = level
204 
205  def __enter__(self):
206  self.open()
207  return self
208 
209  def __exit__(self, type, value, traceback):
210  self.close()
211 
212  def __del__(self):
213  self.close()
214 
215  def open(self):
216  if self.name is not None:
217  Log.pushContext(self.name)
218  if self.level is not None:
219  Log.getDefaultLogger().setLevel(self.level)
220 
221  def close(self):
222  if self.name is not None:
223  Log.popContext()
224  self.name = None
225 
226  def setLevel(self, level):
227  Log.getDefaultLogger().setLevel(level)
228 
229  def getLevel(self):
230  return Log.getDefaultLogger().getLevel()
231 
232  def isEnabledFor(self, level):
233  return Log.getDefaultLogger().isEnabledFor(level)
234 
235 
236 class LogHandler(logging.Handler):
237  """Handler for Python logging module that emits to LSST logging."""
238 
239  def __init__(self, name=None, level=None):
240  self.context = LogContext(name=name, level=level)
241  self.context.open()
242  logging.Handler.__init__(self)
243 
244  def __del__(self):
245  self.close()
246 
247  def close(self):
248  if self.context is not None:
249  self.context.close()
250  self.context = None
251  logging.Handler.close(self)
252 
253  def handle(self, record):
254  if self.context.isEnabledFor(self.translateLevel(record.levelno)):
255  logging.Handler.handle(self, record)
256 
257  def emit(self, record):
258  Log.getLogger(record.name).logMsg(self.translateLevel(record.levelno),
259  record.filename, record.funcName,
260  record.lineno,
261  record.msg % record.args)
262 
263  def translateLevel(self, levelno):
264  """
265  Translates from standard python logging module levels
266  to standard log4cxx levels.
267  """
268  return levelno*1000
269