43 UsePythonLogging =
False 44 """Forward Python `lsst.log` messages to Python `logging` package.""" 48 """Forward log messages to Python `logging` 52 This is useful for unit testing when you want to ensure 53 that log messages are captured by the testing environment 54 as distinct from standard output. 56 This state only affects messages sent to the `lsst.log` 63 """Forward log messages to LSST logging system. 67 This is the default state. 72 self.
_log(Log.TRACE,
False, fmt, *args)
75 self.
_log(Log.DEBUG,
False, fmt, *args)
77 def info(self, fmt, *args):
78 self.
_log(Log.INFO,
False, fmt, *args)
80 def warn(self, fmt, *args):
81 self.
_log(Log.WARN,
False, fmt, *args)
87 self.
_log(Log.ERROR,
False, fmt, *args)
90 self.
_log(Log.FATAL,
False, fmt, *args)
92 def tracef(self, fmt, *args, **kwargs):
93 self.
_log(Log.TRACE,
True, fmt, *args, **kwargs)
95 def debugf(self, fmt, *args, **kwargs):
96 self.
_log(Log.DEBUG,
True, fmt, *args, **kwargs)
98 def infof(self, fmt, *args, **kwargs):
99 self.
_log(Log.INFO,
True, fmt, *args, **kwargs)
101 def warnf(self, fmt, *args, **kwargs):
102 self.
_log(Log.WARN,
True, fmt, *args, **kwargs)
105 self.
_log(Log.ERROR,
True, fmt, *args, **kwargs)
108 self.
_log(Log.FATAL,
True, fmt, *args, **kwargs)
110 def _log(self, level, use_format, fmt, *args, **kwargs):
111 if self.isEnabledFor(level):
112 frame = inspect.currentframe().f_back
114 filename = os.path.split(frame.f_code.co_filename)[1]
115 funcname = frame.f_code.co_name
117 msg = fmt.format(*args, **kwargs)
if args
or kwargs
else fmt
119 msg = fmt % args
if args
else fmt
121 pylog = logging.getLogger(self.getName())
122 record = logging.LogRecord(self.getName(), LevelTranslator.lsstLog2logging(level),
123 filename, frame.f_lineno, msg,
None,
False, func=funcname)
126 self.logMsg(level, filename, funcname, frame.f_lineno, msg)
129 """Implement pickle support. 131 args = (self.getName(), )
133 return (getLogger, args)
143 Log.configure_prop(properties)
147 return Log.getDefaultLogger()
151 return Log.getLogger(loggername)
155 Log.MDC(key, str(value))
163 Log.MDCRegisterInit(func)
167 Log.getLogger(loggername).
setLevel(level)
171 Log.getLogger(loggername).
getLevel()
178 def log(loggername, level, fmt, *args, **kwargs):
179 Log.getLogger(loggername)._log(level,
False, fmt, *args)
183 Log.getDefaultLogger()._log(TRACE,
False, fmt, *args)
187 Log.getDefaultLogger()._log(DEBUG,
False, fmt, *args)
191 Log.getDefaultLogger()._log(INFO,
False, fmt, *args)
195 Log.getDefaultLogger()._log(WARN,
False, fmt, *args)
203 Log.getDefaultLogger()._log(ERROR,
False, fmt, *args)
207 Log.getDefaultLogger()._log(FATAL,
False, fmt, *args)
210 def logf(loggername, level, fmt, *args, **kwargs):
211 Log.getLogger(loggername)._log(level,
True, fmt, *args, **kwargs)
215 Log.getDefaultLogger()._log(TRACE,
True, fmt, *args, **kwargs)
219 Log.getDefaultLogger()._log(DEBUG,
True, fmt, *args, **kwargs)
223 Log.getDefaultLogger()._log(INFO,
True, fmt, *args, **kwargs)
227 Log.getDefaultLogger()._log(WARN,
True, fmt, *args, **kwargs)
231 Log.getDefaultLogger()._log(ERROR,
True, fmt, *args, **kwargs)
235 Log.getDefaultLogger()._log(FATAL,
True, fmt, *args, **kwargs)
243 Log.usePythonLogging()
247 Log.doNotUsePythonLogging()
251 """Context manager to enable Python log forwarding temporarily.""" 257 Log.usePythonLogging()
259 def __exit__(self, exc_type, exc_value, traceback):
260 Log.UsePythonLogging = self.
current 264 """Helper class to translate levels between ``lsst.log`` and Python 269 """Translates from lsst.log/log4cxx levels to `logging` module levels. 274 Logging level number used by `lsst.log`, typically one of the 275 constants defined in this module (`DEBUG`, `INFO`, etc.) 280 Correspoding logging level number for Python `logging` module. 289 """Translates from standard python `logging` module levels to 290 lsst.log/log4cxx levels. 295 Logging level number used by Python `logging`, typically one of 296 the constants defined by `logging` module (`logging.DEBUG`, 297 `logging.INFO`, etc.) 302 Correspoding logging level number for `lsst.log` module. 308 """Handler for Python logging module that emits to LSST logging. 312 If this handler is enabled and `lsst.log` has been configured to use 313 Python `logging`, the handler will do nothing itself if any other 314 handler has been registered with the Python logger. If it does not 315 think that anything else is handling the message it will attempt to 316 send the message via a default `~logging.StreamHandler`. The safest 317 approach is to configure the logger with an additional handler 318 (possibly the ROOT logger) if `lsst.log` is to be configured to use 323 logging.Handler.__init__(self, level=level)
329 logger = Log.getLogger(record.name)
330 if logger.isEnabledFor(LevelTranslator.logging2lsstLog(record.levelno)):
331 logging.Handler.handle(self, record)
334 if Log.UsePythonLogging:
340 pylgr = logging.getLogger(record.name)
344 if any(
not isinstance(h, self.__class__)
for h
in pylgr.handlers):
350 if pylgr.parent
and pylgr.parent.hasHandlers()
and pylgr.propagate:
356 stream = logging.StreamHandler()
357 stream.setFormatter(logging.Formatter(fmt=
"%(name)s %(levelname)s (fallback): %(message)s"))
358 stream.handle(record)
361 logger = Log.getLogger(record.name)
365 logger.logMsg(LevelTranslator.logging2lsstLog(record.levelno),
366 record.filename, record.funcName,
367 record.lineno, message)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def debug(self, fmt, args)
def trace(self, fmt, args)
def infof(fmt, args, kwargs)
def errorf(fmt, args, kwargs)
def errorf(self, fmt, args, kwargs)
def lsstLog2logging(level)
def __init__(self, level=logging.NOTSET)
def MDCRegisterInit(func)
bool any(CoordinateExpr< N > const &expr) noexcept
Return true if any elements are true.
def fatalf(fmt, args, kwargs)
def debugf(self, fmt, args, kwargs)
def _log(self, level, use_format, fmt, args, kwargs)
def __exit__(self, exc_type, exc_value, traceback)
def fatal(self, fmt, args)
def tracef(self, fmt, args, kwargs)
def warn(self, fmt, args)
def setLevel(loggername, level)
def logging2lsstLog(level)
def warnf(fmt, args, kwargs)
def infof(self, fmt, args, kwargs)
def warnf(self, fmt, args, kwargs)
def fatalf(self, fmt, args, kwargs)
def log(loggername, level, fmt, args, kwargs)
def isEnabledFor(logger, level)
def doNotUsePythonLogging(cls)
def usePythonLogging(cls)
def logf(loggername, level, fmt, args, kwargs)
def error(self, fmt, args)
def info(self, fmt, args)
def tracef(fmt, args, kwargs)
def warning(self, fmt, args)
def configure_prop(properties)
def doNotUsePythonLogging()
def debugf(fmt, args, kwargs)
def getLogger(loggername)