I have a python script. It has a SimpleLogger with sys.stdout as output_stream.

logger = SimpleLogger(level=LogLevel.DEBUG)

When I run it in console, I get the logs properly, but whenever I redirect the output to a file, nothing is found in the target.

I tried multiple ways:

  1. python server.py > /tmp/x.log 2>&1
  2. python server.py > /tmp/x.log

In both cases, the /tmp/x.log is empty.

I also tried nohup python server.py, but nothing has been written in nohup.out.


This is probably just due to buffering. You will only see something in the file when enough output has been accumulated. You can try using python -u to ask for unbuffered output, or set environment variable PYTHONUNBUFFERED= to any non-empty string, as documented in the Python command line documentation, or add a .flush() call after each .debug() or similar call.

  • Both solutions worked. Additionally, mentioning buffer remind me of PYTHONUNBUFFERED environment variable. I tried to set it to 1; this solution worked as well. Would you please add this one to your answer? I think the credit is yours, plus it's better to sum the answer up in one place. – Zeinab Abbasimazar Sep 1 '18 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.