|
@@ -5,20 +5,33 @@ import os.path
|
5
|
5
|
from Lodel.settings import Settings
|
6
|
6
|
|
7
|
7
|
# Variables & constants definitions
|
8
|
|
-default_format = '%(asctime)-15s %(levelname)s %(_pathname)s:%(_lineno)s:%(_funcName)s() %(message)s'
|
|
8
|
+default_format = '%(asctime)-15s %(levelname)s %(_pathname)s:%(_lineno)s:%(_funcName)s() : %(message)s'
|
|
9
|
+simple_format = '%(asctime)-15s %(levelname)s : %(message)s'
|
9
|
10
|
SECURITY_LOGLEVEL = 35
|
10
|
11
|
logging.addLevelName(SECURITY_LOGLEVEL, 'SECURITY')
|
11
|
|
-
|
12
|
|
-# Disabled, because the custom format raises error (enable to give the _ preffixed arguments to logger resulting into a KeyError exception )
|
13
|
|
-#logging.captureWarnings(True) # Log warnings
|
|
12
|
+handlers = dict() # Handlers list (generated from settings)
|
14
|
13
|
|
15
|
14
|
# Fetching default root logger
|
16
|
15
|
logger = logging.getLogger()
|
17
|
|
-logger.setLevel(logging.DEBUG)
|
18
|
16
|
|
19
|
17
|
# Setting options from Lodel.settings.Settings.logging
|
20
|
|
-for logging_opt in Settings.logging:
|
21
|
|
- print("Ben woot !")
|
|
18
|
+def __init_from_settings():
|
|
19
|
+ # Disabled, because the custom format raises error (enable to give the _ preffixed arguments to logger resulting into a KeyError exception )
|
|
20
|
+ #logging.captureWarnings(True) # Log warnings
|
|
21
|
+
|
|
22
|
+ logger.setLevel(logging.DEBUG)
|
|
23
|
+ for name, logging_opt in Settings.logging.items():
|
|
24
|
+ add_handler(name, logging_opt)
|
|
25
|
+
|
|
26
|
+## @brief Add an handler, identified by a name, to a given logger
|
|
27
|
+# @param logger logging.Logger : Basically the root logger
|
|
28
|
+# @param name str : The handler name
|
|
29
|
+# @param logging_opt dict : dict containing options ( see @ref jesaispasou_pour_les_details )
|
|
30
|
+def add_handler(name, logging_opt):
|
|
31
|
+ logger = logging.getLogger()
|
|
32
|
+ if name in handlers:
|
|
33
|
+ raise KeyError("A handler named '%s' allready exists")
|
|
34
|
+
|
22
|
35
|
if 'filename' in logging_opt:
|
23
|
36
|
maxBytes = (1024 * 10) if 'maxBytes' not in logging_opt else logging_opt['maxBytes']
|
24
|
37
|
backupCount = 10 if 'backupCount' not in logging_opt else logging_opt['backupCount']
|
|
@@ -37,10 +50,30 @@ for logging_opt in Settings.logging:
|
37
|
50
|
if 'format' in logging_opt:
|
38
|
51
|
formatter = logging.Formatter(logging_opt['format'])
|
39
|
52
|
else:
|
40
|
|
- formatter = logging.Formatter(default_format)
|
|
53
|
+ if 'context' in logging_opt and not logging_opt['context']:
|
|
54
|
+ formatter = logging.Formatter(simple_format)
|
|
55
|
+ else:
|
|
56
|
+ formatter = logging.Formatter(default_format)
|
41
|
57
|
|
42
|
58
|
handler.setFormatter(formatter)
|
|
59
|
+ handlers[name] = handler
|
43
|
60
|
logger.addHandler(handler)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+## @brief Remove an handler generated from configuration (runtime logger configuration)
|
|
64
|
+# @param name str : handler name
|
|
65
|
+def remove_handler(name):
|
|
66
|
+ if name in handlers:
|
|
67
|
+ logger.removeHandler(handlers[name])
|
|
68
|
+ # else: can we do anything ?
|
|
69
|
+
|
|
70
|
+## @brief Utility function that disable unconditionnaly handlers that implies console output
|
|
71
|
+# @note In fact, this function disables handlers generated from settings wich are instances of logging.StreamHandler
|
|
72
|
+def remove_console_handlers():
|
|
73
|
+ for name, handler in handlers.items():
|
|
74
|
+ if isinstance(handler, logging.StreamHandler):
|
|
75
|
+ remove_handler(name)
|
|
76
|
+
|
44
|
77
|
|
45
|
78
|
# Utility functions
|
46
|
79
|
|
|
@@ -52,7 +85,10 @@ for logging_opt in Settings.logging:
|
52
|
85
|
def log(lvl, msg, *args, **kwargs):
|
53
|
86
|
caller = logger.findCaller() # Opti warning : small overhead
|
54
|
87
|
extra = {
|
55
|
|
- '_pathname': os.path.relpath(caller[0]), # os.path.relpath add another small overhead
|
|
88
|
+ '_pathname': os.path.relpath(
|
|
89
|
+ caller[0],
|
|
90
|
+ start=Settings.lodel2_lib_path
|
|
91
|
+ ), # os.path.relpath add another small overhead
|
56
|
92
|
'_lineno': caller[1],
|
57
|
93
|
'_funcName': caller[2],
|
58
|
94
|
}
|
|
@@ -64,3 +100,7 @@ def warning(msg, *args, **kwargs): log(logging.WARNING, msg, *args, **kwargs)
|
64
|
100
|
def security(msg, *args, **kwargs): log(SECURITY_LOGLEVEL, msg, *args, **kwargs)
|
65
|
101
|
def error(msg, *args, **kwargs): log(logging.ERROR, msg, *args, **kwargs)
|
66
|
102
|
def critical(msg, *args, **kwargs): log(logging.CRITICAL, msg, *args, **kwargs)
|
|
103
|
+
|
|
104
|
+# Initialisation triggering
|
|
105
|
+if len(handlers) == 0:
|
|
106
|
+ __init_from_settings()
|