Skip to content

Commit

Permalink
fix: colorize logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenac95 committed Jan 30, 2025
1 parent 0feaab1 commit 9f9c62b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pandas = "^2.2.3"
docker = "^7.1.0"
gitpython = "^3.1.44"
minio = "^7.2.15"
colorlog = "^6.9.0"


[tool.poetry.scripts]
Expand Down
17 changes: 13 additions & 4 deletions warehouse/metrics_tools/utils/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import typing as t
import logging
import os
import sys
import typing as t

import colorlog

connected_to_sqlmesh_logs = False

Expand Down Expand Up @@ -55,20 +57,27 @@ def setup_multiple_modules_logging(module_names: t.List[str]):
def setup_module_logging(
module_name: str,
level: int = logging.DEBUG,
format: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
override_format: str = "",
color: bool = False,
):
logger = logging.getLogger(module_name)
logger.setLevel(level) # Adjust the level as needed

# Create a handler that logs to stdout
stdout_handler = logging.StreamHandler(sys.stdout)
if color:
format = "%(asctime)s - %(log_color)s%(levelname)-8s%(reset)s - %(name)s - %(message)s"
stdout_handler = colorlog.StreamHandler(sys.stdout)
formatter = colorlog.ColoredFormatter(format, datefmt="%Y-%m-%dT%H:%M:%S")
else:
format = "%(asctime)s - %(levelname)-8s - %(name)s - %(message)s"
stdout_handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter(format, datefmt="%Y-%m-%dT%H:%M:%S")
stdout_handler.setLevel(level) # Adjust the level as needed

# Add the filter to the handler
stdout_handler.addFilter(ModuleFilter(module_name))

# Set a formatter (optional)
formatter = logging.Formatter(format, datefmt="%Y-%m-%dT%H:%M:%S")
stdout_handler.setFormatter(formatter)

# Add the handler to the logger
Expand Down
8 changes: 4 additions & 4 deletions warehouse/oso_lets_go/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
@click.option("--debug/--no-debug", default=False)
@click.pass_context
def cli(ctx: click.Context, debug: bool):
setup_module_logging("oso_lets_go")
setup_module_logging("metrics_tools")
setup_module_logging("oso_dagster")
setup_module_logging("opsscripts")
setup_module_logging("oso_lets_go", color=True)
setup_module_logging("metrics_tools", color=True)
setup_module_logging("oso_dagster", color=True)
setup_module_logging("opsscripts", color=True)
ctx.ensure_object(dict)
ctx.obj["DEBUG"] = debug

Expand Down

0 comments on commit 9f9c62b

Please sign in to comment.