Blame view

src/pyros_logger.py 8.45 KB
f5c731dd   Etienne Pallier   New pyros logger ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3

"""LOGGER

"""


# Standard library imports
import logging
# https://docs.python.org/3/library/logging.handlers.html#module-logging.handlers
from logging.handlers import SMTPHandler, TimedRotatingFileHandler
import sys
import os
#import logging_tree


# Third party imports
# None

# Local application imports
# None

# Exported Objects (to modules that use this logger)
log: logging.Logger = None


# ./../logs/
#LOGFILES_PATH = __file__ + os.sep + '..' + os.sep + '..' + os.sep + 'logs'
#LOGFILES_PATH = os.path.join(os.path.dirname(__file__), '..', 'logs')
_LOGFILES_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'logs')
#LOGFILES_PATH = LOGFILES_PATH + os.sep + 'logs'


# https://docs.python.org/3/howto/logging-cookbook.html#filters-contextual
7f6e4bdc   Etienne Pallier   Améliorations log...
35
class _FilterOnDebugModeAndNoLevelINFO(logging.Filter):
f5c731dd   Etienne Pallier   New pyros logger ...
36
37
38
39
40
41
42
43
44

    def filter(self, logRecord):
        debug_mode = int(os.environ.get('PYROS_DEBUG', 0))
        #print("debug_mode:", debug_mode)
        #print(debug_mode)
        #return True
        # 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 
7f6e4bdc   Etienne Pallier   Améliorations log...
45
46
47
        if logging.getLevelName(logRecord.levelno) == 'INFO': return False 
        if logging.getLevelName(logRecord.levelno) == 'DEBUG': return (debug_mode == 1)
        return True
f5c731dd   Etienne Pallier   New pyros logger ...
48
49
50
51
        # On n'affiche sur la console QUE si mode DEBUG is ON
        #return (debug_mode == 1)


7f6e4bdc   Etienne Pallier   Améliorations log...
52
class _FilterOnLogLevelMax(logging.Filter):
f5c731dd   Etienne Pallier   New pyros logger ...
53
54
55
56
57
58
59
60

    def __init__(self, level):
        self.__level_max = level

    def filter(self, logRecord):
        return logRecord.levelno <= self.__level_max


7f6e4bdc   Etienne Pallier   Améliorations log...
61
def _formatter_full():
83b5ebb3   Etienne Pallier   Amelioration log
62
    return logging.Formatter('%(asctime)s - %(levelname)-8s - %(filename)-12s - %(processName)s - %(threadName)s : %(lineno)-5s - %(message)s')
7f6e4bdc   Etienne Pallier   Améliorations log...
63
64
65
66
67
68

def _formatter_mini():
    return logging.Formatter('%(message)s')


def _handler_console(minloglevel: int, a_filter: logging.Filter, formatter: logging.Formatter):
f5c731dd   Etienne Pallier   New pyros logger ...
69
70
71
72
73
74
75
76
    # Console should print ALL messages (starting from lower level DEBUG)
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(minloglevel)
    #handler.setLevel(logging.DEBUG)
    #formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
    #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
7f6e4bdc   Etienne Pallier   Améliorations log...
77
    ##formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(processName)s - %(threadName)s : %(lineno)s - %(message)s')
f5c731dd   Etienne Pallier   New pyros logger ...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    handler.setFormatter(formatter)
    handler.addFilter(a_filter)
    #handler.addFilter(DebugIsOnFilter())
    return handler


