Steve Piercy
2017-10-22 4567204570eff25408278fd01919c1b048b9f7f1
commit | author | age
b731b5 1 .. _qtut_logging:
PE 2
b1b922 3 ============================================
PE 4 16: Collecting Application Info With Logging
5 ============================================
6
e672bd 7 Capture debugging and error output from your web applications using standard
SP 8 Python logging.
9
b1b922 10
PE 11 Background
12 ==========
13
e672bd 14 It's important to know what is going on inside our web application. In
SP 15 development we might need to collect some output. In production, we might need
16 to detect problems when other people use the site. We need *logging*.
b1b922 17
b52d1a 18 Fortunately Pyramid uses the normal Python approach to logging. The project
e672bd 19 generated in your ``development.ini`` has a number of lines that configure the
SP 20 logging for you to some reasonable defaults. You then see messages sent by
21 Pyramid, for example, when a new request comes in.
22
b1b922 23
PE 24 Objectives
25 ==========
26
e672bd 27 - Inspect the configuration setup used for logging.
b1b922 28
e672bd 29 - Add logging statements to your view code.
SP 30
b1b922 31
PE 32 Steps
33 =====
34
35 #. First we copy the results of the ``view_classes`` step:
36
37    .. code-block:: bash
38
187104 39     $ cd ..; cp -r view_classes logging; cd logging
14fd6d 40     $ $VENV/bin/pip install -e .
b1b922 41
PE 42 #. Extend ``logging/tutorial/views.py`` to log a message:
43
44    .. literalinclude:: logging/tutorial/views.py
45     :linenos:
46
e672bd 47 #. Finally let's edit ``development.ini`` configuration file to enable logging
SP 48    for our Pyramid application:
a42397 49
AB 50    .. literalinclude:: logging/development.ini
51        :language: ini
52
b1b922 53 #. Make sure the tests still pass:
PE 54
55    .. code-block:: bash
56
e672bd 57     $ $VENV/bin/py.test tutorial/tests.py -q
SP 58     ....
59     4 passed in 0.41 seconds
b1b922 60
PE 61 #. Run your Pyramid application with:
62
63    .. code-block:: bash
64
187104 65     $ $VENV/bin/pserve development.ini --reload
b1b922 66
e672bd 67 #. Open http://localhost:6543/ and http://localhost:6543/howdy in your browser.
SP 68    Note, both in the console and in the debug toolbar, the message that you
69    logged.
70
b1b922 71
PE 72 Analysis
73 ========
74
e672bd 75 In our configuration file ``development.ini``, our ``tutorial`` Python package
SP 76 is set up as a logger and configured to log messages at a ``DEBUG`` or higher
77 level. When you visit http://localhost:6543, your console will now show:
b1b922 78
e672bd 79 .. code-block:: text
SP 80
81     2013-08-09 10:42:42,968 DEBUG [tutorial.views][MainThread] In home view
b1b922 82
PE 83 Also, if you have configured your Pyramid application to use the
84 ``pyramid_debugtoolbar``, logging statements appear in one of its menus.
85
2033ee 86 .. seealso:: See also :ref:`logging_chapter`.