Move var helpers into their own file, log update with additional levels

Add levels for ALERT, EMERGENCY to be syslog compatible
Add direct wrappers for all, but they are not yet fully usable because the stack fix is not yet implemented

Add a new debug helepr to get the stack as a string
This commit is contained in:
Clemens Schwaighofer
2025-07-11 19:06:49 +09:00
parent 6902768fed
commit 4ca45ebc73
15 changed files with 522 additions and 344 deletions

View File

@@ -11,7 +11,7 @@ from typing import Any, Tuple, Sequence, cast
from pathlib import Path
from corelibs.logging_handling.log import Log
from corelibs.iterator_handling.list_helpers import convert_to_list, is_list_in_list
from corelibs.string_handling.string_helpers import is_int, is_float, str_to_bool
from corelibs.var_handling.var_helpers import is_int, is_float, str_to_bool
from corelibs.config_handling.settings_loader_handling.settings_loader_check import SettingsLoaderCheck

View File

@@ -0,0 +1,33 @@
"""
Various debug helpers
"""
import traceback
import os
def traceback_call_str(start: int = 2, depth: int = 1):
"""
get the trace for the last entry
Keyword Arguments:
start {int} -- _description_ (default: {2})
depth {int} -- _description_ (default: {1})
Returns:
_type_ -- _description_
"""
# can't have more than in the stack for depth
depth = min(depth, start)
depth = start - depth
# 0 is full stack length from start
if depth == 0:
stack = traceback.extract_stack()[-start:]
else:
stack = traceback.extract_stack()[-start:-depth]
return ' -> '.join(
f"{os.path.basename(f.filename)}:{f.name}:{f.lineno}"
for f in stack
)
# __END__

View File

