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)
137 Log.configure_prop(properties)
141 return Log.getDefaultLoggerName()
145 Log.pushContext(name)
153 Log.MDC(key,
str(value))
161 Log.MDCRegisterInit(func)
165 Log.getLogger(loggername).
setLevel(level)
169 Log.getLogger(loggername).
getLevel()
176 def log(loggername, level, fmt, *args, **kwargs):
177 Log.getLogger(loggername)._log(level,
False, fmt, *args)
181 Log.getDefaultLogger()._log(TRACE,
False, fmt, *args)
185 Log.getDefaultLogger()._log(DEBUG,
False, fmt, *args)
189 Log.getDefaultLogger()._log(INFO,
False, fmt, *args)
193 Log.getDefaultLogger()._log(WARN,
False, fmt, *args)
201 Log.getDefaultLogger()._log(ERROR,
False, fmt, *args)
205 Log.getDefaultLogger()._log(FATAL,
False, fmt, *args)
208 def logf(loggername, level, fmt, *args, **kwargs):
209 Log.getLogger(loggername)._log(level,
True, fmt, *args, **kwargs)
213 Log.getDefaultLogger()._log(TRACE,
True, fmt, *args, **kwargs)
217 Log.getDefaultLogger()._log(DEBUG,
True, fmt, *args, **kwargs)
221 Log.getDefaultLogger()._log(INFO,
True, fmt, *args, **kwargs)
225 Log.getDefaultLogger()._log(WARN,
True, fmt, *args, **kwargs)
229 Log.getDefaultLogger()._log(ERROR,
True, fmt, *args, **kwargs)
233 Log.getDefaultLogger()._log(FATAL,
True, fmt, *args, **kwargs)
241 Log.usePythonLogging()
245 Log.doNotUsePythonLogging()
249 """Context manager for logging.""" 266 if self.
name is not None:
267 Log.pushContext(self.
name)
268 if self.
level is not None:
272 if self.
name is not None:
277 Log.getDefaultLogger().
setLevel(level)
280 return Log.getDefaultLogger().
getLevel()
287 """Context manager to enable Python log forwarding temporarily.""" 293 Log.usePythonLogging()
295 def __exit__(self, exc_type, exc_value, traceback):
296 Log.UsePythonLogging = self.
current 300 """Helper class to translate levels between ``lsst.log`` and Python 305 """Translates from lsst.log/log4cxx levels to `logging` module levels. 310 Logging level number used by `lsst.log`, typically one of the 311 constants defined in this module (`DEBUG`, `INFO`, etc.) 316 Correspoding logging level number for Python `logging` module. 325 """Translates from standard python `logging` module levels to 326 lsst.log/log4cxx levels. 331 Logging level number used by Python `logging`, typically one of 332 the constants defined by `logging` module (`logging.DEBUG`, 333 `logging.INFO`, etc.) 338 Correspoding logging level number for `lsst.log` module. 344 """Handler for Python logging module that emits to LSST logging. 348 If this handler is enabled and `lsst.log` has been configured to use 349 Python `logging`, the handler will do nothing itself if any other 350 handler has been registered with the Python logger. If it does not 351 think that anything else is handling the message it will attempt to 352 send the message via a default `~logging.StreamHandler`. The safest 353 approach is to configure the logger with an additional handler 354 (possibly the ROOT logger) if `lsst.log` is to be configured to use 359 logging.Handler.__init__(self, level=level)
365 logger = Log.getLogger(record.name)
366 if logger.isEnabledFor(LevelTranslator.logging2lsstLog(record.levelno)):
367 logging.Handler.handle(self, record)
370 if Log.UsePythonLogging:
376 pylgr = logging.getLogger(record.name)
380 if any(
not isinstance(h, self.__class__)
for h
in pylgr.handlers):
386 if pylgr.parent
and pylgr.parent.hasHandlers()
and pylgr.propagate:
392 stream = logging.StreamHandler()
393 stream.setFormatter(logging.Formatter(fmt=
"%(name)s %(levelname)s (fallback): %(message)s"))
394 stream.handle(record)
397 logger = Log.getLogger(record.name)
401 logger.logMsg(LevelTranslator.logging2lsstLog(record.levelno),
402 record.filename, record.funcName,
403 record.lineno, message)
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 setLevel(self, level)
def __init__(self, level=logging.NOTSET)
def MDCRegisterInit(func)
def __exit__(self, type, value, traceback)
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 __init__(self, name=None, level=None)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
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 isEnabledFor(self, level)
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 getDefaultLoggerName()
def doNotUsePythonLogging()
def debugf(fmt, args, kwargs)