我需要根据变量的值更改日志记录级别。例如:
if accel >= 10.0:
log_func = logging.critical # Critical for splat event
elif accel >= 2.0:
log_func = logging.warning # Warning for high-G
elif accel >= 1.0:
log_func = logging.info # Info for normal
else:
log_func = logging.debug # Debug otherwise
log_func("Collision with %0.1fG impact.", accel)
我的代码中都有这样的实例。有没有更好的办法?