<?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>Xster.net &#187; output</title>
	<atom:link href="http://tech.xster.net/tag/output/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.xster.net</link>
	<description>Never relearn twice</description>
	<lastBuildDate>Fri, 20 Jan 2012 21:11:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python Log Stdout to File</title>
		<link>http://tech.xster.net/tips/python-log-stdout-to-file/</link>
		<comments>http://tech.xster.net/tips/python-log-stdout-to-file/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 18:25:25 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[stdout]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=156</guid>
		<description><![CDATA[Python has the ability to alter its sys.stdout as to redirect its print commands to pretty much anything. If, for instance, you want to print to both standard output and to a log file, you can create a class to handle the stdout like such: class MyOutput&#40;&#41;: def __init__&#40;self, logfile&#41;: self.stdout = sys.stdout self.log = [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Python has the ability to alter its sys.stdout as to redirect its print commands to pretty much anything.</p>
<p>If, for instance, you want to print to both standard output and to a log file, you can create a class to handle the stdout like such:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> MyOutput<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, logfile<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">stdout</span> = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>logfile, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> write<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, text<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">stdout</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span>.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> close<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">stdout</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span> = MyOutput<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log.txt&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;blah blah blah&quot;</span></pre></div></div>

<p><span id="more-156"></span><br />
This class would implement stdout&#8217;s write and close functions. We then assign the stdout to an instance of this class. Inside the write/close implementations, we also add functionalities to write the texts in a log file as well. We flush the file buffer on each write because otherwise, we would only be able to see the file content when sys.stdout (therefore MyOutput) closes.</p>
<p>With this, outputs of print will both show in the screen and in the file log.txt</p>
<div class="shr-publisher-156"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-log-stdout-to-file%2F' data-shr_title='Python+Log+Stdout+to+File'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpython-log-stdout-to-file%2F' data-shr_title='Python+Log+Stdout+to+File'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/python-log-stdout-to-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

