You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello. First of all, thank you so much for this awesome library, and pardon me for more theoretical question. As there is no "Discussions" tab, going to ask this here.
In the documentation, the author says:
Here it is implied, that if you use string interpolation instead of f-strings, this would defer rendering text and if log level is higher that the caller's, then text will not be rendered, possibly saving some time on calling expensive function.
However, this is not really like that, as simply using string interpolation will still make the call:
importloggingdefsimple_func():
print("Called!")
return5logging.basicConfig(level=logging.WARNING)
logging.debug(f"Running with number {simple_func()}")
logging.debug("Running with number %s", simple_func())
And it is the same with structlog:
importloggingimportstructlogfromstructlogimportWriteLoggerFactoryfromstructlog.typesimportFilteringBoundLoggerdefget_processors() ->list:
processors= [
structlog.processors.TimeStamper(),
structlog.processors.add_log_level,
structlog.dev.ConsoleRenderer(),
]
returnprocessorsdefget_structlog_config() ->dict:
min_level=logging.INFOreturn {
"processors": get_processors(),
"cache_logger_on_first_use": True,
"wrapper_class": structlog.make_filtering_bound_logger(min_level),
"logger_factory": WriteLoggerFactory()
}
defsimple_func():
print("Called!")
return5logger: FilteringBoundLogger=structlog.get_logger()
structlog.configure(**get_structlog_config())
logger.debug(f"Running with number {simple_func()}", extra="extra_f_string")
logger.debug("Running with number %s", simple_func(), extra="extra_interpolate")
In both examples, there will be two "Called!" strings in the console.
Am I missing something? Is there any other configuration needed to avoid rendering log text when overall log level is higher? Also, what's the analogue of "isEnabledFor" in structlog?
Thanks in advance!
The text was updated successfully, but these errors were encountered:
What you're missing is that normal Python code has no control over over how its function argument values are created. structlog only gets the result of your function call and doesn't know it was created by calling a function.
What the delay means is that the string isn't rendered, based on those arguments unless necessary.
The best I can offer you off-cuff is setting some key to a special value and writing a processor that executes your expensive function based on that.
Hello. First of all, thank you so much for this awesome library, and pardon me for more theoretical question. As there is no "Discussions" tab, going to ask this here.
In the documentation, the author says:
Here it is implied, that if you use string interpolation instead of f-strings, this would defer rendering text and if log level is higher that the caller's, then text will not be rendered, possibly saving some time on calling expensive function.
However, this is not really like that, as simply using string interpolation will still make the call:
And it is the same with structlog:
In both examples, there will be two "Called!" strings in the console.
Am I missing something? Is there any other configuration needed to avoid rendering log text when overall log level is higher? Also, what's the analogue of "isEnabledFor" in structlog?
Thanks in advance!
The text was updated successfully, but these errors were encountered: