Browse Source

Added a logger to Lodel2

Yann Weber 9 years ago
parent
commit
eec3e107ba
4 changed files with 78 additions and 0 deletions
  1. 66
    0
      Lodel/logger.py
  2. 1
    0
      Lodel/settings_format.py
  3. 1
    0
      leapi/lecrud.py
  4. 10
    0
      settings.py

+ 66
- 0
Lodel/logger.py View File

@@ -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)

+ 1
- 0
Lodel/settings_format.py View File

@@ -15,6 +15,7 @@ MANDATORY = [
15 15
     'migration_options',
16 16
     'base_path',
17 17
     'plugins',
18
+    'logging',
18 19
 ]
19 20
 
20 21
 ## @brief List allowed (but not mandatory) configurations keys

+ 1
- 0
leapi/lecrud.py View File

@@ -9,6 +9,7 @@ import warnings
9 9
 import importlib
10 10
 import re
11 11
 
12
+from Lodel import logger
12 13
 from EditorialModel.fieldtypes.generic import DatasConstructor
13 14
 from Lodel.hooks import LodelHook
14 15
 

+ 10
- 0
settings.py View File

@@ -30,3 +30,13 @@ migration_options = {
30 30
 
31 31
 em_graph_format = 'png'
32 32
 em_graph_output = '/tmp/em_%s_graph.png'
33
+
34
+logging = [
35
+    { 'level': 'INFO' },
36
+    {
37
+        'level': 'DEBUG',
38
+        'filename': '/tmp/lodel2.log',
39
+        'maxBytes': 1024 * 50, # rotate at 50MB
40
+        'backupCount': 10, # keep at most 10 backup
41
+    }
42
+]

Loading…
Cancel
Save