pcdsutils.log.log_warning_handler

pcdsutils.log.log_warning_handler(message: Warning, category: type[Warning], filename: str, lineno: int, file: ~typing.TextIO | None = None, line: str | None = None, logger: ~logging.Logger = <Logger pcdsutils.log.warnings (WARNING)>) None[source]

Warning handler that redirects all of the warnings to a logger.

This can be used as a drop-in replacement for warnings.showwarning to redirect unfiltered warnings into the logging stream.

Rather than duplicate the warning display text, this handler opts to simplify it and put the extra details into the “extra” dictionary argument in the logging library.

The warnings module displays the warnings as: filename:lineno: category: messagenline (where, in all cases I’ve seen, “line” is generated by reading the file)

The log message generated here will simply be: category: message

All arguments (except “logger”) will be included in the “extra” dictionary. This means they can be used in log filters without parsing the message. The keys used will be “warning_{key}” for each keyword parameter to this function, to avoid collisions.

Parameters:
messageWarning

This is the Warning object created by a warnings.warn call. When converted using str, this becomes the string message that was passed into warnings.warn. This will be put into the generated log message text and into the extra dict.

categorytype[Warning]

The warning type, e.g. UserWarning, DeprecationWarning, etc. this will be put into the generated log message text and into the extra dict.

filenamestr

The name of the source code file that generated the warning. This will be put into the extra dict.

linenoint

The line number in the file that generated the warning. This will be put into the extra dict.

filefile-like, optional

A file-like object that is normally used in the warnings handler as the destination for warnings, defaulting to sys.stderr. This will be put into the extra dict.

linestr, optional

The string line in the file that generated the warning. I have never seen this passed into the warning handler. This will be put into the extra dict.

loggerLogger, optional

Use this argument to override the default logger for the warnings handler, which is the warnings_logger defined in this module. This is currently the pcdsutils.log.warnings logger.