Skip to content
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

feat: colored logging formatter #348

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ARG PYTHON_VERSION=3.11
FROM python:${PYTHON_VERSION}-slim-bookworm AS builder

ARG POETRY_VERSION=1.6
ARG POETRY_VERSION=1.7
ARG INSTALL_EXTRAS

ENV POETRY_NO_INTERACTION=1 \
Expand All @@ -19,6 +19,7 @@ ENV POETRY_NO_INTERACTION=1 \
RUN apt-get update \
&& apt-get install --no-install-recommends -y build-essential \
&& apt-get clean && rm -rf /var/lib/apt/lists/* \
&& pip install --upgrade pip \
&& pip install --no-cache-dir poetry==$POETRY_VERSION

WORKDIR /app
Expand Down
5 changes: 3 additions & 2 deletions log.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ level=DEBUG
formatter=consoleFormatter

[formatter_consoleFormatter]
format=%(asctime)s - %(thread)d - %(levelname)s - %(message)s
class=logging.Formatter
format=%(asctime)s | %(thread)d | %(levelname)s | %(message)s
datefmt=%Y-%m-%d %H:%M:%S
class=numalogic.udfs.ColoredFormatter
25 changes: 25 additions & 0 deletions numalogic/udfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@
from numalogic.udfs.trainer import TrainerUDF, PromTrainerUDF, DruidTrainerUDF


class ColoredFormatter(logging.Formatter):
"""Custom formatter for colored console logs."""

WHITE = "\x1b[1m"
BLUE = "\x1b[1;36m"
YELLOW = "\x1b[33;20m"
RED = "\x1b[31;20m"
BOLD_RED = "\x1b[31;1m"
RESET = "\x1b[0m"

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._formats = {
logging.DEBUG: f"{self.BLUE}{self._fmt}{self.RESET}",
logging.INFO: f"{self.WHITE}{self._fmt}{self.RESET}",
Copy link

Choose a reason for hiding this comment

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

Do we ensure that the text background won't be WHITE as well?

logging.WARNING: f"{self.YELLOW}{self._fmt}{self.RESET}",
logging.ERROR: f"{self.RED}{self._fmt}{self.RESET}",
logging.CRITICAL: f"{self.BOLD_RED}{self._fmt}{self.RESET}",
}

def format(self, record):
self._style._fmt = self._formats.get(record.levelno)
Copy link

Choose a reason for hiding this comment

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

Do we need to prepare for the case when a new / typo logging level is introduced?

return super().format(record)


def set_logger() -> None:
"""Sets the logger for the UDFs."""
logconf.fileConfig(
Expand Down
Loading
Loading