fix(logging):

This commit is contained in:
Alexander Myasoedov
2025-03-08 17:51:44 +02:00
parent 7fe93e8a14
commit db994fd483
3 changed files with 19 additions and 41 deletions
+1 -1
View File
@@ -4,12 +4,12 @@ from datetime import datetime
import colorama import colorama
import tqdm.asyncio import tqdm.asyncio
from loguru import logger
from rich.console import Console from rich.console import Console
from rich.table import Table from rich.table import Table
from tabulate import tabulate from tabulate import tabulate
from agentic_security.config import SettingsMixin # Importing the configuration mixin from agentic_security.config import SettingsMixin # Importing the configuration mixin
from agentic_security.logutils import logger
from agentic_security.primitives import Scan from agentic_security.primitives import Scan
from agentic_security.probe_data import REGISTRY from agentic_security.probe_data import REGISTRY
from agentic_security.routes.scan import streaming_response_generator from agentic_security.routes.scan import streaming_response_generator
+17 -40
View File
@@ -1,45 +1,22 @@
import logging import sys
from os import getenv
from rich.logging import RichHandler from loguru import logger
LOGGER_NAME = "root" # Define custom colors
BLUE = "#89CFF0"
BROWN = "#8B4513" # Brown for DEBUG
# Define custom log level colors
logger.level("DEBUG", color=f"<fg {BROWN}>")
logger.level("INFO", color=f"<fg {BLUE}>")
def get_logger(logger_name: str) -> logging.Logger: # Define custom log format with aligned messages and colored levels
# https://rich.readthedocs.io/en/latest/reference/logging.html#rich.logging.RichHandler LOG_FORMAT = (
# https://rich.readthedocs.io/en/latest/logging.html#handle-exceptions "<level>{level:<8}</level> " # Properly formatted and colored log level
rich_handler = RichHandler( "<level>{message:<100}</level> " # Left-aligned message for readability
show_time=False, "<cyan>{file.name}</cyan>:<cyan>{line}</cyan>" # File name and line number in cyan
rich_tracebacks=False, )
show_path=True if getenv("API_RUNTIME") == "dev" else False,
tracebacks_show_locals=False,
)
rich_handler.setFormatter(
logging.Formatter(
fmt="%(message)s",
datefmt="[%X]",
)
)
_logger = logging.getLogger(logger_name) # Remove default handlers and add a new one with custom formatting
_logger.addHandler(rich_handler) logger.remove()
_logger.setLevel(logging.INFO) logger.add(sys.stdout, format=LOG_FORMAT, level="DEBUG", colorize=True)
_logger.propagate = True
return _logger
logger: logging.Logger = get_logger(LOGGER_NAME)
def set_log_level_to_debug():
_logger = logging.getLogger(LOGGER_NAME)
_logger.setLevel(logging.DEBUG)
def set_log_level_to_info():
_logger = logging.getLogger(LOGGER_NAME)
_logger.setLevel(logging.INFO)
set_log_level_to_debug()
+1
View File
@@ -80,6 +80,7 @@ def generate_banner(
def init_banner(): def init_banner():
return
ver = version("agentic_security") ver = version("agentic_security")
try: try:
print(generate_banner(version=ver)) print(generate_banner(version=ver))