Capturing a warning sent using logging.warning() from a library function, python.

huangapple go评论64阅读模式
英文:

Capturing a warning sent using logging.warning() from a library function, python

问题

我调用 Qsc 函数时,对于一些值,我收到以下警告:

logger.warning('Newton solve did not get close to desired tolerance. Final residual: {last_residual_norm}')

当发生这种情况时,我想捕获这个警告,以便我可以以不同的方式处理这些结果。

我希望解决这个问题而不改变库。

英文:

I call the Qsc function with various values, but for some values I receive the following warning:

Newton solve did not get close to desired tolerance. Final residual: 1.7380027083030736e-09

When this happens I want to capture the warning, so I can deal with these results differently.

I would like to solve the problem without changing the library.

This is the where the warning is sent from: logger.warning('Newton solve did not get close to desired tolerance. 'f'Final residual: {last_residual_norm}'), https://github.com/landreman/pyQSC/blob/1cb01ee6202144f3263150154164e72e74f7082c/qsc/newton.py#L59

I tried this:

import logging
import warnings
from qsc import Qsc

logging.captureWarnings(True)

with warnings.catch_warnings(record=True) as w:
    stel = Qsc(rc=[1, 0.20990165844612937], zs=[0, -0.008780473233288922], nfp=2, etabar=-1.699682124042855)
    warnings.warn("warn from warning")
    logging.warn("warn from logging")
    logging.warning("warning from logging")
    if w:
        for warning in w:
            logging.warning(str(warning.message))
            print("warning was captured\n")

And obtained this, meaning that I am only capturing the warning from warning.warn()

Newton solve did not get close to desired tolerance. Final residual: 1.7380027083030736e-09
WARNING:root:warning from logging
WARNING:root:warn from warning
warning was captured

答案1

得分: 1

你可能将WARNING级别的日志事件(通常使用记录器的warning方法发出)与warnings模块混淆了,后者是不同的东西。因此,你的catch_warnings()不会产生期望的效果。

要捕获警告:向记录器添加一个记录该消息的过滤器或发出该消息的处理程序,这将导致你想要的自定义处理,并从该过滤器中返回False。例如:

def newton_solution_fail_filter(record):
    if not is_newton_failed_message(record):  # 例如,通过检查record.msg
        return True
    custom_function_to_solve_the_problem()  # 执行解决问题的操作
    return False  # 防止消息出现

logger_or_handler.addFilter(newton_solution_fail_filter)
英文:

You may be confusing a logging event at WARNING level (often emitted using the warning method of a logger) with the warnings module, which is a different thing. Hence, your catch_warnings() will not have the desired effect.

To capture the warning: Add a filter to the logger which logs that message or handler which emits the message, which leads to the custom processing you want, and return False from that filter. For example:

def newton_solution_fail_filter(record):
    if not is_newton_failed_message(record):  # e.g. by examing record.msg
        return True
    custom_function_to_solve_the_problem()  # do your thing to solve it
    return False  # prevent the message appearing

logger_or_handler.addFilter(newton_solution_fail_filter)

huangapple
  • 本文由 发表于 2023年5月8日 00:11:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定