Skip to content

refactor: ♻️ replace print statements with logging where possible #2831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions discord/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import argparse
import importlib.metadata
import logging
import platform
import sys
from pathlib import Path
Expand All @@ -34,6 +35,8 @@

import discord

_log = logging.getLogger(__name__)


def show_version() -> None:
entries = [
Expand Down Expand Up @@ -238,8 +241,8 @@ def newbot(parser, args) -> None:
cogs.mkdir(exist_ok=True)
init = cogs / "__init__.py"
init.touch()
except OSError as exc:
print(f"warning: could not create cogs directory ({exc})")
except OSError:
_log.exception(f"Could not create cogs directory.")

try:
with open(str(new_directory / "config.py"), "w", encoding="utf-8") as fp:
Expand All @@ -258,18 +261,18 @@ def newbot(parser, args) -> None:
try:
with open(str(new_directory / ".gitignore"), "w", encoding="utf-8") as fp:
fp.write(_gitignore_template)
except OSError as exc:
print(f"warning: could not create .gitignore file ({exc})")
except OSError:
_log.exception(f"Could not create .gitignore file.")

print("successfully made bot at", new_directory)
print("Successfully made bot at", new_directory)


def newcog(parser, args) -> None:
cog_dir = to_path(parser, args.directory)
try:
cog_dir.mkdir(exist_ok=True)
except OSError as exc:
print(f"warning: could not create cogs directory ({exc})")
except OSError:
_log.exception(f"Could not create cogs directory.")

directory = cog_dir / to_path(parser, args.name)
directory = directory.with_suffix(".py")
Expand Down
9 changes: 3 additions & 6 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import copy
import inspect
import logging
import sys
import traceback
from abc import ABC, abstractmethod
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -1240,7 +1238,7 @@ async def on_application_command_error(

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 with :meth:`logging.exception` however it could be
overridden to have a different implementation.

This only fires if you do not specify any listeners for command error.
Expand All @@ -1255,9 +1253,8 @@ async def on_application_command_error(
if cog and cog.has_error_handler():
return

print(f"Ignoring exception in command {context.command}:", file=sys.stderr)
traceback.print_exception(
type(exception), exception, exception.__traceback__, file=sys.stderr
_log.error(
f"Ignoring exception in command {context.command}.", exc_info=exception
)

# global check registration
Expand Down
19 changes: 6 additions & 13 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import asyncio
import logging
import signal
import sys
import traceback
from types import TracebackType
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generator, Sequence, TypeVar
Expand Down Expand Up @@ -536,11 +535,11 @@ async def on_error(self, event_method: str, *args: Any, **kwargs: Any) -> None:

The default error handler provided by the client.

By default, this prints to :data:`sys.stderr` however it could be
By default, this logs with :meth:`logging.error` however it could be
overridden to have a different implementation.
Check :func:`~discord.on_error` for more details.
"""
print(f"Ignoring exception in {event_method}", file=sys.stderr)
_log.error(f"Ignoring exception in {event_method}")
traceback.print_exc()

async def on_view_error(
Expand All @@ -553,27 +552,21 @@ async def on_view_error(
This only fires for a view if you did not define its :func:`~discord.ui.View.on_error`.
"""

print(
_log.error(
f"Ignoring exception in view {interaction.view} for item {item}:",
file=sys.stderr,
)
traceback.print_exception(
error.__class__, error, error.__traceback__, file=sys.stderr
exc_info=error,
)

async def on_modal_error(self, error: Exception, interaction: Interaction) -> None:
"""|coro|

The default modal error handler provided by the client.
The default implementation prints the traceback to stderr.
The default implementation logs the traceback with :meth:`logging.error`.

This only fires for a modal if you did not define its :func:`~discord.ui.Modal.on_error`.
"""

print(f"Ignoring exception in modal {interaction.modal}:", file=sys.stderr)
traceback.print_exception(
error.__class__, error, error.__traceback__, file=sys.stderr
)
_log.error(f"Ignoring exception in modal {interaction.modal}", exc_info=error)

# hooks

Expand Down
12 changes: 6 additions & 6 deletions discord/ext/commands/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@

import collections
import collections.abc
import sys
import traceback
import logging
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Iterable, TypeVar

import discord
Expand All @@ -51,6 +50,8 @@
"AutoShardedBot",
)

_log = logging.getLogger(__name__)

MISSING: Any = discord.utils.MISSING

T = TypeVar("T")
Expand Down Expand Up @@ -156,7 +157,7 @@ async def on_command_error(

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 with :meth:`logging.error` however it could be
overridden to have a different implementation.

This only fires if you do not specify any listeners for command error.
Expand All @@ -172,9 +173,8 @@ async def on_command_error(
if cog and cog.has_error_handler():
return

print(f"Ignoring exception in command {context.command}:", file=sys.stderr)
traceback.print_exception(
type(exception), exception, exception.__traceback__, file=sys.stderr
logging.error(
f"Ignoring exception in command {context.command}", exc_info=exception
)

async def can_run(self, ctx: Context, *, call_once: bool = False) -> bool:
Expand Down
14 changes: 6 additions & 8 deletions discord/ext/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import asyncio
import datetime
import inspect
import sys
import traceback
import logging
from collections.abc import Sequence
from typing import Any, Awaitable, Callable, Generic, TypeVar, cast

Expand All @@ -47,6 +46,8 @@
FT = TypeVar("FT", bound=_func)
ET = TypeVar("ET", bound=Callable[[Any, BaseException], Awaitable[Any]])

_log = logging.getLogger(__name__)


class SleepHandle:
__slots__ = ("future", "loop", "handle")
Expand Down Expand Up @@ -474,12 +475,9 @@ def is_running(self) -> bool:

async def _error(self, *args: Any) -> None:
exception: Exception = args[-1]
print(
_log.error(
f"Unhandled exception in internal background task {self.coro.__name__!r}.",
file=sys.stderr,
)
traceback.print_exception(
type(exception), exception, exception.__traceback__, file=sys.stderr
exc_info=exception,
)

def before_loop(self, coro: FT) -> FT:
Expand Down Expand Up @@ -544,7 +542,7 @@ def error(self, coro: ET) -> ET:

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 with the logging module however it could be
overridden to have a different implementation.

.. versionadded:: 1.4
Expand Down
4 changes: 2 additions & 2 deletions discord/opus.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def run(self):
data.decrypted_data
)
except OpusError:
print("Error occurred while decoding opus frame.")
_log.exception("Error occurred while decoding opus frame.")
continue

self.client.recv_decoded_audio(data)
Expand All @@ -560,7 +560,7 @@ def stop(self):
time.sleep(0.1)
self.decoder = {}
gc.collect()
print("Decoder Process Killed")
_log.info("Decoder Process Killed")
self._end_thread.set()

def get_decoder(self, ssrc):
Expand Down
2 changes: 0 additions & 2 deletions discord/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,6 @@ def _call_after(self) -> None:
elif error:
msg = f"Exception in voice thread {self.name}"
_log.exception(msg, exc_info=error)
print(msg, file=sys.stderr)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this one pls feedback

traceback.print_exception(type(error), error, error.__traceback__)

def stop(self) -> None:
self._end.set()
Expand Down
2 changes: 1 addition & 1 deletion discord/ui/modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ async def on_error(self, error: Exception, interaction: Interaction) -> None:

A callback that is called when the modal's callback fails with an error.

The default implementation prints the traceback to stderr.
The default implementation logs the traceback with the logging module.

Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion discord/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ async def on_error(
A callback that is called when an item's callback or :meth:`interaction_check`
fails with an error.

The default implementation prints the traceback to stderr.
The default implementation logs the traceback with the logging module.

Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions discord/voice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ def recv_audio(self, sink, callback, *args):
ready, _, err = select.select([self.socket], [], [self.socket], 0.01)
if not ready:
if err:
print(f"Socket error: {err}")
_log.error(f"Socket error: {err}")
continue

try:
Expand All @@ -880,7 +880,7 @@ def recv_audio(self, sink, callback, *args):
result = callback.result()

if result is not None:
print(result)
_log.info(result)

def recv_decoded_audio(self, data: RawData):
# Add silence when they were not being recorded.
Expand Down
Loading