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.
This commit is contained in:
benoitc 2014-01-01 16:09:47 +01:00
parent 03a6136083
commit ee08ac8644

View File

@ -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),