How to write to log in python with nginx + uwsgi

11,291

Solution 1

use logging.StreamHandler as logging handler

Solution 2

uWSGI is a wsgi server, and as such passes a stream in the environ dict passed to the application callable it hosts, using the key wsgi.errors. If you are writing a bare wsgi app, then writing to that stream should do the job. If you are using a framework that abstracts the wsgi interface out (and by the sound of it, you are, print would ordinarily write to sys.stdout, which gets closed on a daemonized process and would never make it to any log file), you will probably need to look into how that framework handles error logging.

Share:
11,291
elkelk
Author by

elkelk

I'm a full stack Senior Engineer with an eye for design. I currently work in Node.js or Ruby on Rails, and I have worked with Python, PHP, and others languages/frameworks in the past. My focus has been around web applications and APIs. I like working on the entirety of an application and enjoy using my creativity to build products.

Updated on June 13, 2022

Comments

  • elkelk
    elkelk almost 2 years

    I have a server running nginx + UWSGI + python. UWSGI is running as a daemon with the flag set: --daemonize /var/log/uwsgi.log which logs all application errors.

    I've noticed that on error if I use a python print statement it will write to the log but only on an error. The standard python logging library doesn't seem to affect the log in any situation.

    How do I point the python logging libraries to use the UWSGI log?

  • elkelk
    elkelk over 12 years
    Although your answer was not detailed, it led me down the right path. This answer describes the method I used: stackoverflow.com/questions/1383254/…
  • Michel Müller
    Michel Müller almost 10 years
    @elkelk: Could you extend? I don't need the filters and tried to log with this method - it still doesn't print anything into the file.
  • msgre
    msgre about 8 years
    I have similar problem but in regards with Docker containers. Take a look at stackoverflow.com/questions/35511839/…