@@ -56,4 +56,130 @@ This document only specifies a logbook setup. If you are interested in
56
56
the documentation for the old logging system, have a look at
57
57
:ref: `old-logging `.
58
58
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
+
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
+
59
184
.. _Logbook : http://logbook.pocoo.org/
185
+ .. _Logbook documentation : http://logbook.pocoo.org/
0 commit comments