Automatic st.write Logging

I’ve been looking for a way to do some logging to reflect what I see in my streamlit GUI and am wondering if the ability to do so has been considered for functions like st.write, st.success, st.error, st.warning, etc.?

In my code I do a lot of this…

        txt = f"Sorry, we can't write to your Google Sheet if you don't select one for processing."
        st.warning(txt)
        state('logger').warning(txt)

I’m using loguru and I’ve put the logger into an st.session_state variable called ‘logger’ so I can easily access it and control it throughout my app. This is working nicely, but I’m wondering if there’s a better, more concise way to do this, or maybe a custom component that can help?

Can you help me better understand your goal? Is it to have logging present in both the app ui as well as the terminal you ran the app in?

Yes, Nico and thanks for responding. My real goal was to capture most of my GUI output in a log file and I’ve achieved that with the simple three lines of code I shared. I have about 30 instances of that block in my app now and it’s working nicely.

I was just thinking that capability would be a nice addition to all of the st.write, st.error, st.warning, etc. functions. I might still create a wrapper function (or a handful of them) to replace those three-line blocks if there is no better way.

If logging were to be added to the existing st.* functions I was thinking an optional argument holding the logger handle would do the trick nicely. Thanks.

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