Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to log in python with nginx + uwsgi

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?

like image 939
elkelk Avatar asked Nov 05 '11 18:11

elkelk


People also ask

Where does uWSGI log to?

Logging to sockets will send log entries to the Unix socket /tmp/uwsgi.

What is uWSGI in nginx?

Nginx implements a uwsgi proxying mechanism, which is a fast binary protocol that uWSGI can use to talk with other servers. The uwsgi protocol is actually uWSGI's default protocol, so simply by omitting a protocol specification, it will fall back to uwsgi .

What is the difference between Nginx and uWSGI?

Nginx is “a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache”. uWSGI is an implementation of the WSGI spec, which describes how a web server should communicate with a web app, which makes uWSGI also a type of web server.

How do I use uWSGI app?

The most basic way to run uWSGI is to tell it to start an HTTP server and import your application. If you're using the app factory pattern, you'll need to create a small Python file to create the app, then point uWSGI at that.


2 Answers

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.

like image 65
SingleNegationElimination Avatar answered Oct 04 '22 03:10

SingleNegationElimination


use logging.StreamHandler as logging handler

like image 35
roberto Avatar answered Oct 04 '22 01:10

roberto