Skip to content

Importing streamlit adds StreamHandler to root logger #1248

@plonerma

Description

@plonerma

Problem

When importing streamlit (without calling it via the cli), a StreamHandler is registered to the root logger. In my current project, I import Streamlit, but the script is not always executed as streamlit app - when running headless, it is not used.

import logging
print(logging.getLogger().handlers)  # outputs '[]'

import streamlit
print(logging.getLogger().handlers)  # now outputs '[<StreamHandler <stderr> (NOTSET)>]'

In my opinion, no handler should be registerd when the library is just imported (should only happen when the script is executed via streamlit).

Solution

Would it be possible to only add the handler when the script is called via the streamlit run command?

Additional context

Since I am beginner using logging as well as streamlit, I am not sure whether this is in fact the intended behavior, but I did not expect the import of streamlit to change logging settings.

Version 0.56.0

$ pip freeze | grep streamlit
streamlit==0.56.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:enhancementRequests for feature enhancements or new features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions