Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Log WSGI directly to stdout
#1
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
Reply
#2
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.
Reply
#3
(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
Reply
#4
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()
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WSGI Multi processing. simbha 1 2,813 May-05-2020, 10:34 AM
Last Post: pyzyx3qwerty
  wsgi server ports chrisdb 3 3,288 Feb-26-2020, 04:24 PM
Last Post: snippsat
  can i merge stderr and stdout? Skaperen 2 1,983 Sep-29-2019, 03:39 AM
Last Post: Skaperen
  Open Sqlite database file directly in django sourabhjaiswal92 0 2,947 May-22-2018, 05:55 AM
Last Post: sourabhjaiswal92
  Trouble deploying Django (project folder, wsgi.py and libapache2-mod-wsgi) Drone4four 8 7,943 Mar-17-2018, 09:40 PM
Last Post: Drone4four
  Python wsgi example: problem with javascript imonike 12 9,974 Jun-19-2017, 03:27 PM
Last Post: imonike

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020