3
3
flask.logging
4
4
~~~~~~~~~~~~~
5
5
6
- Implements the logging support for Flask.
6
+ Implements the logging support for Flask. This is not supposed to be a
7
+ abstraction layer above multiple logging systems, it mainly exists because
8
+ Flask started out using logging and is currently in the process to switch
9
+ to Logbook. This module will become mostly useless once we drop support
10
+ for the stdlib's logging.
11
+
12
+ In some other parts of Flask there are explicit hardcoded checks that
13
+ opt-in features in case Logbook is present.
7
14
8
15
:copyright: (c) 2010 by Armin Ronacher.
9
16
:license: BSD, see LICENSE for more details.
13
20
14
21
15
22
def create_logger (app ):
23
+ """Creates a new logger for the application. This is mainly needed
24
+ because Flask supports dynamic logger name changes. Once we drop
25
+ support for logging we can remove this as well because it is easily
26
+ possible to reflect the channel name from another value in logbook
27
+ """
28
+ return logging_systems [app .logging_system ][1 ](app )
29
+
30
+
31
+ def init_logging_system (app ):
16
32
"""Initializes the logging system for this app."""
17
- return logging_systems [app .logging_system ](app )
33
+ return logging_systems [app .logging_system ][ 0 ] (app )
18
34
19
35
20
- def init_logbook (app ):
36
+ def create_logbook_logger (app ):
21
37
"""Initializes the logbook default config for the application."""
22
- try :
23
- from logbook import Logger
24
- except ImportError :
25
- raise RuntimeError ('Logbook is not installed but required for '
26
- 'the logbook logging backend.' )
38
+ from logbook import Logger
27
39
return Logger (app .logger_name )
28
40
29
41
30
- def init_logging (app ):
42
+ def init_logbook (app ):
43
+ """Stuffs a default logging setup on the application object in case
44
+ the attribute was not set so far.
45
+ """
46
+ if app .logbook_setup is None :
47
+ from logbook import StderrHandler
48
+ app .logbook_setup = StderrHandler (format_string = (
49
+ '-' * 80 + '\n ' +
50
+ '{record.level_name} in {record.module} '
51
+ '[{record.filename}:{record.lineno}]:\n ' +
52
+ '{record.message}\n ' +
53
+ '-' * 80
54
+ ))
55
+
56
+
57
+ def create_logging_logger (app ):
31
58
"""Creates a logger for the given application. This logger works
32
59
similar to a regular Python logger but changes the effective logging
33
60
level based on the application's debug flag. Furthermore this
@@ -46,7 +73,12 @@ def emit(x, record):
46
73
47
74
handler = DebugHandler ()
48
75
handler .setLevel (DEBUG )
49
- handler .setFormatter (Formatter (app .debug_log_format ))
76
+ handler .setFormatter (Formatter (
77
+ '-' * 80 + '\n ' +
78
+ '%(levelname)s in %(module)s [%(pathname)s:%(lineno)d]:\n ' +
79
+ '%(message)s\n ' +
80
+ '-' * 80
81
+ ))
50
82
logger = getLogger (app .logger_name )
51
83
# just in case that was not a new logger, get rid of all the handlers
52
84
# already attached to it.
@@ -56,7 +88,23 @@ def emit(x, record):
56
88
return logger
57
89
58
90
91
+ def create_dummy_logger (app ):
92
+ """Creates a dummy logger."""
93
+ return _DummyLogger (app .logger_name )
94
+
95
+
96
+ class _DummyLogger (object ):
97
+ """Not a very helpful logger."""
98
+ def __init__ (self , name , level = 0 ):
99
+ self .name = name
100
+ self .level = level
101
+ debug = info = warn = warning = notice = error = exception = \
102
+ critical = log = lambda * a , ** kw : None
103
+
104
+
105
+ _dummy = lambda x : None
59
106
logging_systems = {
60
- 'logbook' : init_logbook ,
61
- 'logging' : init_logging
107
+ 'logbook' : (init_logbook , create_logbook_logger ),
108
+ 'logging' : (_dummy , create_logging_logger ),
109
+ 'none' : (_dummy , create_dummy_logger )
62
110
}
0 commit comments