From ceeb7e1c0616f0540c46efb25f9d4b3bca6fcf0c Mon Sep 17 00:00:00 2001 From: antazoey Date: Fri, 25 Aug 2023 17:26:10 -0500 Subject: [PATCH] feat: show plugin name in log messages [APE-1327] (#1628) --- src/ape/logging.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ape/logging.py b/src/ape/logging.py index 9fabfafeaf..abfb38697d 100644 --- a/src/ape/logging.py +++ b/src/ape/logging.py @@ -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 @@ -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): @@ -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)