def _handler_file(minloglevel: int, suffix: str, filepath: str, a_filter: logging.Filter=None):
    # Log File should contain ONLY messages starting from level WARNING (ERROR ?)
    #f_handler = logging.FileHandler('file.log')
    handler = TimedRotatingFileHandler(filepath+os.sep + f"pyroslog_{suffix}.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')
    #f_format = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)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)
    if a_filter: handler.addFilter(a_filter)
    return handler


# public for Agent.py
def handler_filebyagent(minloglevel: int, agent_name: str, filepath: str=None):
    if filepath is None: filepath = _LOGFILES_PATH
7f6e4bdc   Etienne Pallier   Améliorations log...
101
102
103
104
    # Create agent folder if does not exist
    agentDir = os.path.join(filepath, agent_name)
    while not os.path.isdir(agentDir): os.mkdir(agentDir)

f5c731dd   Etienne Pallier   New pyros logger ...
105
106
107
    # 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')
7f6e4bdc   Etienne Pallier   Améliorations log...
108
109
    #handler = TimedRotatingFileHandler(filepath+os.sep+agent_name+'.log', when='midnight')
    handler = TimedRotatingFileHandler(os.path.join(agentDir,agent_name+'.log'), when='midnight')
f5c731dd   Etienne Pallier   New pyros logger ...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    handler.setLevel(minloglevel)
    # Create formatter and add it to handler
    #f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    #f_format = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)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)
    return handler


def _handler_db():
    # DB should contain ONLY messages starting from level ERROR
    # TODO:
    pass


def _handler_email(minloglevel: int):
    # Email should be sent ONLY for messages starting from level CRITICAL
    # http://sametmax.com/envoi-dun-email-par-logging-en-cas-de-plantage-dun-script-python-ou-comment-faire-bouffer-uxe9-a-smtphandler/
    # TODO: a ameliorer avec article ci-dessus pour UNICODE
    m_handler = SMTPHandler(
        # Host et port
        ('SMTP.GMAIL.COM', 587),
        # From
        "MOI@GMAIL.COM",
        # To (liste)
        ["QUELQU.UN@QUELQUE.PART"],
        # Sujet du message
        "Erreur critique dans module MMM",
        # pour l'authentification
        credentials = ("MONEMAIL@GMAIL.COM", "MONSUPERPASSWORD"),
        secure = ()
    )
    m_handler.setLevel(minloglevel)
    m_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    m_handler.setFormatter(m_format)
    return m_handler




#def get_logger(logger_name: str='pyroslogger'):
###def set_logger_config(logger_name: str='pyroslogger'):

###global logger


# Basic configuration
'''
#logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG, filename='client.log', filemode='a', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.debug("Client instanciated")
'''

# Advanced configuration
# https://docs.python.org/3/library/logging.html#logging.getLogger
#logger = logging.getLogger(__name__)
##logger = logging.getLogger(log_name)
log = logging.getLogger('pyroslogger')

# If not set, default level is NOTSET (all messages are logged)
# The defined levels, in order of increasing severity : DEBUG, INFO, WARNING, ERROR, CRITICAL    
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
7f6e4bdc   Etienne Pallier   Améliorations log...
175
176
177
178
179
180
181
# - 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
f5c731dd   Etienne Pallier   New pyros logger ...
182
183
184
185
186
187
188
189
190
191
192
193
log.addHandler(_handler_file(logging.WARN, 'error', _LOGFILES_PATH))
##logger.addHandler(handler_filebyagent(logging.INFO, LOGFILES_PATH))
# TODO:
####logger.addHandler(get_email_handler(logging.ERROR))
##logger.addHandler(db_handler)

# with this pattern, it's rarely necessary to propagate the error up to parent
##logger.propagate = False

##return logger


83b5ebb3   Etienne Pallier   Amelioration log
194
log.debug("log files are logged to:" + _LOGFILES_PATH)
f5c731dd   Etienne Pallier   New pyros logger ...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220


'''
# Shortcuts for logger:
#def log_d(msg:str): log.debug(msg)
def log_d(msg:str, *args, **kwargs): log.debug(msg, *args, **kwargs)
def log_i(msg:str, *args, **kwargs): log.info(msg, *args, **kwargs)
def log_w(msg:str, *args, **kwargs): log.warning(msg, *args, **kwargs)
def log_e(msg:str, *args, **kwargs): log.error(msg, *args, **kwargs)
def log_c(msg:str, *args, **kwargs): log.critical(msg, *args, **kwargs)
'''


if __name__ == "__main__":

    set_logger_config()
    #my_logger = get_logger("my_module_name")
    #my_logger = get_logger()

    #logging_tree.printout()  

    log.debug("a debug message")
    log.info("a info message")
    log.warning("a warn message")
    log.error("a error message")
    log.critical("a critical message")