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: Add colored log messages #1432

Merged
merged 1 commit into from
Mar 20, 2024
Merged
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
21 changes: 12 additions & 9 deletions backend/capellacollab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@

from . import __version__

handlers: list[logging.Handler] = [
logging.StreamHandler(),
core_logging.CustomTimedRotatingFileHandler(
str(config["logging"]["logPath"]) + "backend.log"
),
]
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(core_logging.CustomFormatter())

for handler in handlers:
handler.setFormatter(core_logging.CustomFormatter())
timed_rotating_file_handler = core_logging.CustomTimedRotatingFileHandler(
str(config["logging"]["logPath"]) + "backend.log"
)
timed_rotating_file_handler.setFormatter(
core_logging.CustomFormatter(colored_output=False)
)

logging.basicConfig(level=config["logging"]["level"], handlers=handlers)
logging.basicConfig(
level=config["logging"]["level"],
handlers=[stream_handler, timed_rotating_file_handler],
)


async def startup():
Expand Down
38 changes: 29 additions & 9 deletions backend/capellacollab/core/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,40 @@


class CustomFormatter(logging.Formatter):
def __init__(self):
self._request_formatters = logging.Formatter(
'time="%(asctime)s" level=%(levelname)s function=%(funcName)s %(message)s'
)
self._default_formatter = logging.Formatter(
'time="%(asctime)s" level=%(levelname)s name=%(name)s function=%(funcName)s message="%(message)s"'
)
_colors = {
logging.DEBUG: "\x1b[37;40m", # white
logging.INFO: "\x1b[34;40m", # blue
logging.WARNING: "\x1b[93;40m", # yellow
logging.ERROR: "\x1b[31;40m", # red
logging.CRITICAL: "\x1b[31;40m", # red
"reset": "\x1b[0m",
}

def __init__(self, colored_output: bool = True) -> None:
super().__init__()
self.colored_output = colored_output

def format(self, record):
log_format = 'time="%(asctime)s" level=%(levelname)s '
if record.name == "capellacollab.request":
return self._request_formatters.format(record)
log_format += "function=%(funcName)s %(message)s"
else:
log_format += (
'name=%(name)s function=%(funcName)s message="%(message)s"'
)

if self.colored_output:
log_format = (
self._colors[record.levelno]
+ log_format
+ self._colors["reset"]
)

formatter = logging.Formatter(
log_format, datefmt="%Y-%m-%dT%H:%M:%S%z"
)

return self._default_formatter.format(record)
return formatter.format(record)


class CustomTimedRotatingFileHandler(handlers.TimedRotatingFileHandler):
Expand Down
2 changes: 1 addition & 1 deletion helm/templates/promtail/promtail.configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data:
- job_name: system
pipeline_stages:
- multiline:
firstline: '^time=\"\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2},\d{3}\"'
firstline: '^time='
static_configs:
- targets:
- localhost
Expand Down
Loading