Skip to content

Commit

Permalink
feat: show plugin name in log messages [APE-1327] (#1628)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 25, 2023
1 parent e48fece commit ceeb7e1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ape/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import traceback
from enum import IntEnum
from pathlib import Path
from typing import IO, Any, Dict, Optional, Union

import click
Expand All @@ -19,7 +20,7 @@ class LogLevel(IntEnum):
logging.addLevelName(LogLevel.SUCCESS.value, LogLevel.SUCCESS.name)
logging.SUCCESS = LogLevel.SUCCESS.value # type: ignore
DEFAULT_LOG_LEVEL = LogLevel.INFO.name
DEFAULT_LOG_FORMAT = "%(levelname)s: %(message)s"
DEFAULT_LOG_FORMAT = "%(levelname)s%(plugin)s: %(message)s"


def success(self, message, *args, **kws):
Expand Down Expand Up @@ -68,12 +69,19 @@ def __init__(self, fmt: Optional[str] = None):

def format(self, record):
if _isatty(sys.stdout) and _isatty(sys.stderr):
# only color log messages when sys.stdout and sys.stderr are sent to the terminal
# Only color log messages when sys.stdout and sys.stderr are sent to the terminal.
level = LogLevel(record.levelno)
default_dict: Dict[str, Any] = {}
styles: Dict[str, Any] = CLICK_STYLE_KWARGS.get(level, default_dict)
record.levelname = click.style(record.levelname, **styles)

path = Path(record.pathname)
record.plugin = ""
for part in path.parts:
if part.startswith("ape-"):
record.plugin = f" ({part})"
break

return super().format(record)


Expand Down

0 comments on commit ceeb7e1

Please sign in to comment.