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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
.. _qtut_logging:
============================================
16: Collecting Application Info With Logging
============================================
Capture debugging and error output from your web applications using
standard Python logging.
Background
==========
It's important to know what is going on inside our web application.
In development we might need to collect some output. In production,
we might need to detect problems when other people use the site. We
need *logging*.
Fortunately Pyramid uses the normal Python approach to logging. The
scaffold generated in your ``development.ini`` has a number of lines that
configure the logging for you to some reasonable defaults. You then see
messages sent by Pyramid, for example, when a new request comes in.
Objectives
==========
- Inspect the configuration setup used for logging
- Add logging statements to your view code
Steps
=====
#. First we copy the results of the ``view_classes`` step:
.. code-block:: bash
$ cd ..; cp -r view_classes logging; cd logging
$ $VENV/bin/python setup.py develop
#. Extend ``logging/tutorial/views.py`` to log a message:
.. literalinclude:: logging/tutorial/views.py
:linenos:
#. Finally let's edit ``development.ini`` configuration file
to enable logging for our Pyramid application:
.. literalinclude:: logging/development.ini
:language: ini
#. Make sure the tests still pass:
.. code-block:: bash
$ $VENV/bin/nosetests tutorial
#. Run your Pyramid application with:
.. code-block:: bash
$ $VENV/bin/pserve development.ini --reload
#. Open http://localhost:6543/ and http://localhost:6543/howdy
in your browser. Note, both in the console and in the debug
toolbar, the message that you logged.
Analysis
========
In our configuration file ``development.ini``, our ``tutorial`` Python
package is setup as a logger and configured to log messages at a
``DEBUG`` or higher level. When you visit http://localhost:6543 your
console will now show::
2013-08-09 10:42:42,968 DEBUG [tutorial.views][MainThread] In home view
Also, if you have configured your Pyramid application to use the
``pyramid_debugtoolbar``, logging statements appear in one of its menus.
.. seealso:: See also :ref:`logging_chapter`.
|