Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 46 lines (36 sloc) 1.398 kB
77e2fbf @mitsuhiko Added a separate logging module
authored
1 # -*- coding: utf-8 -*-
2 """
3 flask.logging
4 ~~~~~~~~~~~~~
5
6 Implements the logging support for Flask.
7
ee8417d @mitsuhiko Late but 2010 -> 2011 in some files
authored
8 :copyright: (c) 2011 by Armin Ronacher.
77e2fbf @mitsuhiko Added a separate logging module
authored
9 :license: BSD, see LICENSE for more details.
10 """
11
12 from __future__ import absolute_import
13
7242abc @mitsuhiko Extend the logging from the dynamically set logger class. This fixes…
authored
14 from logging import getLogger, StreamHandler, Formatter, getLoggerClass, DEBUG
77e2fbf @mitsuhiko Added a separate logging module
authored
15
16
17 def create_logger(app):
18 """Creates a logger for the given application. This logger works
19 similar to a regular Python logger but changes the effective logging
20 level based on the application's debug flag. Furthermore this
21 function also removes all attached handlers in case there was a
22 logger with the log name before.
23 """
7242abc @mitsuhiko Extend the logging from the dynamically set logger class. This fixes…
authored
24 Logger = getLoggerClass()
77e2fbf @mitsuhiko Added a separate logging module
authored
25
26 class DebugLogger(Logger):
27 def getEffectiveLevel(x):
61a9519 @mitsuhiko Changed logic for debug level log settings
authored
28 if x.level == 0 and app.debug:
29 return DEBUG
30 return Logger.getEffectiveLevel(x)
77e2fbf @mitsuhiko Added a separate logging module
authored
31
32 class DebugHandler(StreamHandler):
33 def emit(x, record):
34 StreamHandler.emit(x, record) if app.debug else None
35
36 handler = DebugHandler()
37 handler.setLevel(DEBUG)
38 handler.setFormatter(Formatter(app.debug_log_format))
39 logger = getLogger(app.logger_name)
40 # just in case that was not a new logger, get rid of all the handlers
41 # already attached to it.
42 del logger.handlers[:]
43 logger.__class__ = DebugLogger
44 logger.addHandler(handler)
45 return logger
Something went wrong with that request. Please try again.