Commit 7f6e4bdc18a812d00c0f910a18ffcb911a66eace

Authored by Etienne Pallier
1 parent ad746b1f
Exists in dev

Améliorations logger :

- log agents : 1 dossier par agent
- log.info => format simplifié = print()

v0.3.1.1
CHANGELOG
1 1  
2 2  
3 3 05-01-2022 (EP) :
  4 + - log agents : 1 dossier par agent
  5 + - log.info => format simplifié = print()
  6 +
  7 +05-01-2022 (EP) :
4 8 - New pyros logger : src/pyros_logger.py
5 9  
6 10 08-12-2021 (EP) :
... ...
src/core/pyros_django/agent/Agent.py
... ... @@ -574,6 +574,7 @@ class Agent:
574 574 log.debug(obs_config_file_path)
575 575 log.debug(path_to_obs_config_folder)
576 576 log.debug(unit)
  577 + log.warning("petit warning bidon")
577 578 print("\n")
578 579 print("- Observatory:" + oc.get_obs_name())
579 580 my_unit_name = oc.get_units_name()[0]
... ...
src/core/pyros_django/pyros/settings.py
... ... @@ -404,6 +404,6 @@ python_version = subprocess.run( "python --version | cut -d ' ' -f 2 | cut -d '.
404 404 python_version = python_version.stdout
405 405 today = "2021-11-09"
406 406 django_version_major,django_version_minor = django.VERSION[:2][0],django.VERSION[:2][1]
407   -pyros_version = "0.3.1.0"
  407 +pyros_version = "0.3.1.1"
408 408 #pyros_version = "0.2.12.0"
409 409 VERSION_NUMBER = f"{pyros_version}_{django_version_major}.{django_version_minor}_{python_version}_{today}"
... ...
src/pyros_logger.py
... ... @@ -32,7 +32,7 @@ _LOGFILES_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..',
32 32  
33 33  
34 34 # https://docs.python.org/3/howto/logging-cookbook.html#filters-contextual
35   -class _FilterOnDebugMode(logging.Filter):
  35 +class _FilterOnDebugModeAndNoLevelINFO(logging.Filter):
36 36  
37 37 def filter(self, logRecord):
38 38 debug_mode = int(os.environ.get('PYROS_DEBUG', 0))
... ... @@ -42,13 +42,14 @@ class _FilterOnDebugMode(logging.Filter):
42 42 # On affiche TOUJOURS sur la console SAUF level debug si mode DEBUG is OFF
43 43 #print("level name", logging.getLevelName(logRecord.levelno))
44 44 #if logRecord.levelno != 10: return True
45   - if logging.getLevelName(logRecord.levelno) != 'DEBUG': return True
46   - return (debug_mode == 1)
  45 + if logging.getLevelName(logRecord.levelno) == 'INFO': return False
  46 + if logging.getLevelName(logRecord.levelno) == 'DEBUG': return (debug_mode == 1)
  47 + return True
47 48 # On n'affiche sur la console QUE si mode DEBUG is ON
48 49 #return (debug_mode == 1)
49 50  
50 51  
51   -class FilterOnLogLevelMax(logging.Filter):
  52 +class _FilterOnLogLevelMax(logging.Filter):
52 53  
53 54 def __init__(self, level):
54 55 self.__level_max = level
... ... @@ -57,7 +58,14 @@ class FilterOnLogLevelMax(logging.Filter):
57 58 return logRecord.levelno <= self.__level_max
58 59  
59 60  
60   -def handler_console(minloglevel: int, a_filter: logging.Filter):
  61 +def _formatter_full():
  62 + return logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s')
  63 +
  64 +def _formatter_mini():
  65 + return logging.Formatter('%(message)s')
  66 +
  67 +
  68 +def _handler_console(minloglevel: int, a_filter: logging.Filter, formatter: logging.Formatter):
61 69 # Console should print ALL messages (starting from lower level DEBUG)
62 70 handler = logging.StreamHandler(sys.stdout)
63 71 handler.setLevel(minloglevel)
... ... @@ -66,7 +74,7 @@ def handler_console(minloglevel: int, a_filter: logging.Filter):
66 74 #formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s : %(lineno)s - %(message)s')
67 75 #formatter = logging.Formatter('%(name)-12s - %(relativeCreated)5d - %(asctime)s - %(levelname)-8s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s')
68 76 # pyroslogger - 3364 - 2022-01-04 11:41:59,672 - INFO - Agent.py - MainProcess - MainThread : 999 - in set active
69   - formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s')
  77 + ##formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s')
70 78 handler.setFormatter(formatter)
71 79 handler.addFilter(a_filter)
72 80 #handler.addFilter(DebugIsOnFilter())
... ... @@ -90,10 +98,15 @@ def _handler_file(minloglevel: int, suffix: str, filepath: str, a_filter: loggin
90 98 # public for Agent.py
91 99 def handler_filebyagent(minloglevel: int, agent_name: str, filepath: str=None):
92 100 if filepath is None: filepath = _LOGFILES_PATH
  101 + # Create agent folder if does not exist
  102 + agentDir = os.path.join(filepath, agent_name)
  103 + while not os.path.isdir(agentDir): os.mkdir(agentDir)
  104 +
93 105 # Log File should contain ONLY messages starting from level WARNING (ERROR ?)
94 106 #f_handler = logging.FileHandler('file.log')
95 107 #f_handler = TimedRotatingFileHandler(filepath+os.sep+'agentslog.log', when='midnight')
96   - handler = TimedRotatingFileHandler(filepath+os.sep+agent_name+'.log', when='midnight')
  108 + #handler = TimedRotatingFileHandler(filepath+os.sep+agent_name+'.log', when='midnight')
  109 + handler = TimedRotatingFileHandler(os.path.join(agentDir,agent_name+'.log'), when='midnight')
97 110 handler.setLevel(minloglevel)
98 111 # Create formatter and add it to handler
99 112 #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
159 172 #log.setLevel(logging.ERROR) # better to have too much log than not enough
160 173  
161 174 # Create and add handlers to the logger
162   -log.addHandler(handler_console(logging.DEBUG, _FilterOnDebugMode()))
163   -log.addHandler(_handler_file(logging.INFO, 'info', _LOGFILES_PATH, FilterOnLogLevelMax(logging.INFO)))
  175 +# - log to console for all levels except "info" (and "debug" only if DEBUG is ON)
  176 +log.addHandler(_handler_console(logging.DEBUG, _FilterOnDebugModeAndNoLevelINFO(), _formatter_full()))
  177 +# - log to console only for "info" level
  178 +log.addHandler(_handler_console(logging.INFO, _FilterOnLogLevelMax(logging.INFO), _formatter_mini()))
  179 +# - log to file only for "info" level
  180 +log.addHandler(_handler_file(logging.INFO, 'info', _LOGFILES_PATH, _FilterOnLogLevelMax(logging.INFO)))
  181 +# - log to file only from "warning" level
164 182 log.addHandler(_handler_file(logging.WARN, 'error', _LOGFILES_PATH))
165 183 ##logger.addHandler(handler_filebyagent(logging.INFO, LOGFILES_PATH))
166 184 # TODO:
... ...