|
@@ -0,0 +1,66 @@
|
|
1
|
+#-*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+import logging, logging.handlers
|
|
4
|
+import os.path
|
|
5
|
+from Lodel.settings import Settings
|
|
6
|
+
|
|
7
|
+# Variables & constants definitions
|
|
8
|
+default_format = '%(asctime)-15s %(levelname)s %(_pathname)s:%(_lineno)s:%(_funcName)s() %(message)s'
|
|
9
|
+SECURITY_LOGLEVEL = 35
|
|
10
|
+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
|
|
14
|
+
|
|
15
|
+# Fetching default root logger
|
|
16
|
+logger = logging.getLogger()
|
|
17
|
+logger.setLevel(logging.DEBUG)
|
|
18
|
+
|
|
19
|
+# Setting options from Lodel.settings.Settings.logging
|
|
20
|
+for logging_opt in Settings.logging:
|
|
21
|
+ print("Ben woot !")
|
|
22
|
+ if 'filename' in logging_opt:
|
|
23
|
+ maxBytes = (1024 * 10) if 'maxBytes' not in logging_opt else logging_opt['maxBytes']
|
|
24
|
+ backupCount = 10 if 'backupCount' not in logging_opt else logging_opt['backupCount']
|
|
25
|
+
|
|
26
|
+ handler = logging.handlers.RotatingFileHandler(
|
|
27
|
+ logging_opt['filename'],
|
|
28
|
+ maxBytes = maxBytes,
|
|
29
|
+ backupCount = backupCount,
|
|
30
|
+ encoding = 'utf-8')
|
|
31
|
+ else:
|
|
32
|
+ handler = logging.StreamHandler()
|
|
33
|
+
|
|
34
|
+ if 'level' in logging_opt:
|
|
35
|
+ handler.setLevel(getattr(logging, logging_opt['level'].upper()))
|
|
36
|
+
|
|
37
|
+ if 'format' in logging_opt:
|
|
38
|
+ formatter = logging.Formatter(logging_opt['format'])
|
|
39
|
+ else:
|
|
40
|
+ formatter = logging.Formatter(default_format)
|
|
41
|
+
|
|
42
|
+ handler.setFormatter(formatter)
|
|
43
|
+ logger.addHandler(handler)
|
|
44
|
+
|
|
45
|
+# Utility functions
|
|
46
|
+
|
|
47
|
+## @brief Generic logging function
|
|
48
|
+# @param lvl int : Log severity
|
|
49
|
+# @param msg str : log message
|
|
50
|
+# @param *args : additional positionnal arguments
|
|
51
|
+# @param **kwargs : additional named arguments
|
|
52
|
+def log(lvl, msg, *args, **kwargs):
|
|
53
|
+ caller = logger.findCaller() # Opti warning : small overhead
|
|
54
|
+ extra = {
|
|
55
|
+ '_pathname': os.path.relpath(caller[0]), # os.path.relpath add another small overhead
|
|
56
|
+ '_lineno': caller[1],
|
|
57
|
+ '_funcName': caller[2],
|
|
58
|
+ }
|
|
59
|
+ logger.log(lvl, msg, extra = extra, *args, **kwargs)
|
|
60
|
+
|
|
61
|
+def debug(msg, *args, **kwargs): log(logging.DEBUG, msg, *args, **kwargs)
|
|
62
|
+def info(msg, *args, **kwargs): log(logging.INFO, msg, *args, **kwargs)
|
|
63
|
+def warning(msg, *args, **kwargs): log(logging.WARNING, msg, *args, **kwargs)
|
|
64
|
+def security(msg, *args, **kwargs): log(SECURITY_LOGLEVEL, msg, *args, **kwargs)
|
|
65
|
+def error(msg, *args, **kwargs): log(logging.ERROR, msg, *args, **kwargs)
|
|
66
|
+def critical(msg, *args, **kwargs): log(logging.CRITICAL, msg, *args, **kwargs)
|