diff --git a/CHANGELOG b/CHANGELOG index aaa72d2..1b6b85e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,10 @@ 05-01-2022 (EP) : + - log agents : 1 dossier par agent + - log.info => format simplifié = print() + +05-01-2022 (EP) : - New pyros logger : src/pyros_logger.py 08-12-2021 (EP) : diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index 066c114..4a595cc 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -574,6 +574,7 @@ class Agent: log.debug(obs_config_file_path) log.debug(path_to_obs_config_folder) log.debug(unit) + log.warning("petit warning bidon") print("\n") print("- Observatory:" + oc.get_obs_name()) my_unit_name = oc.get_units_name()[0] diff --git a/src/core/pyros_django/pyros/settings.py b/src/core/pyros_django/pyros/settings.py index 572628a..e3a4256 100644 --- a/src/core/pyros_django/pyros/settings.py +++ b/src/core/pyros_django/pyros/settings.py @@ -404,6 +404,6 @@ python_version = subprocess.run( "python --version | cut -d ' ' -f 2 | cut -d '. python_version = python_version.stdout today = "2021-11-09" django_version_major,django_version_minor = django.VERSION[:2][0],django.VERSION[:2][1] -pyros_version = "0.3.1.0" +pyros_version = "0.3.1.1" #pyros_version = "0.2.12.0" VERSION_NUMBER = f"{pyros_version}_{django_version_major}.{django_version_minor}_{python_version}_{today}" diff --git a/src/pyros_logger.py b/src/pyros_logger.py index 4eda813..202fb0f 100755 --- a/src/pyros_logger.py +++ b/src/pyros_logger.py @@ -32,7 +32,7 @@ _LOGFILES_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', # https://docs.python.org/3/howto/logging-cookbook.html#filters-contextual -class _FilterOnDebugMode(logging.Filter): +class _FilterOnDebugModeAndNoLevelINFO(logging.Filter): def filter(self, logRecord): debug_mode = int(os.environ.get('PYROS_DEBUG', 0)) @@ -42,13 +42,14 @@ class _FilterOnDebugMode(logging.Filter): # On affiche TOUJOURS sur la console SAUF level debug si mode DEBUG is OFF #print("level name", logging.getLevelName(logRecord.levelno)) #if logRecord.levelno != 10: return True - if logging.getLevelName(logRecord.levelno) != 'DEBUG': return True - return (debug_mode == 1) + if logging.getLevelName(logRecord.levelno) == 'INFO': return False + if logging.getLevelName(logRecord.levelno) == 'DEBUG': return (debug_mode == 1) + return True # On n'affiche sur la console QUE si mode DEBUG is ON #return (debug_mode == 1) -class FilterOnLogLevelMax(logging.Filter): +class _FilterOnLogLevelMax(logging.Filter): def __init__(self, level): self.__level_max = level @@ -57,7 +58,14 @@ class FilterOnLogLevelMax(logging.Filter): return logRecord.levelno <= self.__level_max -def handler_console(minloglevel: int, a_filter: logging.Filter): +def _formatter_full(): + return logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s') + +def _formatter_mini(): + return logging.Formatter('%(message)s') + + +def _handler_console(minloglevel: int, a_filter: logging.Filter, formatter: logging.Formatter): # Console should print ALL messages (starting from lower level DEBUG) handler = logging.StreamHandler(sys.stdout) handler.setLevel(minloglevel) @@ -66,7 +74,7 @@ def handler_console(minloglevel: int, a_filter: logging.Filter): #formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s : %(lineno)s - %(message)s') #formatter = logging.Formatter('%(name)-12s - %(relativeCreated)5d - %(asctime)s - %(levelname)-8s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s') # pyroslogger - 3364 - 2022-01-04 11:41:59,672 - INFO - Agent.py - MainProcess - MainThread : 999 - in set active - formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s') + ##formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s') handler.setFormatter(formatter) handler.addFilter(a_filter) #handler.addFilter(DebugIsOnFilter()) @@ -90,10 +98,15 @@ def _handler_file(minloglevel: int, suffix: str, filepath: str, a_filter: loggin # public for Agent.py def handler_filebyagent(minloglevel: int, agent_name: str, filepath: str=None): if filepath is None: filepath = _LOGFILES_PATH + # Create agent folder if does not exist + agentDir = os.path.join(filepath, agent_name) + while not os.path.isdir(agentDir): os.mkdir(agentDir) + # Log File should contain ONLY messages starting from level WARNING (ERROR ?) #f_handler = logging.FileHandler('file.log') #f_handler = TimedRotatingFileHandler(filepath+os.sep+'agentslog.log', when='midnight') - handler = TimedRotatingFileHandler(filepath+os.sep+agent_name+'.log', when='midnight') + #handler = TimedRotatingFileHandler(filepath+os.sep+agent_name+'.log', when='midnight') + handler = TimedRotatingFileHandler(os.path.join(agentDir,agent_name+'.log'), when='midnight') handler.setLevel(minloglevel) # Create formatter and add it to handler #f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') @@ -159,8 +172,13 @@ log.setLevel(logging.DEBUG) # better to have too much log than not enough #log.setLevel(logging.ERROR) # better to have too much log than not enough # Create and add handlers to the logger -log.addHandler(handler_console(logging.DEBUG, _FilterOnDebugMode())) -log.addHandler(_handler_file(logging.INFO, 'info', _LOGFILES_PATH, FilterOnLogLevelMax(logging.INFO))) +# - log to console for all levels except "info" (and "debug" only if DEBUG is ON) +log.addHandler(_handler_console(logging.DEBUG, _FilterOnDebugModeAndNoLevelINFO(), _formatter_full())) +# - log to console only for "info" level +log.addHandler(_handler_console(logging.INFO, _FilterOnLogLevelMax(logging.INFO), _formatter_mini())) +# - log to file only for "info" level +log.addHandler(_handler_file(logging.INFO, 'info', _LOGFILES_PATH, _FilterOnLogLevelMax(logging.INFO))) +# - log to file only from "warning" level log.addHandler(_handler_file(logging.WARN, 'error', _LOGFILES_PATH)) ##logger.addHandler(handler_filebyagent(logging.INFO, LOGFILES_PATH)) # TODO: -- libgit2 0.21.2