Search code examples
pythonloggingsleepsyslog

Why time.sleep() breaks syslog-ng in Python


I have been trying to log a script output with syslog-ng, however, if I use any time.sleep() function in my code, it breaks syslog-ng and stops logging the output of the script.

Here are the details.

// samplescript.py
import time

while True:
    print "hello world"
    time.sleep(5)

I use pipe to get it's output to syslog-ng, and I use unix logger tool, so I'm calling the script like this;

$ python sampleoutput.py | logger

This is not generating any output to my log file. The code is simple, and working.

By the way, I don't thing anything wrong with syslog-ng conf file, since if i use the code below, it works as expected.

// samplescript.py    
while True:
    print "hello world"

Q: why time.sleep() is breaking syslog-ng? Is there any equivalence for sleep() function that I might use on my code?

thanks in advance.


Solution

  • You need to flush the buffered stdout-stream:

    import sys
    sys.stdout.flush()
    

    How to flush output of Python print?