From dd1580e536e45f7157d70c01ee7fa51b649d7976 Mon Sep 17 00:00:00 2001 From: jokob-sk Date: Fri, 24 Jan 2025 18:58:10 +1100 Subject: [PATCH] asus and logging #972 --- front/plugins/dhcp_leases/ASUS_ROUTERS.md | 4 +- server/logger.py | 63 ++++++++++++++++++----- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/front/plugins/dhcp_leases/ASUS_ROUTERS.md b/front/plugins/dhcp_leases/ASUS_ROUTERS.md index 747c077f..67fc6e59 100755 --- a/front/plugins/dhcp_leases/ASUS_ROUTERS.md +++ b/front/plugins/dhcp_leases/ASUS_ROUTERS.md @@ -92,5 +92,5 @@ dnsmasq.leases ## Other Info -Publishing date: 22.1.2025 -Author: [EinKantHolz - odin](https://github.com/EinKantHolz) \ No newline at end of file +- Publishing date: 22.1.2025 +- Author: [EinKantHolz - odin](https://github.com/EinKantHolz) \ No newline at end of file diff --git a/server/logger.py b/server/logger.py index 8a8a4205..cf578cd5 100755 --- a/server/logger.py +++ b/server/logger.py @@ -4,6 +4,10 @@ import datetime import threading import queue import time +import logging + +# NetAlertX imports + import conf from const import * @@ -16,6 +20,16 @@ def timeNowTZ(): else: return datetime.datetime.now().replace(microsecond=0) +#------------------------------------------------------------------------------- +# Map custom debug levels to Python logging levels +custom_to_logging_levels = { + 'none': logging.NOTSET, + 'minimal': logging.WARNING, + 'verbose': logging.INFO, + 'debug': logging.DEBUG, + 'trace': logging.DEBUG, # Can map to DEBUG or lower custom level if needed +} + #------------------------------------------------------------------------------- # More verbose as the numbers go up debugLevels = [ @@ -25,6 +39,10 @@ debugLevels = [ # use the LOG_LEVEL from the config, may be overridden currentLevel = conf.LOG_LEVEL +# tracking log levels +setLvl = 0 +reqLvl = 0 + #------------------------------------------------------------------------------- class Logger: def __init__(self, LOG_LEVEL='verbose'): @@ -32,10 +50,36 @@ class Logger: currentLevel = LOG_LEVEL + # Automatically set up custom logging handler + self.setup_logging() + + def setup_logging(self): + root_logger = logging.getLogger() + # Clear existing handlers to prevent duplicates + if root_logger.hasHandlers(): + root_logger.handlers.clear() + + # Create the custom handler + my_log_handler = MyLogHandler() + # my_log_handler.setLevel(custom_to_logging_levels.get(currentLevel, logging.NOTSET)) + + # Optional: Add a formatter for consistent log message format + # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + formatter = logging.Formatter('%(message)s', datefmt='%H:%M:%S') + my_log_handler.setFormatter(formatter) + + # Attach the handler to the root logger + root_logger.addHandler(my_log_handler) + root_logger.setLevel(custom_to_logging_levels.get(currentLevel, logging.NOTSET)) + +# for python logging +class MyLogHandler(logging.Handler): + def emit(self, record): + log_entry = self.format(record) + log_queue.put(log_entry) def mylog(requestedDebugLevel, n): - setLvl = 0 - reqLvl = 0 + global setLvl, reqLvl # Get debug urgency/relative weight for lvl in debugLevels: @@ -86,23 +130,14 @@ def file_print(*args): result = timeNowTZ().strftime('%H:%M:%S') + ' ' for arg in args: - result += str(arg) + result += str(arg) + + logging.log(custom_to_logging_levels.get(currentLevel, logging.NOTSET), result) # Forward to Python's logging system print(result) # Ensure the log writer thread is running start_log_writer_thread() - # Queue the log entry for writing - append_to_file_with_timeout( result, 5) - -#------------------------------------------------------------------------------- -# Function to append to the file with a timeout -def append_to_file_with_timeout(data, timeout): - try: - log_queue.put_nowait(data) - except queue.Full: - print("Log queue is full, dropping log entry:" + data) - #------------------------------------------------------------------------------- def print_log(pText): # Check if logging is active