Skip to content

Commit

Permalink
asus and logging #972
Browse files Browse the repository at this point in the history
  • Loading branch information
jokob-sk committed Jan 24, 2025
1 parent 630e4f6 commit dd1580e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
4 changes: 2 additions & 2 deletions front/plugins/dhcp_leases/ASUS_ROUTERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ dnsmasq.leases

## Other Info

Publishing date: 22.1.2025
Author: [EinKantHolz - odin](https://github.com/EinKantHolz)
- Publishing date: 22.1.2025
- Author: [EinKantHolz - odin](https://github.com/EinKantHolz)
63 changes: 49 additions & 14 deletions server/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import threading
import queue
import time
import logging

# NetAlertX imports

import conf
from const import *

Expand All @@ -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 = [
Expand All @@ -25,17 +39,47 @@ def timeNowTZ():
# 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'):
global currentLevel

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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dd1580e

Please sign in to comment.