Python Forum

Full Version: Log WSGI directly to stdout
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

How can I log from wsgi_script directly to stdout?
We are using it in a docker container. httpd writes to stdout. But any logs from wsgi appear as error logs from apache httpd.

I want to log directly jsons logs onto stdout.

Regards Thomas
you can write directly to stdout:
import sys

...
sys.write.stdout('some text')
the output is verbose, so any formatting like trailing space or newline gets output as well as text.
(Jan-22-2019, 09:05 PM)Larz60+ Wrote: [ -> ]you can write directly to stdout:

Hi,

sadly this don't work.

print "-----------------------------------------"
print "user:", pwd.getpwuid(os.getuid())[0]
print "version:", sys.version
print "path:", os.path.dirname(sys.executable)
print "-----------------------------------------"

sys.stdout.write('THOMAS1')
The first lines appear in the httpd error log. "Thomas" don't.
I can write to file:
handler=logging.FileHandler("foo.txt")
But not into std file descriptor.

handler=logging.FileHandler("/proc/self/fd/1")
May be this has to do, that the script runs under a different user?

Locally, not as wsgi script all work.

Thomas
what I showed does work, for writing directly to stdout.
what you're asking to do is 'redirect' all output to stdout.
That's done differently Here's an example:
from io import StringIO
import sys


def stuff():
    print("Hello Dolly, well hello Dolly")
    print("It's so nice to have you back where you belong")
    print("You're looking swell, Dolly, we can tell, Dolly")

def redirect(filename):
    return open(filename, 'w')

def main():
    save_stdout = sys.stdout
    sys.stdout = redirect('mylog.txt')
    stuff()
    sys.stdout = save_stdout

if __name__ == '__main__':
    main()
Thanks so far.

But that is not the real problem here. My problem is to do this from inside a wsgi-script that runs also on a different user (than apache). And all in docker container.

Thomas