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:
@@ -23,11 +23,9 @@ class ClanAppOptions:
|
||||
@profile
|
||||
def app_run(app_opts: ClanAppOptions) -> int:
|
||||
if app_opts.debug:
|
||||
setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0])
|
||||
setup_logging(logging.DEBUG, root_log_name="clan_cli")
|
||||
setup_logging(logging.DEBUG)
|
||||
else:
|
||||
setup_logging(logging.INFO, root_log_name=__name__.split(".")[0])
|
||||
setup_logging(logging.INFO, root_log_name="clan_cli")
|
||||
setup_logging(logging.INFO)
|
||||
|
||||
log.debug("Debug mode enabled")
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
@@ -22,7 +23,7 @@ def pytest_sessionstart(session: pytest.Session) -> None:
|
||||
# You can access the session config, items, testsfailed, etc.
|
||||
print(f"Session config: {session.config}")
|
||||
|
||||
setup_logging(level="DEBUG")
|
||||
setup_logging(logging.DEBUG)
|
||||
|
||||
|
||||
# fixture for git_repo
|
||||
|
||||
@@ -452,10 +452,10 @@ def main() -> None:
|
||||
parser.print_help()
|
||||
|
||||
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")
|
||||
else:
|
||||
setup_logging(logging.INFO, root_log_name=__name__.split(".")[0])
|
||||
setup_logging(logging.INFO)
|
||||
|
||||
if not hasattr(args, "func"):
|
||||
return
|
||||
|
||||
@@ -3,7 +3,6 @@ import logging
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from clan_cli.colors import AnsiColor, RgbColor, color_by_tuple
|
||||
|
||||
@@ -33,8 +32,6 @@ class PrefixFormatter(logging.Formatter):
|
||||
self.hostname_color_offset = 0
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
filepath = _get_filepath(record)
|
||||
|
||||
# If extra["color"] is set, use that color for the message.
|
||||
msg_color = getattr(record, "color", None)
|
||||
if not msg_color:
|
||||
@@ -72,6 +69,7 @@ class PrefixFormatter(logging.Formatter):
|
||||
|
||||
# Add the source file and line number if trace_prints is enabled.
|
||||
if self.trace_prints:
|
||||
filepath = _get_filepath(record)
|
||||
format_str += f"\nSource: {filepath}:%(lineno)d::%(funcName)s\n"
|
||||
|
||||
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:
|
||||
trace_depth = int(os.environ.get("TRACE_DEPTH", "0"))
|
||||
callers = get_callers(3, 4 + trace_depth)
|
||||
|
||||
if "run_no_stdout" in callers[0]:
|
||||
callers = callers[1:]
|
||||
else:
|
||||
callers.pop()
|
||||
callers.pop()
|
||||
|
||||
if len(callers) == 1:
|
||||
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})
|
||||
|
||||
|
||||
def setup_logging(
|
||||
level: Any,
|
||||
root_log_name: str = __name__.split(".")[0],
|
||||
) -> None:
|
||||
# Get the root logger and set its level
|
||||
main_logger = logging.getLogger(root_log_name)
|
||||
main_logger.setLevel(level)
|
||||
def setup_logging(level: int) -> None:
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.setLevel(level)
|
||||
|
||||
# Create and add the default handler
|
||||
# Set our formatter handler
|
||||
default_handler = logging.StreamHandler()
|
||||
|
||||
# Create and add your custom handler
|
||||
default_handler.setLevel(level)
|
||||
trace_prints = bool(int(os.environ.get("TRACE_PRINT", "0")))
|
||||
default_handler.setFormatter(PrefixFormatter(trace_prints))
|
||||
main_logger.addHandler(default_handler)
|
||||
root_logger.addHandler(default_handler)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
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.
|
||||
print(f"Session config: {session.config}")
|
||||
|
||||
setup_logging(level="INFO")
|
||||
setup_logging(logging.DEBUG)
|
||||
|
||||
@@ -71,10 +71,9 @@ class MainApplication(Adw.Application):
|
||||
options = options.end().unpack()
|
||||
|
||||
if "debug" in options and self.window is None:
|
||||
setup_logging(logging.DEBUG, root_log_name=__name__.split(".")[0])
|
||||
setup_logging(logging.DEBUG, root_log_name="clan_cli")
|
||||
setup_logging(logging.DEBUG)
|
||||
elif self.window is None:
|
||||
setup_logging(logging.INFO, root_log_name=__name__.split(".")[0])
|
||||
setup_logging(logging.INFO)
|
||||
log.debug("Debug logging enabled")
|
||||
|
||||
if "debug" in options:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
@@ -27,7 +28,7 @@ def pytest_sessionstart(session: pytest.Session) -> None:
|
||||
# You can access the session config, items, testsfailed, etc.
|
||||
print(f"Session config: {session.config}")
|
||||
|
||||
setup_logging(level="DEBUG")
|
||||
setup_logging(logging.DEBUG)
|
||||
|
||||
|
||||
# fixture for git_repo
|
||||
|
||||
Reference in New Issue
Block a user