Glad it works!
Not sure if it’s required but I added
You’re right. Clearing the log handlers is probably a good idea to avoid duplicates due to script reruns.
PS How is the logging module writing to the console without using stdout (or stderr)? I’d always assumed that was the only mechanism to write to the console?
It’s writing to stderr
. The StreamHandler
in Python’s logging module sends logs to streams like stderr
or stdout
or even to files, if configured to do so. When you create a logger and don’t specifically configure its handlers, it uses stderr
. This is why you see log messages in the console, even though you’re not explicitly writing to stdout
. Here’s where that happens in Streamlit’s logging module:
By default, the StreamHandler
in Python logging writes to stderr
and not stdout
.
This means that even though the log messages are displayed in the console, they are being sent via stderr
.
In Streamlit’s case, each logger is configured with this StreamHandler
and a specific format, directing its output to stderr
. This is why your initial approach with redirecting stdout
did not capture these log messages, as they were not being routed through stdout
. Additionally, Streamlit loggers do not propagate their log messages to the root logger (logger.propagate = False
). This means that any configuration done on the root logger (like redirecting stdout
) does not affect Streamlit’s loggers.