Merge pull request 'fix logger no longer applying to clan_lib' (#3709) from logging into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3709
This commit is contained in:
Mic92
2025-05-20 08:21:20 +00:00
7 changed files with 20 additions and 31 deletions

View File

@@ -23,11 +23,9 @@ class ClanAppOptions:
@profile @profile
def app_run(app_opts: ClanAppOptions) -> int: def app_run(app_opts: ClanAppOptions) -> int:
if app_opts.debug: if app_opts.debug:
setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0]) setup_logging(logging.DEBUG)
setup_logging(logging.DEBUG, root_log_name="clan_cli")
else: else:
setup_logging(logging.INFO, root_log_name=__name__.split(".")[0]) setup_logging(logging.INFO)
setup_logging(logging.INFO, root_log_name="clan_cli")
log.debug("Debug mode enabled") log.debug("Debug mode enabled")

View File

@@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import logging
import subprocess import subprocess
from pathlib import Path from pathlib import Path
@@ -22,7 +23,7 @@ def pytest_sessionstart(session: pytest.Session) -> None:
# You can access the session config, items, testsfailed, etc. # You can access the session config, items, testsfailed, etc.
print(f"Session config: {session.config}") print(f"Session config: {session.config}")
setup_logging(level="DEBUG") setup_logging(logging.DEBUG)
# fixture for git_repo # fixture for git_repo

View File

@@ -452,10 +452,10 @@ def main() -> None:
parser.print_help() parser.print_help()
if debug := getattr(args, "debug", False): if debug := getattr(args, "debug", False):
setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0]) setup_logging(logging.DEBUG)
log.debug("Debug log activated") log.debug("Debug log activated")
else: else:
setup_logging(logging.INFO, root_log_name=__name__.split(".")[0]) setup_logging(logging.INFO)
if not hasattr(args, "func"): if not hasattr(args, "func"):
return return

View File

@@ -3,7 +3,6 @@ import logging
import os import os
import sys import sys
from pathlib import Path from pathlib import Path
from typing import Any
from clan_cli.colors import AnsiColor, RgbColor, color_by_tuple from clan_cli.colors import AnsiColor, RgbColor, color_by_tuple
@@ -33,8 +32,6 @@ class PrefixFormatter(logging.Formatter):
self.hostname_color_offset = 0 self.hostname_color_offset = 0
def format(self, record: logging.LogRecord) -> str: def format(self, record: logging.LogRecord) -> str:
filepath = _get_filepath(record)
# If extra["color"] is set, use that color for the message. # If extra["color"] is set, use that color for the message.
msg_color = getattr(record, "color", None) msg_color = getattr(record, "color", None)
if not msg_color: if not msg_color:
@@ -72,6 +69,7 @@ class PrefixFormatter(logging.Formatter):
# Add the source file and line number if trace_prints is enabled. # Add the source file and line number if trace_prints is enabled.
if self.trace_prints: if self.trace_prints:
filepath = _get_filepath(record)
format_str += f"\nSource: {filepath}:%(lineno)d::%(funcName)s\n" format_str += f"\nSource: {filepath}:%(lineno)d::%(funcName)s\n"
return logging.Formatter(format_str).format(record) return logging.Formatter(format_str).format(record)
@@ -137,11 +135,7 @@ def get_callers(start: int = 2, end: int = 2) -> list[str]:
def print_trace(msg: str, logger: logging.Logger, prefix: str | None) -> None: def print_trace(msg: str, logger: logging.Logger, prefix: str | None) -> None:
trace_depth = int(os.environ.get("TRACE_DEPTH", "0")) trace_depth = int(os.environ.get("TRACE_DEPTH", "0"))
callers = get_callers(3, 4 + trace_depth) callers = get_callers(3, 4 + trace_depth)
callers.pop()
if "run_no_stdout" in callers[0]:
callers = callers[1:]
else:
callers.pop()
if len(callers) == 1: if len(callers) == 1:
callers_str = f"Caller: {callers[0]}\n" callers_str = f"Caller: {callers[0]}\n"
@@ -153,19 +147,13 @@ def print_trace(msg: str, logger: logging.Logger, prefix: str | None) -> None:
logger.debug(f"{msg} \n{callers_str}", extra={"command_prefix": prefix}) logger.debug(f"{msg} \n{callers_str}", extra={"command_prefix": prefix})
def setup_logging( def setup_logging(level: int) -> None:
level: Any, root_logger = logging.getLogger()
root_log_name: str = __name__.split(".")[0], root_logger.setLevel(level)
) -> None:
# Get the root logger and set its level
main_logger = logging.getLogger(root_log_name)
main_logger.setLevel(level)
# Create and add the default handler # Set our formatter handler
default_handler = logging.StreamHandler() default_handler = logging.StreamHandler()
# Create and add your custom handler
default_handler.setLevel(level) default_handler.setLevel(level)
trace_prints = bool(int(os.environ.get("TRACE_PRINT", "0"))) trace_prints = bool(int(os.environ.get("TRACE_PRINT", "0")))
default_handler.setFormatter(PrefixFormatter(trace_prints)) default_handler.setFormatter(PrefixFormatter(trace_prints))
main_logger.addHandler(default_handler) root_logger.addHandler(default_handler)

View File

@@ -1,3 +1,5 @@
import logging
import pytest import pytest
from clan_cli.custom_logger import setup_logging from clan_cli.custom_logger import setup_logging
@@ -19,4 +21,4 @@ def pytest_sessionstart(session: pytest.Session) -> None:
# You can access the session config, items, testsfailed, etc. # You can access the session config, items, testsfailed, etc.
print(f"Session config: {session.config}") print(f"Session config: {session.config}")
setup_logging(level="INFO") setup_logging(logging.DEBUG)

View File

@@ -71,10 +71,9 @@ class MainApplication(Adw.Application):
options = options.end().unpack() options = options.end().unpack()
if "debug" in options and self.window is None: if "debug" in options and self.window is None:
setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0]) setup_logging(logging.DEBUG)
setup_logging(logging.DEBUG, root_log_name="clan_cli")
elif self.window is None: elif self.window is None:
setup_logging(logging.INFO, root_log_name=__name__.split(".")[0]) setup_logging(logging.INFO)
log.debug("Debug logging enabled") log.debug("Debug logging enabled")
if "debug" in options: if "debug" in options:

View File

@@ -1,3 +1,4 @@
import logging
import subprocess import subprocess
import sys import sys
from pathlib import Path from pathlib import Path
@@ -27,7 +28,7 @@ def pytest_sessionstart(session: pytest.Session) -> None:
# You can access the session config, items, testsfailed, etc. # You can access the session config, items, testsfailed, etc.
print(f"Session config: {session.config}") print(f"Session config: {session.config}")
setup_logging(level="DEBUG") setup_logging(logging.DEBUG)
# fixture for git_repo # fixture for git_repo