~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/logging.html

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
 
2
  <head>
 
3
<title>Twisted Documentation: Logging with twisted.python.log</title>
 
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
 
5
  </head>
 
6
 
 
7
  <body bgcolor="white">
 
8
    <h1 class="title">Logging with twisted.python.log</h1>
 
9
    <div class="toc"><ol><li><a href="#auto0">Basic usage</a></li><ul><li><a href="#auto1">Logging and twistd</a></li><li><a href="#auto2">Log files</a></li><li><a href="#auto3">Using the Python logging module</a></li></ul><li><a href="#auto4">Writing log observers</a></li><li><a href="#auto5">Customizing twistd logging</a></li></ol></div>
 
10
    <div class="content">
 
11
    <span/>
 
12
 
 
13
    <h2>Basic usage<a name="auto0"/></h2>
 
14
 
 
15
    <p>Twisted provides a simple and flexible logging system in the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.log.html" title="twisted.python.log">twisted.python.log</a></code> module.  It has three commonly used
 
16
    functions:</p>
 
17
 
 
18
    <dl>
 
19
      <dt><code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.log.msg.html" title="twisted.python.log.msg">msg</a></code></dt>
 
20
      <dd>Logs a new message.  For example:
 
21
        <pre class="python"><p class="py-linenumber">1
 
22
2
 
23
</p><span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">log</span>
 
24
<span class="py-src-variable">log</span>.<span class="py-src-variable">msg</span>(<span class="py-src-string">'Hello, world.'</span>)
 
25
</pre>
 
26
      </dd>
 
27
 
 
28
      <dt><code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.log.err.html" title="twisted.python.log.err">err</a></code></dt>
 
29
      <dd>Writes a failure to the log, including traceback information (if any).
 
30
      You can pass it a <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.failure.Failure.html" title="twisted.python.failure.Failure">Failure</a></code> or Exception instance, or
 
31
      nothing.  If you pass something else, it will be converted to a string
 
32
      with <code>repr</code> and logged.
 
33
 
 
34
      If you pass nothing, it will construct a Failure from the
 
35
      currently active exception, which makes it convenient to use in an <code class="python">except</code> clause:
 
36
        <pre class="python"><p class="py-linenumber">1
 
37
2
 
38
3
 
39
4
 
40
</p><span class="py-src-keyword">try</span>:
 
41
    <span class="py-src-variable">x</span> = <span class="py-src-number">1</span> / <span class="py-src-number">0</span>
 
42
<span class="py-src-keyword">except</span>:
 
43
    <span class="py-src-variable">log</span>.<span class="py-src-variable">err</span>()   <span class="py-src-comment"># will log the ZeroDivisionError</span>
 
44
</pre>
 
45
      </dd>
 
46
 
 
47
      <dt><code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.log.startLogging.html" title="twisted.python.log.startLogging">startLogging</a></code></dt>
 
48
      <dd>Starts logging to a given file-like object.  For example:
 
49
        <pre class="python"><p class="py-linenumber">1
 
50
</p><span class="py-src-variable">log</span>.<span class="py-src-variable">startLogging</span>(<span class="py-src-variable">open</span>(<span class="py-src-string">'/var/log/foo.log'</span>, <span class="py-src-string">'w'</span>))
 
51
</pre>
 
52
      or:
 
53
        <pre class="python"><p class="py-linenumber">1
 
54
</p><span class="py-src-variable">log</span>.<span class="py-src-variable">startLogging</span>(<span class="py-src-variable">sys</span>.<span class="py-src-variable">stdout</span>)
 
55
</pre>
 
56
 
 
57
      By default, <code>startLogging</code> will also redirect anything written
 
58
      to <code>sys.stdout</code> and <code>sys.stderr</code> to the log.  You
 
59
      can disable this by passing <code class="python">setStdout=False</code> to
 
60
      <code>startLogging</code>.
 
61
      </dd>
 
62
    </dl>
 
63
 
 
64
    <p>Before <code>startLogging</code> is called, log messages will be
 
65
    discarded and errors will be written to stderr.</p>
 
66
 
 
67
    <h3>Logging and twistd<a name="auto1"/></h3>
 
68
 
 
69
    <p>If you are using <code class="shell">twistd</code> to run your daemon, it
 
70
    will take care of calling <code>startLogging</code> for you, and will also
 
71
    rotate log files.  See <a href="application.html#twistd" shape="rect">twistd and tac</a>
 
72
    and the <code class="shell">twistd</code> man page for details of using
 
73
    twistd.</p>
 
74
 
 
75
    <h3>Log files<a name="auto2"/></h3>
 
76
 
 
77
    <p>The <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.logfile.html" title="twisted.python.logfile">twisted.python.logfile</a></code> module provides
 
78
    some standard classes suitable for use with <code>startLogging</code>, such
 
79
    as <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.logfile.DailyLogFile.html" title="twisted.python.logfile.DailyLogFile">DailyLogFile</a></code>,
 
80
    which will rotate the log to a new file once per day.</p>
 
81
 
 
82
    <h3>Using the Python logging module<a name="auto3"/></h3>
 
83
 
 
84
    <p>If your application uses the logging module or you want to use its ease
 
85
    of configuration but don't want to lose twisted-produced messages,
 
86
    the observer
 
87
    <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.log.PythonLoggingObserver.html" title="twisted.python.log.PythonLoggingObserver">PythonLoggingObserver</a></code>
 
