From efdc99dd91e67b4e03a764c136154146f597d28a Mon Sep 17 00:00:00 2001 From: Christos Stavrakakis Date: Thu, 15 Nov 2012 14:43:15 +0200 Subject: [PATCH] Reopen stdout & stderr if redirected to error log To use the logrotate utility, a USR1 signal is sent, and the corresponding handler reopens the log files. However, sys.stdout and sys.stderr, which may be redirected to the error log file, are not updated. This commit fixes this, by closing the fileobj of the LazyWriter object. There is no need to reopen it, since the LazyWriter will open it when needed. --- gunicorn/glogging.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index d15925fd..1c5c603b 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -74,6 +74,16 @@ class LazyWriter(object): self.lock.release() return self.fileobj + def close(self): + if self.fileobj: + self.lock.acquire() + try: + if self.fileobj: + self.fileobj.close() + self.fileobj = None + finally: + self.lock.release() + def write(self, text): fileobj = self.open() fileobj.write(text) @@ -239,6 +249,10 @@ class Logger(object): def reopen_files(self): + if self.cfg.errorlog != "-": + # Close stderr & stdout if they are redirected to error log file + sys.stderr.close() + sys.stdout.close() for log in loggers(): for handler in log.handlers: if isinstance(handler, logging.FileHandler):