Multiple log captures when I add log after job completed

I run streamlit app in deployed server (AWS), and I added the log file for checking user’s activity like login, file-upload, and download.

But it captures multiple log like below.

The same activity is accumulating multiple times in my log csv file and the logs are off by a few seconds even if I login just one time.

Is this the problem of the pages what I made? Since I made the pages for login and main page for translation and after user login, it goes to the main app page, so I wonder that it captures login multiple time.

from utils.csv_logger import CSVLogger
csvlogger = CSVLogger("log.csv")

if st.session_state["authentication_status"]:
    csvlogger.log(st.session_state["username"], 'login', '1','')
    logger.warning(f'User {st.session_state["username"]} has logged in.')
    app.main()
    st.sidebar.markdown(f"###### Currently logged in as: {st.session_state['name']}")
elif st.session_state["authentication_status"] is False:
    st.error('Wrong ID or Password. Please log in again.')
    logger.error(f'User {st.session_state["username"]} failed to log in.')
    csvlogger.log(st.session_state["username"], 'login', '0','')

My environment is

  • Streamlit version 1.21.0
  • Python 3.10

Streamlit reruns with every user interaction. If you put your main function nested inside an authentication check like that, it’s going to run that logging command when the user logs in, and then again every time the user clicks on anything.

I don’t know where or how you initialize st.session_state["authentication_status"], but I recommend executing your logging command only at the point where that value is updated to True.

1 Like