mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-04 09:11:34 -07:00
BE: mylog support non-standard levels
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import conf
|
|||||||
from const import logPath
|
from const import logPath
|
||||||
from utils.datetime_utils import timeNowTZ
|
from utils.datetime_utils import timeNowTZ
|
||||||
|
|
||||||
|
DEFAULT_LEVEL = "none"
|
||||||
|
|
||||||
# -------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------
|
||||||
# Map custom debug levels to Python logging levels
|
# Map custom debug levels to Python logging levels
|
||||||
@@ -48,6 +49,11 @@ class MyLogHandler(logging.Handler):
|
|||||||
class Logger:
|
class Logger:
|
||||||
def __init__(self, LOG_LEVEL):
|
def __init__(self, LOG_LEVEL):
|
||||||
global currentLevel
|
global currentLevel
|
||||||
|
|
||||||
|
# Normalize invalid levels to "none"
|
||||||
|
if LOG_LEVEL not in dict(debugLevels):
|
||||||
|
LOG_LEVEL = DEFAULT_LEVEL
|
||||||
|
|
||||||
currentLevel = LOG_LEVEL
|
currentLevel = LOG_LEVEL
|
||||||
conf.LOG_LEVEL = currentLevel
|
conf.LOG_LEVEL = currentLevel
|
||||||
|
|
||||||
@@ -59,10 +65,11 @@ class Logger:
|
|||||||
self.setup_logging()
|
self.setup_logging()
|
||||||
|
|
||||||
def _to_num(self, level_str):
|
def _to_num(self, level_str):
|
||||||
for lvl in debugLevels:
|
for name, num in debugLevels:
|
||||||
if level_str == lvl[0]:
|
if level_str == name:
|
||||||
return lvl[1]
|
return num
|
||||||
return None
|
# Non-existing levels → "none"
|
||||||
|
return 0
|
||||||
|
|
||||||
def setup_logging(self):
|
def setup_logging(self):
|
||||||
root_logger = logging.getLogger()
|
root_logger = logging.getLogger()
|
||||||
@@ -84,8 +91,7 @@ class Logger:
|
|||||||
file_print(*args)
|
file_print(*args)
|
||||||
|
|
||||||
def isAbove(self, requestedDebugLevel):
|
def isAbove(self, requestedDebugLevel):
|
||||||
reqLvl = self._to_num(requestedDebugLevel)
|
return self.setLvl >= self._to_num(requestedDebugLevel)
|
||||||
return reqLvl is not None and self.setLvl is not None and self.setLvl >= reqLvl
|
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user