modify log_file option

Gunicorn should generally only bother writing its own log and let
the application handle the way it want to log its own errors.

Now the log_file option will be overriden by the gunicorn options
`--error-logfile` and `--access-logfile` if they are given.
This commit is contained in:
benoitc 2013-12-27 11:50:16 +01:00
parent 5fb4b49a91
commit a0ccfa0c4f
3 changed files with 69 additions and 38 deletions

View File

@ -46,3 +46,33 @@ class=logging.Formatter
[formatter_access]
format=%(message)s
class=logging.Formatter
2014-01-01 14:30:00 [15391] [INFO] Starting gunicorn 18.2
2014-01-01 14:30:00 [15391] [INFO] Listening at: http://127.0.0.1:8000 (15391)
2014-01-01 14:30:00 [15391] [INFO] Using worker: sync
2014-01-01 14:30:00 [15395] [INFO] Booting worker with pid: 15395
2014-01-01 14:30:00 [15396] [INFO] Booting worker with pid: 15396
2014-01-01 14:30:00 [15397] [INFO] Booting worker with pid: 15397
2014-01-01 14:30:02 [15397] [INFO] Worker exiting (pid: 15397)
2014-01-01 14:30:02 [15396] [INFO] Worker exiting (pid: 15396)
2014-01-01 14:30:02 [15391] [INFO] Handling signal: int
2014-01-01 14:30:02 [15391] [INFO] Shutting down: Master
2014-01-01 14:31:36 [15399] [INFO] Starting gunicorn 18.2
2014-01-01 14:31:36 [15399] [INFO] Listening at: http://127.0.0.1:8000 (15399)
2014-01-01 14:31:36 [15399] [INFO] Using worker: sync
2014-01-01 14:31:36 [15402] [INFO] Booting worker with pid: 15402
2014-01-01 14:31:36 [15403] [INFO] Booting worker with pid: 15403
2014-01-01 14:31:36 [15404] [INFO] Booting worker with pid: 15404
2014-01-01 14:31:37 [15403] [INFO] Worker exiting (pid: 15403)
2014-01-01 14:31:37 [15404] [INFO] Worker exiting (pid: 15404)
2014-01-01 14:31:37 [15399] [INFO] Handling signal: int
2014-01-01 14:31:37 [15399] [INFO] Shutting down: Master
2014-01-01 14:32:18 [15405] [INFO] Starting gunicorn 18.2
2014-01-01 14:32:18 [15405] [INFO] Listening at: http://127.0.0.1:8000 (15405)
2014-01-01 14:32:18 [15405] [INFO] Using worker: sync
2014-01-01 14:32:18 [15408] [INFO] Booting worker with pid: 15408
2014-01-01 14:32:18 [15409] [INFO] Booting worker with pid: 15409
2014-01-01 14:32:18 [15410] [INFO] Booting worker with pid: 15410
2014-01-01 14:32:20 [15410] [INFO] Worker exiting (pid: 15410)
2014-01-01 14:32:20 [15409] [INFO] Worker exiting (pid: 15409)
2014-01-01 14:32:20 [15405] [INFO] Handling signal: int
2014-01-01 14:32:20 [15405] [INFO] Shutting down: Master

View File

@ -983,7 +983,7 @@ class ErrorLog(Setting):
cli = ["--error-logfile", "--log-file"]
meta = "FILE"
validator = validate_string
default = "-"
default = None
desc = """\
The Error log file to write to.
@ -1039,10 +1039,11 @@ class LogConfig(Setting):
validator = validate_string
default = None
desc = """\
The log config file to use.
Gunicorn uses the standard Python logging module's Configuration
file format.
"""
The log config file to use.
Gunicorn uses the standard Python logging module's Configuration
file format.
"""
class SyslogTo(Setting):
name = "syslog_addr"

View File

@ -13,7 +13,6 @@ import sys
import traceback
import threading
from gunicorn import util
from gunicorn.six import string_types
@ -168,40 +167,40 @@ class Logger(object):
self.setup(cfg)
def setup(self, cfg):
if not cfg.logconfig:
loglevel = self.LOG_LEVELS.get(cfg.loglevel.lower(), logging.INFO)
self.error_log.setLevel(loglevel)
self.access_log.setLevel(logging.INFO)
loglevel = self.LOG_LEVELS.get(cfg.loglevel.lower(), logging.INFO)
self.error_log.setLevel(loglevel)
self.access_log.setLevel(logging.INFO)
if cfg.errorlog != "-":
# if an error log file is set redirect stdout & stderr to
# this log file.
for stream in sys.stdout, sys.stderr:
stream.flush()
if cfg.errorlog != "-":
# if an error log file is set redirect stdout & stderr to
# this log file.
for stream in sys.stdout, sys.stderr:
stream.flush()
self.logfile = open(cfg.errorlog, 'a+')
os.dup2(self.logfile.fileno(), sys.stdout.fileno())
os.dup2(self.logfile.fileno(), sys.stderr.fileno())
self.logfile = open(cfg.errorlog, 'a+')
os.dup2(self.logfile.fileno(), sys.stdout.fileno())
os.dup2(self.logfile.fileno(), sys.stderr.fileno())
# set gunicorn.error handler
self._set_handler(self.error_log, cfg.errorlog,
logging.Formatter(self.error_fmt, self.datefmt))
# set gunicorn.error handler
self._set_handler(self.error_log, cfg.errorlog,
logging.Formatter(self.error_fmt, self.datefmt))
# set gunicorn.access handler
if cfg.accesslog is not None:
self._set_handler(self.access_log, cfg.accesslog,
fmt=logging.Formatter(self.access_fmt))
# set gunicorn.access handler
if cfg.accesslog is not None:
self._set_handler(self.access_log, cfg.accesslog,
fmt=logging.Formatter(self.access_fmt))
# set syslog handler
if cfg.syslog:
self._set_syslog_handler(self.error_log, cfg, self.syslog_fmt)
# set syslog handler
if cfg.syslog:
self._set_syslog_handler(self.error_log, cfg, self.syslog_fmt)
else:
if cfg.logconfig:
if os.path.exists(cfg.logconfig):
fileConfig(cfg.logconfig, defaults=CONFIG_DEFAULTS,
disable_existing_loggers=False)
else:
raise RuntimeError("Error: log config '%s' not found" % cfg.logconfig)
raise RuntimeError("Error: log config '%s' not found" %
cfg.logconfig)
def critical(self, msg, *args, **kwargs):
self.error_log.critical(msg, *args, **kwargs)
@ -334,15 +333,16 @@ class Logger(object):
if h:
log.handlers.remove(h)
if output == "-":
h = logging.StreamHandler()
else:
util.check_is_writeable(output)
h = logging.FileHandler(output)
if output is not None:
if output == "-":
h = logging.StreamHandler()
else:
util.check_is_writeable(output)
h = logging.FileHandler(output)
h.setFormatter(fmt)
h._gunicorn = True
log.addHandler(h)
h.setFormatter(fmt)
h._gunicorn = True
log.addHandler(h)
def _set_syslog_handler(self, log, cfg, fmt):
# setup format