slackapi / bolt-python

A framework to build Slack apps using Python
https://slack.dev/bolt-python/
MIT License
1.03k stars 237 forks source link

Why are logging levels explicily set when creating a logger. #1047

Closed Kroppeb closed 4 months ago

Kroppeb commented 4 months ago

https://github.com/slackapi/bolt-python/blob/996f29f0ee741444c5b2a151b2e1930005d91fc2/slack_bolt/logger/__init__.py#L40-L42

Calling logging.getLogger('slack_bolt').setLevel(logging.INFO) or logging.getLogger('slack_bolt').setLevel(logging.INFO) does nothing when the global logging level is set to DEBUG because this library overrides these values for some reason.

seratch commented 4 months ago

Hi @Kroppeb, thanks for writing in! To configure the log-level for the internal loggers, you can pass logger to App constructor: https://slack.dev/bolt-python/api-docs/slack_bolt/app/index.html#slack_bolt.app.App.logger The logger will be used as the base logger internally, so the log level you want to set will be respected for all the internal loggers. This might be a bit surprising to you but we decided to go with this design to support various use cases.

I hope this helps! Let me close this issue now, but whenever you have further to ask, please feel free to write in! :wave:

Kroppeb commented 4 months ago

I find this very strange. It seems to me you are implementing a worse version of the stdlibs logging hierarchy internally?

Is there a way to change the logging level of all the internal loggers you could be creating at once. Calling changing the loglevel, or adding or removing handlers/filters after passing it doesn't seem to get propagated to the internal loggers.

There is a logger.getChild() available, it's weird that it's not being used.

seratch commented 4 months ago

Calling changing the loglevel, or adding or removing handlers/filters after passing it doesn't seem to get propagated to the internal loggers.

The only way to set the log level is to call the setter method before passing it to App constructor. In other words, Changing it does not work after passing it but works before passing it.

base_logger = logging.getLogger("my-bolt-logger")
base_logger.setLevel(logging.DEBUG)

app = App(
    token=os.environ.get("SLACK_BOT_TOKEN"),
    logger=base_logger,
)

This could be a limitation to some scenarios, but it should work for most real-world use cases. We don't have any plans to make changes to this mechanism.

Thanks again for asking the question and the feedback.