Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"capture_output" fails when using "logconfig_dict" for logging configuration #3209

Open
cdhigh opened this issue May 12, 2024 · 0 comments
Open

Comments

@cdhigh
Copy link

cdhigh commented May 12, 2024

The persistence of a bug has been identified in Gunicorn version 22.0.0.
Bug Summary:
In the configuration file gunicorn.conf.py, the capture_output variable fails to function properly when the logging system is configured using logconfig_dict rather than errorlog.

Issue Details:
Previously, I configured Gunicorn using the errorlog variable along with capture_output, which worked as expected. However, the log file grew rapidly, prompting a need for file size control. Consequently, I opted to use logconfig_dict with RotatingFileHandler. After this change, the logging system ceased to capture stdout messages.

Upon investigation of the Gunicorn source code, I identified the following segment:

if self.cfg.capture_output and cfg.errorlog != "-":

if self.cfg.capture_output and cfg.errorlog != "-":
            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())

This code snippet reveals that capture_output only takes effect when errorlog is set. However, I did not set errorlog; instead, I configured the logging system using logconfig_dict.

here is the full content of my gunicorn.conf.py

# gunicorn.conf.py
import os
pythonpath = "/usr/local/lib/python3.9/site-packages"
bind = "0.0.0.0:8000"
workers = 1
threads = 3
capture_output = True
enable_stdio_inheritance = True
#accesslog = "/data/gunicorn.access.log"
#errorlog = "/data/gunicorn.error.log"
#loglevel = "info"
#preload_app = True
certfile = os.getenv('GUNI_CERT')
keyfile = os.getenv('GUNI_KEY')
#example: https://github.com/benoitc/gunicorn/blob/master/gunicorn/glogging.py
logconfig_dict = {
    'version': 1,
    'disable_existing_loggers': False,
    "root": {"level": "INFO", "handlers": ["error_file"]},
    'loggers': {
        "gunicorn.error": {
            "level": "INFO", 
            "handlers": ["error_file"],
            "propagate": False,
            "qualname": "gunicorn.error"
        },
        "gunicorn.access": {
            "level": "INFO",
            "handlers": ["access_file"],
            "propagate": False,
            "qualname": "gunicorn.access"
        }
    },
    'handlers': {
        "error_file": {
            "class": "logging.handlers.RotatingFileHandler",
            "maxBytes": 50*1024*1024, #50M
            "backupCount": 1,
            "formatter": "generic",
            "filename": "/data/gunicorn.error.log"
        },
        "access_file": {
            "class": "logging.handlers.RotatingFileHandler",
            "maxBytes": 10*1024*1024, #10M
            "backupCount": 1,
            "formatter": "access",
            "filename": "/data/gunicorn.access.log"
        }
    },
    'formatters':{
        "generic": {
            "format": "%(asctime)s %(levelname)s [%(filename)s:%(lineno)s] %(message)s",
            "datefmt": "[%Y-%m-%d %H:%M:%S %z]",
            "class": "logging.Formatter"
        },
        "access": {
            "format": "%(message)s",
            "datefmt": "[%Y-%m-%d %H:%M:%S %z]",
            "class": "logging.Formatter"
        }
    }
}
@cdhigh cdhigh changed the title capture_output is not working when config log system using logconfig_dict "capture_output" fails when using "logconfig_dict" for logging configuration May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant