Always use the the user configured logger class.

Previously, configuring statsd_host would override the configured class
This commit is contained in:
bloodearnest 2016-01-29 10:28:00 +00:00
parent f68a043c24
commit 34b595e817
3 changed files with 44 additions and 9 deletions

View File

@ -532,6 +532,10 @@ The logger you want to use to log events in Gunicorn.
The default class (``gunicorn.glogging.Logger``) handle most of The default class (``gunicorn.glogging.Logger``) handle most of
normal usages in logging. It provides error and access logging. normal usages in logging. It provides error and access logging.
If you enable statsd support, then a special subclass
(``gunicorn.instrument.statsd.Statsd``) is used instead, which handles
sending the metrics to *statsd_host*
You can provide your own worker by giving Gunicorn a You can provide your own worker by giving Gunicorn a
Python path to a subclass like ``gunicorn.glogging.Logger``. Python path to a subclass like ``gunicorn.glogging.Logger``.
Alternatively the syntax can also load the Logger class Alternatively the syntax can also load the Logger class
@ -611,6 +615,9 @@ statsd_host
``host:port`` of the statsd server to log to. ``host:port`` of the statsd server to log to.
Note: enabling this switches the default *logger_class* to
``gunicorn.instrument.statsd.Statsd``
.. versionadded:: 19.1 .. versionadded:: 19.1
statsd_prefix statsd_prefix

View File

@ -144,16 +144,18 @@ class Config(object):
uri = self.settings['logger_class'].get() uri = self.settings['logger_class'].get()
if uri == "simple": if uri == "simple":
# support the default # support the default
uri = "gunicorn.glogging.Logger" uri = LoggerClass.default
# if statsd is on, automagically switch to the statsd logger # if default logger is in use, and statsd is on, automagically switch
if uri is not None and 'statsd_host' in self.settings and self.settings['statsd_host'].value is not None: # to the statsd logger
logger_class = util.load_class("gunicorn.instrument.statsd.Statsd", if uri == LoggerClass.default:
section="gunicorn.loggers") if 'statsd_host' in self.settings and self.settings['statsd_host'].value is not None:
else: uri = "gunicorn.instrument.statsd.Statsd"
logger_class = util.load_class(uri,
default="gunicorn.glogging.Logger", logger_class = util.load_class(
section="gunicorn.loggers") uri,
default="gunicorn.glogging.Logger",
section="gunicorn.loggers")
if hasattr(logger_class, "install"): if hasattr(logger_class, "install"):
logger_class.install() logger_class.install()

View File

@ -13,6 +13,8 @@ import pytest
from gunicorn import config from gunicorn import config
from gunicorn.app.base import Application from gunicorn.app.base import Application
from gunicorn.workers.sync import SyncWorker from gunicorn.workers.sync import SyncWorker
from gunicorn import glogging
from gunicorn.instrument import statsd
dirname = os.path.dirname(__file__) dirname = os.path.dirname(__file__)
def cfg_module(): def cfg_module():
@ -57,6 +59,9 @@ def test_property_access():
# Class was loaded # Class was loaded
assert c.worker_class == SyncWorker assert c.worker_class == SyncWorker
# logger class was loaded
assert c.logger_class == glogging.Logger
# Workers defaults to 1 # Workers defaults to 1
assert c.workers == 1 assert c.workers == 1
c.set("workers", 3) c.set("workers", 3)
@ -259,3 +264,24 @@ def test_nworkers_changed():
c.set("nworkers_changed", nworkers_changed_3) c.set("nworkers_changed", nworkers_changed_3)
assert c.nworkers_changed(1, 2, 3) == 3 assert c.nworkers_changed(1, 2, 3) == 3
def test_statsd_changes_logger():
c = config.Config()
assert c.logger_class == glogging.Logger
c.set('statsd_host', 'localhost:12345')
assert c.logger_class == statsd.Statsd
class MyLogger(glogging.Logger):
# dummy custom logger class for testing
pass
def test_always_use_configured_logger():
c = config.Config()
c.set('logger_class', __name__ + '.MyLogger')
assert c.logger_class == MyLogger
c.set('statsd_host', 'localhost:12345')
# still uses custom logger over statsd
assert c.logger_class == MyLogger