| 1 |
.. _logging: |
| 2 |
|
| 3 |
Logging |
| 4 |
======= |
| 5 |
|
| 6 |
Pecan uses the Python standard library's :py:mod:`logging` module by passing |
| 7 |
logging configuration options into the `logging.config.dictConfig`_ |
| 8 |
function. The full documentation for the :func:`dictConfig` format is |
| 9 |
the best source of information for logging configuration, but to get |
| 10 |
you started, this chapter will provide you with a few simple examples. |
| 11 |
|
| 12 |
.. _logging.config.dictConfig: http://docs.python.org/library/logging.config.html#configuration-dictionary-schema |
| 13 |
|
| 14 |
Configuring Logging |
| 15 |
------------------- |
| 16 |
|
| 17 |
Sample logging configuration is provided with the quickstart project |
| 18 |
introduced in :ref:`quick_start`: |
| 19 |
|
| 20 |
:: |
| 21 |
|
| 22 |
$ pecan create myapp |
| 23 |
|
| 24 |
The default configuration defines one handler and two loggers. |
| 25 |
|
| 26 |
:: |
| 27 |
|
| 28 |
# myapp/config.py |
| 29 |
|
| 30 |
app = { ... } |
| 31 |
server = { ... } |
| 32 |
|
| 33 |
logging = { |
| 34 |
'root' : {'level': 'INFO', 'handlers': ['console']}, |
| 35 |
'loggers': { |
| 36 |
'myapp': {'level': 'DEBUG', 'handlers': ['console']} |
| 37 |
}, |
| 38 |
'handlers': { |
| 39 |
'console': { |
| 40 |
'level': 'DEBUG', |
| 41 |
'class': 'logging.StreamHandler', |
| 42 |
'formatter': 'simple' |
| 43 |
} |
| 44 |
}, |
| 45 |
'formatters': { |
| 46 |
'simple': { |
| 47 |
'format': ('%(asctime)s %(levelname)-5.5s [%(name)s]' |
| 48 |
'[%(threadName)s] %(message)s') |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
* ``console`` logs messages to ``stderr`` using the ``simple`` formatter. |
| 54 |
|
| 55 |
* ``myapp`` logs messages sent at a level above or equal to ``DEBUG`` to |
| 56 |
the ``console`` handler |
| 57 |
|
| 58 |
* ``root`` logs messages at a level above or equal to the ``INFO`` level to |
| 59 |
the ``console`` handler |
| 60 |
|
| 61 |
|
| 62 |
Writing Log Messages in Your Application |
| 63 |
---------------------------------------- |
| 64 |
|
| 65 |
The logger named ``myapp`` is reserved for your usage in your Pecan |
| 66 |
application. |
| 67 |
|
| 68 |
Once you have configured your logging, you can place logging calls in your |
| 69 |
code. Using the logging framework is very simple. |
| 70 |
|
| 71 |
:: |
| 72 |
|
| 73 |
# myapp/myapp/controllers/root.py |
| 74 |
from pecan import expose |
| 75 |
import logging |
| 76 |
|
| 77 |
logger = logging.getLogger(__name__) |
| 78 |
|
| 79 |
class RootController(object): |
| 80 |
|
| 81 |
@expose() |
| 82 |
def index(self): |
| 83 |
if bad_stuff(): |
| 84 |
logger.error('Uh-oh!') |
| 85 |
return dict() |
| 86 |
|
| 87 |
Logging to Files and Other Locations |
| 88 |
------------------------------------ |
| 89 |
|
| 90 |
Python's :py:mod:`logging` library defines a variety of handlers that assist in |
| 91 |
writing logs to file. A few interesting ones are: |
| 92 |
|
| 93 |
* :class:`~logging.FileHandler` - used to log messages to a file on the filesystem |
| 94 |
* :class:`~logging.handlers.RotatingFileHandler` - similar to |
| 95 |
:class:`~logging.FileHandler`, but also rotates logs |
| 96 |
periodically |
| 97 |
* :class:`~logging.handlers.SysLogHandler` - used to log messages to a UNIX syslog |
| 98 |
* :class:`~logging.handlers.SMTPHandler` - used to log messages to an email |
| 99 |
address via SMTP |
| 100 |
|
| 101 |
Using any of them is as simple as defining a new handler in your |
| 102 |
application's ``logging`` block and assigning it to one of more loggers. |
| 103 |
|
| 104 |
Logging Requests with Paste Translogger |
| 105 |
--------------------------------------- |
| 106 |
|
| 107 |
`Paste <http://pythonpaste.org/>`_ (which is not included with Pecan) includes |
| 108 |
the :class:`~paste.translogger.TransLogger` middleware |
| 109 |
for logging requests in `Apache Combined Log Format |
| 110 |
<http://httpd.apache.org/docs/2.2/logs.html#combined>`_. Combined with |
| 111 |
file-based logging, TransLogger can be used to create an ``access.log`` file |
| 112 |
similar to ``Apache``. |
| 113 |
|
| 114 |
To add this middleware, modify your the ``setup_app`` method in your |
| 115 |
project's ``app.py`` as follows:: |
| 116 |
|
| 117 |
# myapp/myapp/app.py |
| 118 |
from pecan import make_app |
| 119 |
from paste.translogger import TransLogger |
| 120 |
|
| 121 |
def setup_app(config): |
| 122 |
# ... |
| 123 |
app = make_app( |
| 124 |
config.app.root |
| 125 |
# ... |
| 126 |
) |
| 127 |
app = TransLogger(app, setup_console_handler=False) |
| 128 |
return app |
| 129 |
|
| 130 |
By default, :class:`~paste.translogger.TransLogger` creates a logger named |
| 131 |
``wsgi``, so you'll need to specify a new (file-based) handler for this logger |
| 132 |
in our Pecan configuration file:: |
| 133 |
|
| 134 |
# myapp/config.py |
| 135 |
|
| 136 |
app = { ... } |
| 137 |
server = { ... } |
| 138 |
|
| 139 |
logging = { |
| 140 |
'loggers': { |
| 141 |
# ... |
| 142 |
'wsgi': {'level': 'INFO', 'handlers': ['logfile'], 'qualname': 'wsgi'} |
| 143 |
}, |
| 144 |
'handlers': { |
| 145 |
# ... |
| 146 |
'logfile': { |
| 147 |
'class': 'logging.FileHandler', |
| 148 |
'filename': '/etc/access.log', |
| 149 |
'level': 'INFO', |
| 150 |
'formatter': 'messageonly' |
| 151 |
} |
| 152 |
}, |
| 153 |
'formatters': { |
| 154 |
# ... |
| 155 |
'messageonly': {'format': '%(message)s'} |
| 156 |
} |
| 157 |
} |