Skip to content

Commit

Permalink
Improve the logging
Browse files Browse the repository at this point in the history
Improve the logging by outputting different log levels in different
colors. Also, the name `--debug` option is renamed to `--verbose`.
  • Loading branch information
tacgomes committed Apr 11, 2024
1 parent 6e652c9 commit 503e956
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/investir/investir.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pathlib

from .config import Config
from .logging import setup_logging
from .parser.factory import ParserFactory
from .transaction import OrderType, TransferType
from .trhistory import TrHistory
Expand Down Expand Up @@ -119,9 +120,16 @@ def main() -> None:
)

parser.add_argument(
'-d', '--debug',
'--verbose',
action='store_true',
help='Enable debug logging')
help='enable verbose logging')

parser.add_argument(
'--no-colour',
action='store_false',
dest='colour',
default=True,
help='disable coloured output')

parser.add_argument(
'-v', '--version',
Expand All @@ -131,10 +139,7 @@ def main() -> None:

args = parser.parse_args()

logging.basicConfig(
format='[%(asctime)s] [%(levelname)-8s] %(message)s',
level=logging.DEBUG if args.debug else logging.WARNING
)
setup_logging(args.verbose, args.colour)

config = Config(strict=True)

Expand Down
39 changes: 39 additions & 0 deletions src/investir/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging


class CustomFormatter(logging.Formatter):
LIGHT_CYAN = '\033[0;96m'
GREEN = '\033[0;32m'
YELLOW = '\033[38;5;226m'
RED = '\033[38;5;196m'
BOLD_RED = '\033[31;1m'
RESET = '\033[0m'

def __init__(self, fmt: str) -> None:
super().__init__()
self.formats = {
logging.DEBUG: self.LIGHT_CYAN + fmt + self.RESET,
logging.INFO: self.GREEN + fmt + self.RESET,
logging.WARNING: self.YELLOW + fmt + self.RESET,
logging.ERROR: self.RED + fmt + self.RESET,
logging.CRITICAL: self.BOLD_RED + fmt + self.RESET
}

def format(self, record: logging.LogRecord) -> str:
log_fmt = self.formats.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)


def setup_logging(debug: bool, color: bool) -> None:
logger = logging.getLogger()
logger.setLevel(logging.NOTSET)

fmt = '%(levelname)8s | %(message)s'
console_handler = logging.StreamHandler()
console_handler.setLevel(
logging.DEBUG if debug else logging.INFO)
console_handler.setFormatter(
CustomFormatter(fmt) if color else logging.Formatter(fmt))

logger.addHandler(console_handler)
2 changes: 1 addition & 1 deletion src/investir/parser/freetrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _parse_dividend(self, row: dict[str, str]):
# the total amount is occasionally off by one cent.
logger.warning(
'Calculated amount (£%s ~= £%s) differs from the amount read '
'(£%s) for row %s',
'(£%s) for row %s\n',
calculated_ta, rounded_calculated_ta, total_amount, row)

self._dividends.append(Dividend(
Expand Down

0 comments on commit 503e956

Please sign in to comment.