Skip to content
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
5 changes: 4 additions & 1 deletion redbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ async def run_bot(red: Red, cli_flags: Namespace) -> None:
redbot.logging.init_logging(
level=cli_flags.logging_level,
location=data_manager.core_data_path() / "logs",
cli_flags=cli_flags,
rich_logging=cli_flags.rich_logging,
rich_tracebacks=cli_flags.rich_tracebacks,
rich_traceback_extra_lines=cli_flags.rich_traceback_extra_lines,
rich_traceback_show_locals=cli_flags.rich_traceback_show_locals,
)

log.debug("====Basic Config====")
Expand Down
50 changes: 33 additions & 17 deletions redbot/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,20 @@ def emit(self, record: LogRecord) -> None:
self.console.print(traceback)


def init_logging(level: int, location: pathlib.Path, cli_flags: argparse.Namespace) -> None:
_FILE_FORMATTER = logging.Formatter(
"[{asctime}] [{levelname}] {name}: {message}", datefmt="%Y-%m-%d %H:%M:%S", style="{"
)


def init_logging(
level: int,
*,
location: Optional[pathlib.Path] = None,
rich_logging: Optional[bool] = None,
rich_tracebacks: bool = False,
rich_traceback_extra_lines: int = 0,
rich_traceback_show_locals: bool = False,
) -> None:
root_logger = logging.getLogger()
root_logger.setLevel(level)
# DEBUG logging for discord.py is a bit too ridiculous :)
Expand Down Expand Up @@ -312,24 +325,21 @@ def init_logging(level: int, location: pathlib.Path, cli_flags: argparse.Namespa

enable_rich_logging = False

if isatty(0) and cli_flags.rich_logging is None:
if isatty(0) and rich_logging is None:
# Check if the bot thinks it has a active terminal.
enable_rich_logging = True
elif cli_flags.rich_logging is True:
elif rich_logging is True:
enable_rich_logging = True

file_formatter = logging.Formatter(
"[{asctime}] [{levelname}] {name}: {message}", datefmt="%Y-%m-%d %H:%M:%S", style="{"
)
if enable_rich_logging is True:
rich_formatter = logging.Formatter("{message}", datefmt="[%X]", style="{")

stdout_handler = RedRichHandler(
rich_tracebacks=cli_flags.rich_tracebacks,
rich_tracebacks=rich_tracebacks,
show_path=False,
highlighter=NullHighlighter(),
tracebacks_extra_lines=cli_flags.rich_traceback_extra_lines,
tracebacks_show_locals=cli_flags.rich_traceback_show_locals,
tracebacks_extra_lines=rich_traceback_extra_lines,
tracebacks_show_locals=rich_traceback_show_locals,
tracebacks_theme=(
PygmentsSyntaxTheme(FixedMonokaiStyle)
if rich_console.color_system == "truecolor"
Expand All @@ -339,11 +349,22 @@ def init_logging(level: int, location: pathlib.Path, cli_flags: argparse.Namespa
stdout_handler.setFormatter(rich_formatter)
else:
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(file_formatter)
stdout_handler.setFormatter(_FILE_FORMATTER)

root_logger.addHandler(stdout_handler)
logging.captureWarnings(True)

if location is not None:
init_file_logging(location)

if not enable_rich_logging and rich_tracebacks:
log.warning(
"Rich tracebacks were requested but they will not be enabled"
" as Rich logging is not active."
)


def init_file_logging(location: pathlib.Path) -> None:
if not location.exists():
location.mkdir(parents=True, exist_ok=True)
# Rotate latest logs to previous logs
Expand Down Expand Up @@ -379,12 +400,7 @@ def init_logging(level: int, location: pathlib.Path, cli_flags: argparse.Namespa
encoding="utf-8",
)

root_logger = logging.getLogger()
for fhandler in (latest_fhandler, all_fhandler):
fhandler.setFormatter(file_formatter)
fhandler.setFormatter(_FILE_FORMATTER)
root_logger.addHandler(fhandler)

if not enable_rich_logging and cli_flags.rich_tracebacks:
log.warning(
"Rich tracebacks were requested but they will not be enabled"
" as Rich logging is not active."
)
11 changes: 3 additions & 8 deletions redbot/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import click

import redbot.logging
from redbot.core._cli import confirm
from redbot.core.utils._internal_utils import (
safe_delete,
Expand Down Expand Up @@ -436,15 +437,9 @@ def cli(
overwrite_existing_instance: bool,
) -> None:
"""Create a new instance."""

level = cli_level_to_log_level(debug)
base_logger = logging.getLogger("red")
base_logger.setLevel(level)
formatter = logging.Formatter(
"[{asctime}] [{levelname}] {name}: {message}", datefmt="%Y-%m-%d %H:%M:%S", style="{"
)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(formatter)
base_logger.addHandler(stdout_handler)
redbot.logging.init_logging(level)

if ctx.invoked_subcommand is None:
basic_setup(
Expand Down
Loading