1616 from logging import Handler , Logger
1717 from typing import Final
1818
19- from ._schema import LoggingSettings
19+ from ._schema import (
20+ DiscordAPILoggingSettings ,
21+ DiscordChannelLoggingSettings ,
22+ LoggingSettings ,
23+ )
2024
2125
2226__all__ : "Sequence[str]" = ("DISCORD_LOGGER_NAME" , "LOGGER_NAME" , "apply_logging_settings" )
3640discord_logger : "Final[Logger]" = logging .getLogger (DISCORD_LOGGER_NAME )
3741
3842
39- def _remove_handlers_of_type (target_logger : "Logger" , handler_type : type ) -> None :
40- """Remove every handler of the given type from the given logger."""
43+ def _remove_handlers_of_type (
44+ target_logger : "Logger" , handler_type : type , * , excluding : type | None = None
45+ ) -> None :
46+ """
47+ Remove every handler of the given type from the given logger.
48+
49+ A subtype of that type may be excluded, because the logging module's own handler
50+ hierarchy does not match the destinations they write to: `FileHandler` subclasses
51+ `StreamHandler`, so removing every stream handler would otherwise also remove the
52+ file handler that a different destination's settings are responsible for.
53+ """
4154 existing_handler : Handler
4255 for existing_handler in tuple (target_logger .handlers ):
43- if isinstance (existing_handler , handler_type ):
56+ HANDLER_IS_EXCLUDED : bool = excluding is not None and isinstance (
57+ existing_handler , excluding
58+ )
59+ if isinstance (existing_handler , handler_type ) and not HANDLER_IS_EXCLUDED :
4460 target_logger .removeHandler (existing_handler )
4561 existing_handler .close ()
4662
@@ -49,7 +65,7 @@ def _apply_console_logging_settings(logging_settings: "LoggingSettings") -> None
4965 """Set up logging to the console output stream."""
5066 # NOTE: Handlers are replaced rather than reconfigured in place, so that applying
5167 # settings repeatedly (upon every reload) cannot accumulate duplicate handlers.
52- _remove_handlers_of_type (logger , logging .StreamHandler )
68+ _remove_handlers_of_type (logger , logging .StreamHandler , excluding = logging . FileHandler )
5369
5470 console_logging_handler : Handler = logging .StreamHandler ()
5571 console_logging_handler .setFormatter (
@@ -65,7 +81,9 @@ def _apply_discord_channel_logging_settings(logging_settings: "LoggingSettings")
6581 """Set up relaying of error logs to a Discord log channel."""
6682 _remove_handlers_of_type (logger , DiscordHandler )
6783
68- DISCORD_CHANNEL_LOGGING_SETTINGS = logging_settings .discord_channel
84+ DISCORD_CHANNEL_LOGGING_SETTINGS : Final [DiscordChannelLoggingSettings | None ] = (
85+ logging_settings .discord_channel
86+ )
6987 if DISCORD_CHANNEL_LOGGING_SETTINGS is None :
7088 logger .debug (
7189 "No Discord log-channel webhook-URL was set, "
@@ -89,8 +107,15 @@ def _apply_discord_api_logging_settings(logging_settings: "LoggingSettings") ->
89107 """Set up recording of the logs emitted by the Discord API wrapper."""
90108 _remove_handlers_of_type (discord_logger , logging .FileHandler )
91109
92- DISCORD_API_LOGGING_SETTINGS = logging_settings .discord_api
110+ DISCORD_API_LOGGING_SETTINGS : Final [DiscordAPILoggingSettings ] = (
111+ logging_settings .discord_api
112+ )
93113 if not DISCORD_API_LOGGING_SETTINGS .enabled :
114+ # NOTE: The level is restored alongside the handler, so that disabling these logs
115+ # after they have been enabled leaves the Discord API logger exactly as it began.
116+ # Leaving a previously configured level in place would otherwise send every record
117+ # meeting it to the root logger, now that propagation has been turned back on.
118+ discord_logger .setLevel (logging .NOTSET )
94119 discord_logger .propagate = True
95120 return
96121
0 commit comments