Streamlit Logging with Python Logger

Hey guys I am experiencing weird behavior in my Streamlit application. I host my own Streamlit application on my own server and I would like to use the python logging library to do so but I am running into some unexpected behaviour.

Whenever I start my app which just consists of the following code:

(menv) PS C:\code > streamlit run .\src\test.py

import logging
from logging import getLogger

import streamlit as st

print("---------------------------")
app_logger = getLogger()
app_logger.addHandler(logging.StreamHandler())
app_logger.setLevel(logging.INFO)
app_logger.info("best")
print("---------------------------")
st.info("Something went wrong please contact us about this error if it persists.")

I get the following output:

  You can now view your Streamlit app in your browser.

  Network URL: http://10.6.2.242:8501/stream
  External URL: http://129.115.2.66:8501/stream

---------------------------
best
---------------------------
---------------------------
best
best
---------------------------
---------------------------
best
best
best
---------------------------

After I refresh my webapage I get this output:

---------------------------
best
best
best
best
---------------------------

Not sure what is going on here but if anyone can help me resolve this I would greatly appreciate it.

Adding here also a mention that I found a post on stack overflow that has some upvotes describing similar behavior

You are adding a handler to the logger on each rerun or refresh.

1 Like

Thank you!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.