Skip to content

Commit 79562c6

Browse files
committed
Some documentation updates
1 parent 24c8e8a commit 79562c6

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

docs/logging.rst

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,130 @@ This document only specifies a logbook setup. If you are interested in
5656
the documentation for the old logging system, have a look at
5757
:ref:`old-logging`.
5858

59+
Making the Switch
60+
-----------------
61+
62+
The default configuration of Logbook is fine and Flask even adds a small
63+
little improvement on top of that. However you currently have to opt-in
64+
for Logbook support::
65+
66+
from flask import Flask
67+
app = Flask(__name__, logging_system='logbook')
68+
69+
The other options for the logging system are:
70+
71+
``'logging'``
72+
Deprecated support for logging to the logging system.
73+
74+
``'logbook'``
75+
Logging to `Logbook`_, requires that the library is installed.
76+
77+
``'none'``
78+
Configure a dummy logger for internal usage but do not set up a
79+
real logging system.
80+
81+
In Flask 1.0 the logging system parameter will become a no-op and Logbook
82+
will always be used. In case Logbook is not installed in 1.0 and later,
83+
it will dynamically fall back to the dummy logger which currently is not
84+
the case.
85+
86+
Logging Messages
87+
----------------
88+
89+
If you have used logging before in Flask applications you will have used
90+
the :attr:`~flask.Flask.logger` attribute. While that is nice and you can
91+
continue to use this logger, it is recommended to use a logger for logging
92+
directly. The reason for this is that you have better control over what
93+
you are doing and can shut down a logger completely to reduce runtime
94+
overhead.
95+
96+
To make the switch, replace calls to this::
97+
98+
@app.route('/')
99+
def index():
100+
app.logger.warn('A message')
101+
...
102+
103+
With something like this::
104+
105+
from logbook import Logger
106+
logger = Logger('My component')
107+
108+
@app.route('/')
109+
def index():
110+
logger.warn('A message')
111+
...
112+
113+
Note that Logbook is using Python 2.6's new string formatting feature
114+
instead of the printf style string formatting that was in use by logging::
115+
116+
app.logger.warn('Hello %s!', name)
117+
118+
Becomes this now::
119+
120+
app.logger.warn('Hello {0}!', name)
121+
122+
You can also use named parameters::
123+
124+
app.logger.warn('Hello {name}!', name=name)
125+
126+
Logbook ships with a pure Python implementation of the new string
127+
formatting for Python 2.5 which is why it is recommended to use Python 2.6
128+
in case you are doing a lot of logging calls that are delivered to
129+
handlers in production. It will of course not affect logging calls that
130+
don't happen because their level is too low (like debug log calls on a
131+
production system that only cares about warnings and higher priority
132+
records).
133+
134+
Consult the `Logbook documentation`_ for more information.
135+
136+
Error Mails
137+
-----------
138+
139+
If the application runs in production mode (which it will do on your
140+
server) you won't see any log messages by default. Why is that? Flask
141+
tries to be a zero-configuration framework. Where should it drop the logs
142+
for you if there is no configuration? Guessing is not a good idea because
143+
chances are, the place it guessed is not the place where the user has
144+
permission to create a logfile. Also, for most small applications nobody
145+
will look at the logs anyways.
146+
147+
In fact, I promise you right now that if you configure a logfile for the
148+
application errors you will never look at it except for debugging an issue
149+
when a user reported it for you. What you want instead is a mail the
150+
second the exception happened. Then you get an alert and you can do
151+
something about it.
152+
153+
Flask uses the Python builtin logging system, and it can actually send
154+
you mails for errors which is probably what you want. Here is how you can
155+
configure the Flask logger to send you mails for exceptions::
156+
157+
ADMINS = ['[email protected]']
158+
if not app.debug:
159+
import logging
160+
from logging.handlers import SMTPHandler
161+
mail_handler = SMTPHandler('127.0.0.1',
162+
163+
ADMINS, 'YourApplication Failed')
164+
mail_handler.setLevel(logging.ERROR)
165+
app.logger.addHandler(mail_handler)
166+
167+
So what just happened? We created a new
168+
:class:`~logging.handlers.SMTPHandler` that will send mails with the mail
169+
server listening on ``127.0.0.1`` to all the `ADMINS` from the address
170+
*[email protected]* with the subject "YourApplication Failed". If
171+
your mail server requires credentials, these can also be provided. For
172+
that check out the documentation for the
173+
:class:`~logging.handlers.SMTPHandler`.
174+
175+
We also tell the handler to only send errors and more critical messages.
176+
Because we certainly don't want to get a mail for warnings or other
177+
useless logs that might happen during request handling.
178+
179+
Before you run that in production, please also look at :ref:`logformat` to
180+
put more information into that error mail. That will save you from a lot
181+
of frustration.
182+
183+
59184
.. _Logbook: http://logbook.pocoo.org/
185+
.. _Logbook documentation: http://logbook.pocoo.org/

flask/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class Flask(_PackageBoundObject):
173173
logger_name = ConfigAttribute('LOGGER_NAME')
174174

175175
#: the logbook setup that should be in use. This is only used in case
176-
#: the `LOGGING_SYSTEM` is ``'logbook'``. It can point to any
176+
#: the logging system is ``'logbook'``. It can point to any
177177
#: :class:`logbook.Processor`, :class:`logbook.Handler` or
178178
#: :class:`logbook.NestedSetup`. In case it's `None`, the default
179179
#: application wide setup is used. For further customization you can
@@ -321,7 +321,7 @@ def logger(self):
321321
app.logger.warning('A warning ocurred (%d apples)', 42)
322322
app.logger.error('An error occoured')
323323
324-
In case the `LOGGING_SYSTEM` is ``'logbook'`` it is strongly
324+
In case the logging system is ``'logbook'`` it is strongly
325325
recommended against using this logger and create your own. Unlike
326326
logging, logbook automatically will handle writing to the
327327
:attr:`logbook_setup`.

0 commit comments

Comments
 (0)