Streamlit duplicates log messages when stream handler is added

When I add a stream handler to my logger object (so that I can do formatting), streamlit then outputs 2 messages (one formatted and one not)

Any ideas for where I can look to fix this?

Here’s a sample of what’s happening.

test.py:

import logging
logger = logging.getLogger()
logger.setLevel('INFO')
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s-%(name)s-%(levelname)s>>>%(message)s', "%H:%M:%S")
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.info('Start of program')

Now if I run that using python test.py I get the following:

11:24:31-root-INFO>>>Start of program

Which is what i’d expect

But if I run it using streamlit run test.py I get:
2021-08-24 11:24:58.198 Start of program
11:24:58-root-INFO>>>Start of program

There’s a duplicate, unformatted message.

How can I resolve this, so that there is only one (formatted) message?


(NOTE: FileHandler() objects have the expected behavior. I.e. if you add

fh = logging.FileHandler('test.log', 'w')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)

the output in the log file test.log is as expected. One, properly formatted message, no duplicates)

1 Like

I’m seeing the same behavior. Any progress on this?

Actually, it appears to only duplicate logs when I start the server initially. My guess is that it performs a refresh right at the start which would explain the duplication.