Skip to content

Commit 66a028d

Browse files
authored
feat: implement a debug setting for the client (#1081)
* refactor: give the bot an overrideable logging configuration * credits * ref: change to debug setting * refactor: requested changes
1 parent 4ad132c commit 66a028d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

interactions/client/bot.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import contextlib
2+
import logging
23
import re
34
import sys
45
from asyncio import AbstractEventLoop, CancelledError, get_event_loop, iscoroutinefunction
56
from functools import wraps
67
from importlib import import_module
78
from importlib.util import resolve_name
89
from inspect import getmembers
9-
from logging import Logger
1010
from types import ModuleType
1111
from typing import Any, Callable, Coroutine, Dict, List, Optional, Tuple, Union
1212

@@ -27,7 +27,7 @@
2727
from .models.command import ApplicationCommand, Choice, Command, Option
2828
from .models.component import Button, Modal, SelectMenu
2929

30-
log: Logger = get_logger("client")
30+
log: logging.Logger = get_logger("client")
3131

3232
__all__ = (
3333
"Client",
@@ -58,6 +58,8 @@ class Client:
5858
:type default_scope?: Optional[Union[int, Guild, List[int], List[Guild]]]
5959
:param disable_sync?: Controls whether synchronization in the user-facing API should be automatic or not.
6060
:type disable_sync?: Optional[bool]
61+
:param logging?: Set to ``True`` to enable debug logging or set to a log level to use a specific level
62+
:type logging?: Optional[Union[bool, logging.DEBUG, logging.INFO, logging.NOTSET, logging.WARNING, logging.ERROR, logging.CRITICAL]]
6163
6264
:ivar AbstractEventLoop _loop: The asynchronous event loop of the client.
6365
:ivar HTTPClient _http: The user-facing HTTP connection to the Web API, as its own separate client.
@@ -102,6 +104,21 @@ def __init__(
102104
]
103105
self._default_scope = convert_list(int)(self._default_scope)
104106

107+
if _logging := kwargs.get("logging"):
108+
109+
# thx i0 for posting this on the retux Discord
110+
111+
if _logging is True:
112+
_logging = logging.DEBUG
113+
114+
_format = (
115+
"%(asctime)s [%(levelname)s] - .%(funcName)s(): %(message)s"
116+
if _logging == logging.DEBUG
117+
else "%(asctime)s [%(levelname)s] - %(message)s"
118+
)
119+
120+
logging.basicConfig(format=_format, level=_logging)
121+
105122
if kwargs.get("disable_sync"):
106123
self._automate_sync = False
107124
log.warning(

0 commit comments

Comments
 (0)