mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-08 04:38:42 +00:00
Change stderr prints to use the logging module instead
This commit is contained in:
parent
49e683541b
commit
53685b9b86
@ -23,9 +23,8 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
import logging
|
||||||
import inspect
|
import inspect
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
@ -79,6 +78,8 @@ __all__ = ('CommandTree',)
|
|||||||
|
|
||||||
ClientT = TypeVar('ClientT', bound='Client')
|
ClientT = TypeVar('ClientT', bound='Client')
|
||||||
|
|
||||||
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def _retrieve_guild_ids(
|
def _retrieve_guild_ids(
|
||||||
command: Any, guild: Optional[Snowflake] = MISSING, guilds: Sequence[Snowflake] = MISSING
|
command: Any, guild: Optional[Snowflake] = MISSING, guilds: Sequence[Snowflake] = MISSING
|
||||||
@ -775,8 +776,8 @@ class CommandTree(Generic[ClientT]):
|
|||||||
|
|
||||||
A callback that is called when any command raises an :exc:`AppCommandError`.
|
A callback that is called when any command raises an :exc:`AppCommandError`.
|
||||||
|
|
||||||
The default implementation prints the traceback to stderr if the command does
|
The default implementation logs the exception using the library logger
|
||||||
not have any error handlers attached to it.
|
if the command does not have any error handlers attached to it.
|
||||||
|
|
||||||
To get the command that failed, :attr:`discord.Interaction.command` should
|
To get the command that failed, :attr:`discord.Interaction.command` should
|
||||||
be used.
|
be used.
|
||||||
@ -794,11 +795,9 @@ class CommandTree(Generic[ClientT]):
|
|||||||
if command._has_any_error_handlers():
|
if command._has_any_error_handlers():
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f'Ignoring exception in command {command.name!r}:', file=sys.stderr)
|
_log.error('Ignoring exception in command %r', command.name, exc_info=error)
|
||||||
else:
|
else:
|
||||||
print(f'Ignoring exception in command tree:', file=sys.stderr)
|
_log.error('Ignoring exception in command tree', exc_info=error)
|
||||||
|
|
||||||
traceback.print_exception(error.__class__, error, error.__traceback__, file=sys.stderr)
|
|
||||||
|
|
||||||
def error(self, coro: ErrorFunc) -> ErrorFunc:
|
def error(self, coro: ErrorFunc) -> ErrorFunc:
|
||||||
"""A decorator that registers a coroutine as a local error handler.
|
"""A decorator that registers a coroutine as a local error handler.
|
||||||
|
@ -29,7 +29,6 @@ import datetime
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import traceback
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
AsyncIterator,
|
AsyncIterator,
|
||||||
@ -519,16 +518,16 @@ class Client:
|
|||||||
|
|
||||||
The default error handler provided by the client.
|
The default error handler provided by the client.
|
||||||
|
|
||||||
By default this prints to :data:`sys.stderr` however it could be
|
By default this logs to the library logger however it could be
|
||||||
overridden to have a different implementation.
|
overridden to have a different implementation.
|
||||||
Check :func:`~discord.on_error` for more details.
|
Check :func:`~discord.on_error` for more details.
|
||||||
|
|
||||||
.. versionchanged:: 2.0
|
.. versionchanged:: 2.0
|
||||||
|
|
||||||
``event_method`` parameter is now positional-only.
|
``event_method`` parameter is now positional-only
|
||||||
|
and instead of writing to ``sys.stderr`` it logs instead.
|
||||||
"""
|
"""
|
||||||
print(f'Ignoring exception in {event_method}', file=sys.stderr)
|
_log.exception('Ignoring exception in %s', event_method)
|
||||||
traceback.print_exc()
|
|
||||||
|
|
||||||
# hooks
|
# hooks
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ import collections.abc
|
|||||||
import inspect
|
import inspect
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import logging
|
||||||
import types
|
import types
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
@ -95,6 +95,8 @@ __all__ = (
|
|||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
CFT = TypeVar('CFT', bound='CoroFunc')
|
CFT = TypeVar('CFT', bound='CoroFunc')
|
||||||
|
|
||||||
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def when_mentioned(bot: _Bot, msg: Message, /) -> List[str]:
|
def when_mentioned(bot: _Bot, msg: Message, /) -> List[str]:
|
||||||
"""A callable that implements a command prefix equivalent to being mentioned.
|
"""A callable that implements a command prefix equivalent to being mentioned.
|
||||||
@ -304,7 +306,7 @@ class BotBase(GroupMixin[None]):
|
|||||||
|
|
||||||
The default command error handler provided by the bot.
|
The default command error handler provided by the bot.
|
||||||
|
|
||||||
By default this prints to :data:`sys.stderr` however it could be
|
By default this logs to the library logger, however it could be
|
||||||
overridden to have a different implementation.
|
overridden to have a different implementation.
|
||||||
|
|
||||||
This only fires if you do not specify any listeners for command error.
|
This only fires if you do not specify any listeners for command error.
|
||||||
@ -312,6 +314,7 @@ class BotBase(GroupMixin[None]):
|
|||||||
.. versionchanged:: 2.0
|
.. versionchanged:: 2.0
|
||||||
|
|
||||||
``context`` and ``exception`` parameters are now positional-only.
|
``context`` and ``exception`` parameters are now positional-only.
|
||||||
|
Instead of writing to ``sys.stderr`` this now uses the library logger.
|
||||||
"""
|
"""
|
||||||
if self.extra_events.get('on_command_error', None):
|
if self.extra_events.get('on_command_error', None):
|
||||||
return
|
return
|
||||||
@ -324,8 +327,7 @@ class BotBase(GroupMixin[None]):
|
|||||||
if cog and cog.has_error_handler():
|
if cog and cog.has_error_handler():
|
||||||
return
|
return
|
||||||
|
|
||||||
print(f'Ignoring exception in command {context.command}:', file=sys.stderr)
|
_log.error('Ignoring exception in command %s', command, exc_info=exception)
|
||||||
traceback.print_exception(type(exception), exception, exception.__traceback__, file=sys.stderr)
|
|
||||||
|
|
||||||
# global check registration
|
# global check registration
|
||||||
|
|
||||||
|
@ -42,8 +42,6 @@ from typing import (
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
import inspect
|
import inspect
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from discord.backoff import ExponentialBackoff
|
from discord.backoff import ExponentialBackoff
|
||||||
@ -536,8 +534,7 @@ class Loop(Generic[LF]):
|
|||||||
|
|
||||||
async def _error(self, *args: Any) -> None:
|
async def _error(self, *args: Any) -> None:
|
||||||
exception: Exception = args[-1]
|
exception: Exception = args[-1]
|
||||||
print(f'Unhandled exception in internal background task {self.coro.__name__!r}.', file=sys.stderr)
|
_log.error('Unhandled exception in internal background task %r.', self.coro.__name__, exc_info=exception)
|
||||||
traceback.print_exception(type(exception), exception, exception.__traceback__, file=sys.stderr)
|
|
||||||
|
|
||||||
def before_loop(self, coro: FT) -> FT:
|
def before_loop(self, coro: FT) -> FT:
|
||||||
"""A decorator that registers a coroutine to be called before the loop starts running.
|
"""A decorator that registers a coroutine to be called before the loop starts running.
|
||||||
@ -600,11 +597,15 @@ class Loop(Generic[LF]):
|
|||||||
|
|
||||||
The coroutine must take only one argument the exception raised (except ``self`` in a class context).
|
The coroutine must take only one argument the exception raised (except ``self`` in a class context).
|
||||||
|
|
||||||
By default this prints to :data:`sys.stderr` however it could be
|
By default this logs to the library logger however it could be
|
||||||
overridden to have a different implementation.
|
overridden to have a different implementation.
|
||||||
|
|
||||||
.. versionadded:: 1.4
|
.. versionadded:: 1.4
|
||||||
|
|
||||||
|
.. versionchanged:: 2.0
|
||||||
|
|
||||||
|
Instead of writing to ``sys.stderr``, the library's logger is used.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
------------
|
------------
|
||||||
coro: :ref:`coroutine <coroutine>`
|
coro: :ref:`coroutine <coroutine>`
|
||||||
|
13
docs/api.rst
13
docs/api.rst
@ -195,7 +195,7 @@ overriding the specific events. For example: ::
|
|||||||
|
|
||||||
|
|
||||||
If an event handler raises an exception, :func:`on_error` will be called
|
If an event handler raises an exception, :func:`on_error` will be called
|
||||||
to handle it, which defaults to print a traceback and ignoring the exception.
|
to handle it, which defaults to logging the traceback and ignoring the exception.
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
|
|
||||||
@ -350,7 +350,7 @@ Debug
|
|||||||
.. function:: on_error(event, *args, **kwargs)
|
.. function:: on_error(event, *args, **kwargs)
|
||||||
|
|
||||||
Usually when an event raises an uncaught exception, a traceback is
|
Usually when an event raises an uncaught exception, a traceback is
|
||||||
printed to stderr and the exception is ignored. If you want to
|
logged to stderr and the exception is ignored. If you want to
|
||||||
change this behaviour and handle the exception for whatever reason
|
change this behaviour and handle the exception for whatever reason
|
||||||
yourself, this event can be overridden. Which, when done, will
|
yourself, this event can be overridden. Which, when done, will
|
||||||
suppress the default action of printing the traceback.
|
suppress the default action of printing the traceback.
|
||||||
@ -358,11 +358,6 @@ Debug
|
|||||||
The information of the exception raised and the exception itself can
|
The information of the exception raised and the exception itself can
|
||||||
be retrieved with a standard call to :func:`sys.exc_info`.
|
be retrieved with a standard call to :func:`sys.exc_info`.
|
||||||
|
|
||||||
If you want exception to propagate out of the :class:`Client` class
|
|
||||||
you can define an ``on_error`` handler consisting of a single empty
|
|
||||||
:ref:`raise statement <py:raise>`. Exceptions raised by ``on_error`` will not be
|
|
||||||
handled in any way by :class:`Client`.
|
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
``on_error`` will only be dispatched to :meth:`Client.event`.
|
``on_error`` will only be dispatched to :meth:`Client.event`.
|
||||||
@ -371,6 +366,10 @@ Debug
|
|||||||
:ref:`ext_commands_api_bot` listeners such as
|
:ref:`ext_commands_api_bot` listeners such as
|
||||||
:meth:`~ext.commands.Bot.listen` or :meth:`~ext.commands.Cog.listener`.
|
:meth:`~ext.commands.Bot.listen` or :meth:`~ext.commands.Cog.listener`.
|
||||||
|
|
||||||
|
.. versionchanged:: 2.0
|
||||||
|
|
||||||
|
The traceback is now logged rather than printed.
|
||||||
|
|
||||||
:param event: The name of the event that raised the exception.
|
:param event: The name of the event that raised the exception.
|
||||||
:type event: :class:`str`
|
:type event: :class:`str`
|
||||||
|
|
||||||
|
@ -933,6 +933,17 @@ The following methods have been changed:
|
|||||||
- :meth:`Webhook.send`
|
- :meth:`Webhook.send`
|
||||||
- :meth:`abc.GuildChannel.set_permissions`
|
- :meth:`abc.GuildChannel.set_permissions`
|
||||||
|
|
||||||
|
Logging Changes
|
||||||
|
----------------
|
||||||
|
|
||||||
|
The library now provides a default logging configuration if using :meth:`Client.run`. To disable it, pass ``None`` to the ``log_handler`` keyword parameter. Since the library now provides a default logging configuration, certain methods were changed to no longer print to :data:`sys.stderr` but use the logger instead:
|
||||||
|
|
||||||
|
- :meth:`Client.on_error`
|
||||||
|
- :meth:`discord.ext.tasks.Loop.error`
|
||||||
|
- :meth:`discord.ext.commands.Bot.on_command_error`
|
||||||
|
|
||||||
|
For more information, check :doc:`logging`.
|
||||||
|
|
||||||
Removal of ``StoreChannel``
|
Removal of ``StoreChannel``
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user