Request logging with Paste's TransLogger
TransLogger is a WSGI middleware as part of "Paste". It
produces an Apache-style access log. If TransLogger is enabled
as a middleware then it logs accesses to a logger called "wsgi"
with "INFO" level. (See the "logging" module of the Python Standard
Library on how to use that.) There are two ways to enable it that
are described in the following sections. Which way you choose depends
on whether you want to hardcode logging into your application
(config/middleware.py) or whether you want the administrator of the
web server add logging (development.ini).
config/middleware.py
You can either use it in the config/middleware.py like:
1
2 | from paste.translogger import TransLogger
app = TransLogger(app)
|
(@@TODO: Add logging configuration code)
development.ini
Paste has a feature called filter-with that allows you to send requests
through another WSGI application in your development.ini. Add this line
to your [app:main] section:
This will filter all requests through a filter called "mylogging" that
you need to define, too:
1
2
3
4 | [filter:mylogging]
use = egg:Paste#translogger
setup_console_handler = False
logger_name = wsgi
|
Now the TransLogger will send all requests to the logging module.
By default it will be displayed on the console. You may rather want
to write it to an 'access.log' file. That requires some logging
configuration. Some magic helps us here. First the "logging" module
can be configured through ini-style config files. And second
Paste loads the development.ini as a logging config file. So you
can both put Paste application configuration as well as logging
configuration into your development.ini. Example code for logging:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 | [loggers]
keys = root,wsgi,routes
[handlers]
keys = console,accesslog,errorlog
[formatters]
keys = generic,accesslog
# Send warnings that are not caught by other loggers to the console
[logger_root]
level = WARNING
handlers = console
# Send all messages to the 'wsgi' logger to the accesslog and errorlog handlers
[logger_wsgi]
level = NOTSET
handlers = accesslog,errorlog
qualname = wsgi
propagate = 0
# Routes (the URL dispatcher) sends debug information to the 'routes.middleware' logger
# with DEBUG level.
[logger_routes]
level = NOTSET
handlers = console
qualname = routes.middleware
propagate = 0
# Handler for printing messages to the console
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
# Handler for writing access.log style log lines to an "access.log" file
[handler_accesslog]
class = FileHandler
args = ('access.log','a')
level = INFO
formatter = accesslog
# Handler for writing error messages to an "error.log" file
[handler_errorlog]
class = FileHandler
args = ('error.log','a')
level = WARNING
formatter = generic
# A generic formatter that prints the time, the logger's name, the level and the message
[formatter_generic]
format = %(asctime)s %(name)s[%(levelname)s] %(message)s
# A trivial formatter that just passes-through the log message.
# Translogger already creates Apache-style log messages. No need to change that.
[formatter_accesslog]
format = %(message)s
|
Graham Higgins, Christoph Haas