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">
3
<title>Twisted Documentation: Logging with twisted.python.log</title>
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
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>
13
<h2>Basic usage<a name="auto0"/></h2>
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
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
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>)
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.
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
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>
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>))
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>)
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>.
64
<p>Before <code>startLogging</code> is called, log messages will be
65
discarded and errors will be written to stderr.</p>
67
<h3>Logging and twistd<a name="auto1"/></h3>
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
75
<h3>Log files<a name="auto2"/></h3>
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>
82
<h3>Using the Python logging module<a name="auto3"/></h3>
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,
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>
90
<p>You just start it like any other observers:
91
<pre class="python"><p class="py-linenumber">1
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>()
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>.
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
105
</p><span class="py-src-variable">log</span>.<span class="py-src-variable">msg</span>(<span class="py-src-string">"This is important!"</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">"Don't mind"</span>, <span class="py-src-variable">logLevel</span>=<span class="py-src-variable">logging</span>.<span class="py-src-variable">DEBUG</span>)
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>.
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>
117
<h2>Writing log observers<a name="auto4"/></h2>
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
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>)
130
<p>The dictionary will have at least two items:</p>
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>
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>
144
<p>Other items the built in logging functionality may add include:</p>
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>
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>
158
<p>Important notes:</p>
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>
164
<li>The observer needs to be thread safe if you anticipate using threads
165
in your program.</li>
168
<h2>Customizing <code>twistd</code> logging<a name="auto5"/></h2>
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
178
<p><a href="index.html">Index</a></p>
179
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'