|
@@ -1,8 +1,8 @@
|
1
|
1
|
#-*- coding: utf-8 -*-
|
2
|
2
|
|
|
3
|
+import copy
|
3
|
4
|
import logging, logging.handlers
|
4
|
5
|
import os.path
|
5
|
|
-from lodel.settings import Settings
|
6
|
6
|
|
7
|
7
|
# Variables & constants definitions
|
8
|
8
|
default_format = '%(asctime)-15s %(levelname)s %(_pathname)s:%(_lineno)s:%(_funcName)s() : %(message)s'
|
|
@@ -10,12 +10,22 @@ simple_format = '%(asctime)-15s %(levelname)s : %(message)s'
|
10
|
10
|
SECURITY_LOGLEVEL = 35
|
11
|
11
|
logging.addLevelName(SECURITY_LOGLEVEL, 'SECURITY')
|
12
|
12
|
handlers = dict() # Handlers list (generated from settings)
|
|
13
|
+##@brief Stores sent messages until module is able to be initialized
|
|
14
|
+msg_buffer = []
|
13
|
15
|
|
14
|
16
|
# Fetching default root logger
|
15
|
17
|
logger = logging.getLogger()
|
16
|
18
|
|
17
|
|
-# Setting options from Lodel.settings.Settings.logging
|
|
19
|
+##@brief Module initialisation from settings
|
|
20
|
+#@return True if inited else False
|
18
|
21
|
def __init_from_settings():
|
|
22
|
+ try:
|
|
23
|
+ from lodel.settings import Settings
|
|
24
|
+ except Exception:
|
|
25
|
+ return False
|
|
26
|
+ from lodel.settings.settings import Settings as Lodel2Settings
|
|
27
|
+ if not Lodel2Settings.started():
|
|
28
|
+ return False
|
19
|
29
|
# capture warning disabled, because the custom format raises error (unable
|
20
|
30
|
# to give the _ preffixed arguments to logger resulting into a KeyError
|
21
|
31
|
# exception )
|
|
@@ -24,6 +34,7 @@ def __init_from_settings():
|
24
|
34
|
logger.setLevel(logging.DEBUG)
|
25
|
35
|
for name in Settings.logging._fields:
|
26
|
36
|
add_handler(name, getattr(Settings.logging, name))
|
|
37
|
+ return True
|
27
|
38
|
|
28
|
39
|
|
29
|
40
|
##@brief Add an handler, identified by a name, to a given logger
|
|
@@ -85,8 +96,9 @@ def remove_console_handlers():
|
85
|
96
|
if isinstance(handler, logging.StreamHandler):
|
86
|
97
|
remove_handler(name)
|
87
|
98
|
|
88
|
|
-
|
89
|
|
-# Utility functions
|
|
99
|
+#####################
|
|
100
|
+# Utility functions #
|
|
101
|
+#####################
|
90
|
102
|
|
91
|
103
|
##@brief Generic logging function
|
92
|
104
|
# @param lvl int : Log severity
|
|
@@ -94,6 +106,15 @@ def remove_console_handlers():
|
94
|
106
|
# @param *args : additional positionnal arguments
|
95
|
107
|
# @param **kwargs : additional named arguments
|
96
|
108
|
def log(lvl, msg, *args, **kwargs):
|
|
109
|
+ if len(handlers) == 0: #late initialisation
|
|
110
|
+ if not __init_from_settings():
|
|
111
|
+ s_kwargs = copy.copy(kwargs)
|
|
112
|
+ s_kwargs.update({'lvl': lvl, 'msg':msg})
|
|
113
|
+ msg_buffer.append((s_kwargs, args))
|
|
114
|
+ return
|
|
115
|
+ else:
|
|
116
|
+ for s_kwargs, args in msg_buffer:
|
|
117
|
+ log(*args, **s_kwargs)
|
97
|
118
|
caller = logger.findCaller() # Opti warning : small overhead
|
98
|
119
|
extra = {
|
99
|
120
|
'_pathname': os.path.abspath(caller[0]),
|
|
@@ -109,6 +130,3 @@ def security(msg, *args, **kwargs): log(SECURITY_LOGLEVEL, msg, *args, **kwargs)
|
109
|
130
|
def error(msg, *args, **kwargs): log(logging.ERROR, msg, *args, **kwargs)
|
110
|
131
|
def critical(msg, *args, **kwargs): log(logging.CRITICAL, msg, *args, **kwargs)
|
111
|
132
|
|
112
|
|
-# Initialisation triggering
|
113
|
|
-if len(handlers) == 0:
|
114
|
|
- __init_from_settings()
|