cold
This commit is contained in:
118
AoM_Service/AoM_Configs/library/logger.py
Executable file
118
AoM_Service/AoM_Configs/library/logger.py
Executable file
@@ -0,0 +1,118 @@
|
||||
# logger.py
|
||||
|
||||
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
|
||||
logging.getLogger('requests').setLevel(logging.ERROR)
|
||||
logging.getLogger('urllib3').setLevel(logging.ERROR)
|
||||
logging.getLogger('werkzeug').setLevel(logging.ERROR)
|
||||
|
||||
|
||||
class SingleLevelFilter(logging.Filter):
|
||||
def __init__(self, passlevel, reject):
|
||||
"""
|
||||
initilizer(constructor) of the singlelevelfilter
|
||||
@param passlevel (int) - the int value of the level of the log
|
||||
@param reject (bool) - if true will return if the record level is not equal to the passlevel
|
||||
@return SingleLevelFilter object
|
||||
@note Sets some object parameters
|
||||
"""
|
||||
self.passlevel = passlevel
|
||||
self.reject = reject
|
||||
|
||||
def filter(self, record):
|
||||
"""
|
||||
Returns True/False depending on parameters
|
||||
@param record (Log int) - the record that the filter belongs to
|
||||
@return bool - True/False depending on what self.reject is set to and what record.levelno and self.passlevel are set to
|
||||
@note This causes either only logging of the exact same level to get logged, or only logging other than the same level to get logged
|
||||
"""
|
||||
if self.reject:
|
||||
return (record.levelno != self.passlevel)
|
||||
else:
|
||||
return (record.levelno == self.passlevel)
|
||||
|
||||
|
||||
class AlertLogging(logging.Logger):
|
||||
"""
|
||||
Class Object to handle the logging of the alert on metrics service
|
||||
starts at Error level and can flip on (and add) an additional log file and
|
||||
Debug logger as needed.
|
||||
"""
|
||||
|
||||
def __init__(self, name):
|
||||
"""
|
||||
Inits the formaters and logger
|
||||
"""
|
||||
self.name = name
|
||||
self.debug_formatter = logging.Formatter(
|
||||
"%(asctime)s - [%(levelname)s] - [%(module)s:%(lineno)d] - %(message)s", "%m-%d %H:%M:%S")
|
||||
|
||||
self.standard_formatter = logging.Formatter("%(asctime)s - [%(levelname)s] - %(message)s",
|
||||
"%m-%d %H:%M:%S")
|
||||
logging.getLogger()
|
||||
logging.Logger.__init__(self, name, logging.DEBUG)
|
||||
logging.setLoggerClass(AlertLogging)
|
||||
|
||||
def start(self):
|
||||
"""
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
info_handler = logging.StreamHandler()
|
||||
info_handler.setLevel(logging.INFO)
|
||||
info_handler.setFormatter(self.standard_formatter)
|
||||
self.addHandler(info_handler)
|
||||
return self
|
||||
|
||||
def start_log_file(self, file_path, mode='a'):
|
||||
"""
|
||||
Creates a separate log file handler
|
||||
Args:
|
||||
file_path: path to the log file
|
||||
mode: the type of mode to open the file handler with
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.log_path = file_path
|
||||
work_folder = os.path.dirname(file_path)
|
||||
if len(work_folder) > 0 and not os.path.exists(work_folder):
|
||||
os.makedirs(work_folder)
|
||||
self.log_handler = logging.FileHandler(file_path, mode)
|
||||
self.log_handler.setLevel(logging.DEBUG)
|
||||
self.log_handler.setFormatter(self.debug_formatter)
|
||||
self.addHandler(self.log_handler)
|
||||
|
||||
def stop_log_file(self):
|
||||
"""
|
||||
Closes Log file and sets the handler to None
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.log_handler.close()
|
||||
self.removeHandler(self.log_handler)
|
||||
self.log_handler = None
|
||||
|
||||
def start_debug(self):
|
||||
"""
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.debug_handler = logging.StreamHandler()
|
||||
self.debug_handler.setLevel(logging.DEBUG)
|
||||
self.debug_handler.addFilter(SingleLevelFilter(logging.DEBUG, False))
|
||||
self.debug_handler.setFormatter(self.debug_formatter)
|
||||
self.addHandler(self.debug_handler)
|
||||
|
||||
def stop_debug(self):
|
||||
"""
|
||||
stop the debugger
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.removeHandler(self.debug_handler)
|
||||
self.debug_handler = None
|
||||
Reference in New Issue
Block a user