88
    should be useful to you</p>
 
89
 
 
90
    <p>You just start it like any other observers:
 
91
        <pre class="python"><p class="py-linenumber">1
 
92
2
 
93
</p><span class="py-src-variable">observer</span> = <span class="py-src-variable">log</span>.<span class="py-src-variable">PythonLoggingObserver</span>()
 
94
<span class="py-src-variable">observer</span>.<span class="py-src-variable">start</span>()
 
95
</pre>
 
96
    And then you'll just have to configure logging to do what you want:
 
97
    <a href="http://docs.python.org/lib/module-logging.html" shape="rect">
 
98
    logging documentation</a>.
 
99
    </p>
 
100
 
 
101
    <p>This method allows you to customize the log level received by the
 
102
    logging module using the <code>logLevel</code> keyword:
 
103
        <pre class="python"><p class="py-linenumber">1
 
104
2
 
105
</p><span class="py-src-variable">log</span>.<span class="py-src-variable">msg</span>(<span class="py-src-string">&quot;This is important!&quot;</span>, <span class="py-src-variable">logLevel</span>=<span class="py-src-variable">logging</span>.<span class="py-src-variable">CRITICAL</span>)
 
106
<span class="py-src-variable">log</span>.<span class="py-src-variable">msg</span>(<span class="py-src-string">&quot;Don't mind&quot;</span>, <span class="py-src-variable">logLevel</span>=<span class="py-src-variable">logging</span>.<span class="py-src-variable">DEBUG</span>)
 
107
</pre>
 
108
    Unless logLevel is provided, logging.INFO is used for <code>log.msg</code>
 
109
    and logging.ERROR is used for <code>log.err</code>.
 
110
    </p>
 
111
 
 
112
    <p>One special care should be made when you use special configuration of
 
113
    the python logging module: some handlers (e.g. SMTP, HTTP) uses network
 
114
    so can block inside the reactor loop. <em>Nothing</em> in the bridge is
 
115
    done to prevent that.</p>
 
116
 
 
117
    <h2>Writing log observers<a name="auto4"/></h2>
 
118
 
 
119
    <p>Log observers are the basis of the Twisted logging system.  An example of
 
120
    a log observer in Twisted is the <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.python.log.FileLogObserver.html" title="twisted.python.log.FileLogObserver">FileLogObserver</a></code> used by
 
121
    <code>startLogging</code> that writes events to a log file.  A log observer
 
122
    is just a callable that accepts a dictionary as its only argument.  You can
 
123
    then register it to receive all log events (in addition to any other
 
124
    observers):</p>
 
125
 
 
126
    <pre class="python"><p class="py-linenumber">1
 
127
</p><span class="py-src-variable">twisted</span>.<span class="py-src-variable">python</span>.<span class="py-src-variable">log</span>.<span class="py-src-variable">addObserver</span>(<span class="py-src-variable">yourCallable</span>)
 
128
</pre>
 
129
 
 
130
    <p>The dictionary will have at least two items:</p>
 
131
 
 
132
    <dl>
 
133
      <dt>message</dt>
 
134
      <dd>The message (a list, usually of strings)
 
135
      for this log event, as passed to <code>log.msg</code> or the
 
136
      message in the failure passed to <code>log.err</code>.</dd>
 
137
 
 
138
      <dt>isError</dt>
 
139
      <dd>This is a boolean that will be true if this event came from a call to
 
140
      <code>log.err</code>.  If this is set, there may be a <code>failure</code>
 
141
      item in the dictionary as will, with a Failure object in it.</dd>
 
142
    </dl>
 
143
 
 
144
    <p>Other items the built in logging functionality may add include:</p>
 
145
 
 
146
    <dl>
 
147
      <dt>printed</dt>
 
148
      <dd>This message was captured from <code>sys.stdout</code>, i.e. this
 
149
      message came from a <code>print</code> statement.  If
 
150
      <code>isError</code> is also true, it came from
 
151
      <code>sys.stderr</code>.</dd>
 
152
    </dl>
 
153
 
 
154
    <p>You can pass additional items to the event dictionary by passing keyword
 
155
    arguments to <code>log.msg</code> and <code>log.err</code>.  The standard
 
156
    log observers will ignore dictionary items they don't use.</p>
 
157
 
 
158
    <p>Important notes:</p>
 
159
 
 
160
    <ul>
 
161
      <li>Never block in a log observer, as it may run in main Twisted thread.
 
162
      This means you can't use socket or syslog Python-logging backends.</li>
 
163
 
 
164
      <li>The observer needs to be thread safe if you anticipate using threads
 
165
      in your program.</li>
 
166
    </ul>
 
167
 
 
168
    <h2>Customizing <code>twistd</code> logging<a name="auto5"/></h2>
 
169
    <p>
 
170
    The behavior of the logging that <code>twistd</code> does can be customized
 
171
    by setting the <code>ILogObserver</code> component on the application
 
172
    object.  See the <a href="application.html" shape="rect">Application document</a> for
 
173
    more information.
 
174
    </p>
 
175
 
 
176
  </div>
 
177
 
 
178
    <p><a href="index.html">Index</a></p>
 
179
    <span class="version">Version: 10.0.0</span>
 
180
  </body>
 
181
</html>
 
 
b'\\ No newline at end of file'