How can i override print function in Python 2.2.1 (WebLogic version)
<p>How to override print function in Python 2.2 in order to being able to redirect output to custom logger.</p>

<p>I don't have a version of 2.2 to check (why are you using such an old version?), but I suspect the following is valid for all 2.x.</p>

<hr>

<p>The <code>print</code> statement recognizes a first argument beginning with <code>&gt;&gt;</code> to indicate which file to write to.</p>

<p>The following are identical:</p>

<pre><code>print "foo", "bar"
print &gt;&gt;sys.stdout, "foo", "bar"
</code></pre>

<p>As such, you can specify any <code>file</code> object as the target file.</p>

<pre><code>f = open("log.txt", "w")
print &gt;&gt;f, "foo", "bar"
</code></pre>

<p>If you want to redirect <em>every</em> <code>print</code> statement (or at least all the ones that aren't using a specific file as shown above), you can simply replace <code>sys.stdout</code> with your desired file.</p>

<pre><code>sys.stdout = open("log.txt", "w")
print "foo", "bar"   # Goes to log.txt
</code></pre>

<p>If you need it, the original standard output is still available via <code>sys.__stdout__</code>.</p>

python wlst