Skip to content

Commit

Permalink
feat(log): Expose logging QueueListeners in cls object
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinandjarisch committed Dec 22, 2023
1 parent fbc5723 commit dc7b65e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/gallia/command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from argparse import ArgumentParser, Namespace
from datetime import datetime, timezone
from enum import Enum, unique
from logging import Handler
from pathlib import Path
from subprocess import CalledProcessError, run
from tempfile import gettempdir
Expand Down Expand Up @@ -104,6 +105,8 @@ class BaseCommand(ABC):
#: a log message with level critical is logged.
CATCHED_EXCEPTIONS: list[type[Exception]] = []

log_file_handlers: list[Handler]

def __init__(self, parser: ArgumentParser, config: Config = Config()) -> None:
self.id = camel_to_snake(self.__class__.__name__)
self.parser = parser
Expand All @@ -124,6 +127,7 @@ def __init__(self, parser: ArgumentParser, config: Config = Config()) -> None:
self.db_handler: DBHandler | None = None
self.configure_class_parser()
self.configure_parser()
self.log_file_handlers = []

@abstractmethod
def run(self, args: Namespace) -> int:
Expand Down Expand Up @@ -381,10 +385,12 @@ def entry_point(self, args: Namespace) -> int:
args.artifacts_base,
args.artifacts_dir,
)
add_zst_log_handler(
logger_name="gallia",
filepath=self.artifacts_dir.joinpath(FileNames.LOGFILE.value),
file_log_level=get_file_log_level(args),
self.log_file_handlers.append(
add_zst_log_handler(
logger_name="gallia",
filepath=self.artifacts_dir.joinpath(FileNames.LOGFILE.value),
file_log_level=get_file_log_level(args),
)
)

if args.hooks:
Expand Down
3 changes: 2 additions & 1 deletion src/gallia/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def add_stderr_log_handler(

def add_zst_log_handler(
logger_name: str, filepath: Path, file_log_level: Loglevel
) -> None:
) -> logging.Handler:
queue: Queue[Any] = Queue()
logger = get_logger(logger_name)
logger.addHandler(QueueHandler(queue))
Expand All @@ -322,6 +322,7 @@ def add_zst_log_handler(
)
queue_listener.start()
atexit.register(queue_listener.stop)
return zstd_handler


class _PenlogRecordV1(msgspec.Struct, omit_defaults=True):
Expand Down

0 comments on commit dc7b65e

Please sign in to comment.