@@ -9,9 +9,10 @@ import logging.handlers
import logging
import time
from pathlib import Path
from typing import Mapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast
from typing import MutableMapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast
from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel
from corelibs.string_handling.text_colors import Colors
from corelibs.debug_handling.debug_helpers import traceback_call_str
if TYPE_CHECKING:
from multiprocessing import Queue
@@ -41,12 +42,14 @@ class CustomConsoleFormatter(logging.Formatter):
"""
COLORS = {
"DEBUG": Colors.cyan,
"INFO": Colors.green,
"WARNING": Colors.yellow,
"ERROR": Colors.red,
"CRITICAL": Colors.red_bold,
"EXCEPTION": Colors.magenta_bold, # will never be written to console
LoggingLevel.DEBUG.name: Colors.cyan,
LoggingLevel.INFO.name: Colors.green,
LoggingLevel.WARNING.name: Colors.yellow,
LoggingLevel.ERROR.name: Colors.red,
LoggingLevel.CRITICAL.name: Colors.red_bold,
LoggingLevel.ALERT.name: Colors.yellow_bold,
LoggingLevel.EMERGENCY.name: Colors.magenta_bold,
LoggingLevel.EXCEPTION.name: Colors.magenta_bright, # will never be written to console
}
def format(self, record: logging.LogRecord) -> str:
@@ -63,7 +66,7 @@ class CustomConsoleFormatter(logging.Formatter):
reset = Colors.reset
color = self.COLORS.get(record.levelname, reset)
# only highlight level for basic
if record.levelname in ['DEBUG', 'INFO']:
if record.levelname in [LoggingLevel.DEBUG.name, LoggingLevel.INFO.name]:
record.levelname = f"{color}{record.levelname}{reset}"
return super().format(record)
# highlight whole line
@@ -71,6 +74,10 @@ class CustomConsoleFormatter(logging.Formatter):
return f"{color}{message}{reset}"
# TODO: add custom handlers for stack_correct, if not set fill with %(filename)s:%(funcName)s:%(lineno)d
# hasattr(record, 'stack_correct')
# MARK: Log class
class Log:
"""
@@ -86,14 +93,14 @@ class Log:
DEFAULT_LOG_LEVEL_CONSOLE: LoggingLevel = LoggingLevel.WARNING
# default settings
DEFAULT_LOG_SETTINGS: LogSettings = {
"log_level_console": LoggingLevel.WARNING,
"log_level_file": LoggingLevel.DEBUG,
"console_enabled": True,
"console_color_output_enabled": True,
"add_start_info": True,
"add_end_info": False,
"log_queue": None,
}
"log_level_console": LoggingLevel.WARNING,
"log_level_file": LoggingLevel.DEBUG,
"console_enabled": True,
"console_color_output_enabled": True,
"add_start_info": True,
"add_end_info": False,
"log_queue": None,
}
# MARK: constructor
def __init__(
@@ -103,8 +110,10 @@ class Log:
log_settings: dict[str, 'LoggingLevel | str | bool | None | Queue[str]'] | LogSettings | None = None,
other_handlers: dict[str, Any] | None = None
):
# add new level for EXCEPTION
logging.addLevelName(LoggingLevel.EXCEPTION.value, 'EXCEPTION')
# add new level for alert, emergecny and exception
logging.addLevelName(LoggingLevel.ALERT.value, LoggingLevel.ALERT.name)
logging.addLevelName(LoggingLevel.EMERGENCY.value, LoggingLevel.EMERGENCY.name)
logging.addLevelName(LoggingLevel.EXCEPTION.value, LoggingLevel.EXCEPTION.name)
# parse the logging settings
self.log_settings = self.__parse_log_settings(log_settings)
# if path, set log name with .log
@@ -126,7 +135,7 @@ class Log:
self.log_queue: 'Queue[str] | None' = None
self.listener: logging.handlers.QueueListener | None = None
self.logger: logging.Logger | None = None
self.logger: logging.Logger
# setup handlers
# NOTE if console with color is set first, some of the color formatting is set
@@ -220,7 +229,7 @@ class Log:
"""
if self.handlers.get(handler_name):
return False
if self.listener is not None or self.logger is not None:
if self.listener is not None or hasattr(self, 'logger'):
raise ValueError(
f"Cannot add handler {handler_name}: {handler.get_name()} because logger is already running"
)
@@ -335,6 +344,9 @@ class Log:
# set maximum logging level for all logging output
# log level filtering is done per handler
self.logger.setLevel(logging.DEBUG)
# short name
self.lg = self.logger
self.l = self.logger
# MARK: init logger for Fork/Thread
@staticmethod
@@ -355,18 +367,107 @@ class Log:
return root_logger
# FIXME: all below will only work if we add a custom format interface for the stack_correct part
# Important note, although they exist, it is recommended to use self.logger.NAME directly
# so that the correct filename, method and row number is set
# for > 50 use logger.log(LoggingLevel.<LEVEL>.value, ...)
# for exception logger.log(LoggingLevel.EXCEPTION.value, ..., execInfo=True)
# MARK: log message
def exception(self, msg: object, *args: object, extra: Mapping[str, object] | None = None) -> None:
def log(self, level: int, msg: object, *args: object, extra: MutableMapping[str, object] | None = None):
"""log general"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.log(level, msg, *args, extra=extra)
# MARK: DEBUG 10
def debug(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""debug"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.debug(msg, *args, extra=extra)
# MARK: INFO 20
def info(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""info"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.info(msg, *args, extra=extra)
# MARK: WARNING 30
def warning(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""warning"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.warning(msg, *args, extra=extra)
# MARK: ERROR 40
def error(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""error"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.error(msg, *args, extra=extra)
# MARK: CRITICAL 50
def critical(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""critcal"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.critical(msg, *args, extra=extra)
# MARK: ALERT 55
def alert(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""alert"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
# extra_dict = dict(extra)
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.log(LoggingLevel.ALERT.value, msg, *args, extra=extra)
# MARK: EMERGECNY: 60
def emergency(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""emergency"""
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.log(LoggingLevel.EMERGENCY.value, msg, *args, extra=extra)
# MARK: EXCEPTION: 70
def exception(self, msg: object, *args: object, extra: MutableMapping[str, object] | None = None) -> None:
"""
log on exceotion level
log on exceotion level, this is log.exception, but logs with a new level
Args:
msg (object): _description_
*args (object): arguments for msg
extra: Mapping[str, object] | None: extra arguments for the formatting if needed
"""
if self.logger is None:
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
if extra is None:
extra = {}
extra['stack_correct'] = traceback_call_str(start=3)
self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra)
# MARK: break line
@@ -377,7 +478,7 @@ class Log:
Keyword Arguments:
info {str} -- _description_ (default: {"BREAK"})
"""
if self.logger is None:
if not hasattr(self, 'logger'):
raise ValueError('Logger is not yet initialized')
self.logger.info("[%s] %s>", info, self.SPACER_CHAR * self.SPACER_LENGTH)

View File

@@ -17,7 +17,9 @@ class LoggingLevel(Enum):
WARNING = logging.WARNING # 30
ERROR = logging.ERROR # 40
CRITICAL = logging.CRITICAL # 50
EXCEPTION = 60 # 60 (manualy set)
ALERT = 55 # 55 (for Sys log)
EMERGENCY = 60 # 60 (for Sys log)
EXCEPTION = 70 # 70 (manualy set, error but with higher level)
# Alternative names
WARN = logging.WARN # 30 (alias for WARNING)
FATAL = logging.FATAL # 50 (alias for CRITICAL)
@@ -83,6 +85,6 @@ class LoggingLevel(Enum):
def is_lower_than(self, level: 'LoggingLevel'):
"""if given value is lower than set"""
return self.value > level.value
return self.value < level.value
# __END__

View File

@@ -2,7 +2,6 @@
String helpers
"""
from typing import Any
from decimal import Decimal, getcontext
from textwrap import shorten
@@ -102,62 +101,4 @@ def format_number(number: float, precision: int = 0) -> str:
"f}"
).format(_number)
def is_int(string: Any) -> bool:
"""
check if a value is int
Arguments:
string {Any} -- _description_
Returns:
bool -- _description_
"""
try:
int(string)
return True
except TypeError:
return False
except ValueError:
return False
def is_float(string: Any) -> bool:
"""
check if a value is float
Arguments:
string {Any} -- _description_
Returns:
bool -- _description_
"""
try:
float(string)
return True
except TypeError:
return False
except ValueError:
return False
def str_to_bool(string: str):
"""
convert string to bool
Arguments:
s {str} -- _description_
Raises:
ValueError: _description_
Returns:
_type_ -- _description_
"""
if string == "True" or string == "true":
return True
if string == "False" or string == "false":
return False
raise ValueError(f"Invalid boolean string: {string}")
# __END__

View File

View File

@@ -0,0 +1,65 @@
"""
variable convert, check, etc helepr
"""
from typing import Any
def is_int(string: Any) -> bool:
"""
check if a value is int
Arguments:
string {Any} -- _description_
Returns:
bool -- _description_
"""
try:
int(string)
return True
except TypeError:
return False
except ValueError:
return False
def is_float(string: Any) -> bool:
"""
check if a value is float
Arguments:
string {Any} -- _description_
Returns:
bool -- _description_
"""
try:
float(string)
return True
except TypeError:
return False
except ValueError:
return False
def str_to_bool(string: str):
"""
convert string to bool
Arguments:
s {str} -- _description_
Raises:
ValueError: _description_
Returns:
_type_ -- _description_
"""
if string == "True" or string == "true":
return True
if string == "False" or string == "false":
return False
raise ValueError(f"Invalid boolean string: {string}")
# __END__