title: Redirect stdout and stderr to file in Django with wsgi
body: <p>I'm trying to make django work with wsgi in a shared hosting, so I cannot access server logs,</p>
  
  <p>I'm trying to redirect the output to a file in my django.wsgi script, like this:</p>
  
  <pre><code>saveout = sys.stdout
  log_out = open('out.log', 'w')  
  sys.stdout = log_out 
  </code></pre>
  
  <p>but this is the error that I have (in the only error log that I can access)</p>
  
  <pre><code>[Thu Oct 07 19:18:08 2010] [error] [client &lt;ip&gt;]   File "/home/myuser/public_html/django.wsgi", line 9, in &lt;module&gt;
  [Thu Oct 07 19:18:08 2010] [error] [client &lt;ip&gt;] mod_wsgi (pid=30677): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
  </code></pre>
  
  <p>and that is all the error I can get</p>
  
  <p>Line 9 is the one that tries to open file. I've tried creating previously the file, giving writing access to everyone, without the file previously created.. and nothing.
  In the app, I only get a 500 server error.</p>
  
  <p>Is there a proper way to get this output without access to apache logs?</p>
  
  <p>Edited:</p>
  
  <p>Using absolute paths, I can open files and write to them, but I get empty files unless I close the files in the end, is it necessary? Could I leave the files open?.. </p>
  
  <p>Regarding to the initial problem, even though I used exception handling, write a "finish ok" to file in the end of my script, then close the file, but still get the error:</p>
  
  <pre><code>[Thu Oct 07 13:33:07 2010] [error] [client &lt;ip&gt;] mod_wsgi (pid=32541): Exception occurred processing WSGI script '/home/myuser/public_html/django.wsgi'.
  </code></pre>
  
  <p>If I intentionally raise an error, it is logged in the file, so as i get "ok" I think the script worked fine but the result is still that message error... no clues</p>