Python Forum
Log WSGI directly to stdout - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Log WSGI directly to stdout (/thread-15572.html)



Log WSGI directly to stdout - ThomasT - Jan-22-2019

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


RE: Log WSGI directly to stdout - Larz60+ - Jan-22-2019

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.


RE: Log WSGI directly to stdout - ThomasT - Jan-23-2019

(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


RE: Log WSGI directly to stdout - Larz60+ - Jan-23-2019

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()



RE: Log WSGI directly to stdout - ThomasT - Jan-24-2019

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