From ee08ac86441e36c3433849b79b3839d1425647fd Mon Sep 17 00:00:00 2001 From: benoitc Date: Wed, 1 Jan 2014 16:09:47 +0100 Subject: [PATCH] make sure to redirect wsgi.errors when needed By if the options ` --error-logfile` is set to '-' then wsgi.errors are returned to sys.stderr, if any log file is passed either using the log-config file or the option, Errors are written to the files. By default no error is returned. --- gunicorn/http/wsgi.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index becbac89..c891cfde 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -3,6 +3,7 @@ # This file is part of gunicorn released under the MIT license. # See the NOTICE for more information. +import io import logging import os import re @@ -42,10 +43,35 @@ class FileWrapper(object): raise IndexError +class WSGIErrorsWraper(io.RawIOBase): + + def __init__(self, cfg): + errorlog = logging.getLogger("gunicorn.error") + self.streams = [] + + if cfg.errorlog == "-": + self.streams.append(sys.stderr) + + for h in errorlog.handlers: + if hasattr(h, "stream"): + self.streams.append(h.stream) + + if not self.streams: + self.streams.append(sys.stderr) + + def write(self, data): + for stream in self.streams: + try: + stream.write(data) + except UnicodeError: + stream.write(data.encode("UTF-8")) + stream.flush() + + def default_environ(req, sock, cfg): return { "wsgi.input": req.body, - "wsgi.errors": sys.stderr, + "wsgi.errors": WSGIErrorsWraper(cfg), "wsgi.version": (1, 0), "wsgi.multithread": False, "wsgi.multiprocess": (cfg.workers > 1),