Skip to content

Commit

Permalink
Setup basic handler if no handlers are defined in dictConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
davfsa committed Oct 7, 2021
1 parent 491b882 commit da83d4e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/832.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Setup basic handler if no handlers are defined in favour passed to `logging.config.dictConfig`
3 changes: 2 additions & 1 deletion hikari/impl/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ class GatewayBot(traits.GatewayBotAware):
If a `typing.Dict[str, typing.Any]` equivalent, then this value is
passed to `logging.config.dictConfig` to allow the user to provide a
specialized logging configuration of their choice.
specialized logging configuration of their choice. If any handlers are
defined in the dict, default handlers will not be setup.
As a side note, you can always opt to leave this on the default value
and then use an incremental `logging.config.dictConfig` that applies
Expand Down
3 changes: 2 additions & 1 deletion hikari/impl/rest_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ class RESTBot(traits.RESTBotAware, interaction_server_.InteractionServer):
If a `typing.Dict[str, typing.Any]` equivalent, then this value is
passed to `logging.config.dictConfig` to allow the user to provide a
specialized logging configuration of their choice.
specialized logging configuration of their choice. If any handlers are
defined in the dict, default handlers will not be setup.
As a side note, you can always opt to leave this on the default value
and then use an incremental `logging.config.dictConfig` that applies
Expand Down
11 changes: 9 additions & 2 deletions hikari/internal/ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def init_logging(
to `sys.stderr` using this configuration.
If you pass a `builtins.dict`, it is treated as the mapping to pass to
`logging.config.dictConfig`.
`logging.config.dictConfig`. If the dict defines any handlers, default
handlers will not be setup.
allow_color : builtins.bool
If `builtins.False`, no colour is allowed. If `builtins.True`, the
output device must be supported for this to return `builtins.True`.
Expand All @@ -112,11 +113,17 @@ def init_logging(

if isinstance(flavor, dict):
logging.config.dictConfig(flavor)
return

if flavor.get("handlers"):
# Handlers are defined => don't configure the default ones
return

flavor = None

# Apparently this makes logging even more efficient!
logging.logThreads = False
logging.logProcesses = False

if supports_color(allow_color, force_color):
colorlog.basicConfig(
level=flavor,
Expand Down
21 changes: 20 additions & 1 deletion tests/hikari/internal/test_ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ def test_when_handlers_specify_not_to_set_up(self):
logging_basic_config.assert_not_called()
colorlog_basic_config.assert_not_called()

def test_when_flavour_is_a_dict(self):
def test_when_flavour_is_a_dict_and_doesnt_define_handlers(self):
stack = contextlib.ExitStack()
stack.enter_context(mock.patch.object(logging, "root", handlers=[]))
stack.enter_context(mock.patch.object(ux, "supports_color", return_value=False))
logging_dict_config = stack.enter_context(mock.patch.object(logging.config, "dictConfig"))
logging_basic_config = stack.enter_context(mock.patch.object(logging, "basicConfig"))
colorlog_basic_config = stack.enter_context(mock.patch.object(colorlog, "basicConfig"))
Expand All @@ -78,6 +79,24 @@ def test_when_flavour_is_a_dict(self):
ux.init_logging({"hikari": {"level": "INFO"}}, True, False)

logging_dict_config.assert_called_once_with({"hikari": {"level": "INFO"}})
logging_basic_config.assert_called_once_with(
level=None,
format="%(levelname)-1.1s %(asctime)23.23s %(name)s: %(message)s",
stream=sys.stderr,
)
colorlog_basic_config.assert_not_called()

def test_when_flavour_is_a_dict_and_defines_handlers(self):
stack = contextlib.ExitStack()
stack.enter_context(mock.patch.object(logging, "root", handlers=[]))
logging_dict_config = stack.enter_context(mock.patch.object(logging.config, "dictConfig"))
logging_basic_config = stack.enter_context(mock.patch.object(logging, "basicConfig"))
colorlog_basic_config = stack.enter_context(mock.patch.object(colorlog, "basicConfig"))

with stack:
ux.init_logging({"hikari": {"level": "INFO"}, "handlers": {"some_handler": {}}}, True, False)

logging_dict_config.assert_called_once_with({"hikari": {"level": "INFO"}, "handlers": {"some_handler": {}}})
logging_basic_config.assert_not_called()
colorlog_basic_config.assert_not_called()

Expand Down

0 comments on commit da83d4e

Please sign in to comment.