<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flapping Head &#187; Python</title>
	<atom:link href="http://scottbarnham.com/blog/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://scottbarnham.com/blog</link>
	<description>Code and comments on web development, Django, Python and things (un)related.</description>
	<lastBuildDate>Thu, 08 Jul 2010 09:49:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SysLogHandler not writing to syslog with Python logging</title>
		<link>http://scottbarnham.com/blog/2008/01/01/sysloghandler-not-writing-to-syslog-with-python-logging/</link>
		<comments>http://scottbarnham.com/blog/2008/01/01/sysloghandler-not-writing-to-syslog-with-python-logging/#comments</comments>
		<pubDate>Tue, 01 Jan 2008 22:21:18 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/01/01/sysloghandler-not-writing-to-syslog-with-python-logging/</guid>
		<description><![CDATA[Logging to syslog in Python
I was trying to use the standard Python logging module to write messages to syslog.  The logging module has a SysLogHandler class which can log to a local or remote syslog daemon.
With no host specified, SysLogHandler uses localhost which is what I wanted.  I tried to use SysLogHandler, but [...]]]></description>
			<content:encoded><![CDATA[<h2>Logging to syslog in Python</h2>
<p>I was trying to use the standard Python <code>logging</code> module to write messages to syslog.  The <code>logging</code> module has a <code>SysLogHandler</code> class which can log to a local or remote syslog daemon.</p>
<p>With no host specified, <code>SysLogHandler</code> uses <code>localhost</code> which is what I wanted.  I tried to use <code>SysLogHandler,</code> but it just wouldn&#8217;t work.  There was no error when I called the logging methods, but my messages didn&#8217;t show up in <code>/var/log/syslog</code>.</p>
<h2>syslog module works</h2>
<p>Python also has a standard <code>syslog</code> module.  I tried it and it worked fine; my messages were written to the syslog file.</p>
<p>For example:</p>
<pre>import syslog
syslog.syslog('test')</pre>
<h2>syslogd isn&#8217;t listening</h2>
<p>After running Wireshark I found the <code>SysLogHandler</code> was correctly sending a UDP packet to <code>localhost</code> on port 514.  I could also see there was an ICMP response indicating the UDP packet was not received on that port.  syslog wasn&#8217;t listening!</p>
<h2>Use /dev/log</h2>
<p>Instead of sending to <code>localhost</code>, I wanted <code>SysLogHandler</code> to pass the message to syslog on the local machine in the same way the <code>syslog</code> Python module was doing.</p>
<p>The solution is to pass <code>/dev/log</code> as the <code>address</code> parameter to <code>SysLogHandler</code>.  It&#8217;s not <a href="http://docs.python.org/lib/node417.html">well documented</a>, but it works.</p>
<p>For example:</p>
<pre>import logging
from logging.handlers import SysLogHandler

logger = logging.getLogger()
logger.setLevel(logging.INFO)
syslog = SysLogHandler(address='/dev/log')
formatter = logging.Formatter('%(name)s: %(levelname)s %(message)s')
syslog.setFormatter(formatter)
logger.addHandler(syslog)</pre>
<p>Easy when you know how.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottbarnham.com/blog/2008/01/01/sysloghandler-not-writing-to-syslog-with-python-logging/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.194 seconds -->
