How to get response and request body in log file in GUNICORN?

Viewed 316

As of now, I am using a .conf file for logging in gunicorn.

My gunicorn.conf file looks like:

[loggers]
keys=root, gunicorn.error, gunicorn.access

[handlers]
keys=console, error_file, access_file

[formatters]
keys=generic, access

[logger_root]
level=INFO
handlers=console

[logger_gunicorn.error]
level=INFO
handlers=error_file
propagate=1
qualname=gunicorn.error

[logger_gunicorn.access]
level=INFO
handlers=access_file
propagate=0
qualname=gunicorn.access

[handler_console]
class=StreamHandler
formatter=generic
args=(sys.stdout, )

[handler_error_file]
class=logging.FileHandler
formatter=generic
args=('/var/log/gunicorn/gunicorn_error.log',)

[handler_access_file]
class=logging.FileHandler
formatter=access
args=('/var/log/gunicorn/gunicorn_access.log',)

[formatter_generic]
format={Time:[%(asctime)s], Process:[%(process)d], LogLevel:[%(levelname)s], Module:[%(module)s], Message:[%(message)s], PathName:[%(filename)s], ThreadId:[%(thread)d], ThreadName:[%(threadName)s]}
datefmt=%Y-%m-%d %H:%M:%S
class=logging.Formatter

[formatter_access]
format={Time:[%(asctime)s], Process:[%(process)d], LogLevel:[%(levelname)s], Module:[%(module)s], Message:[%(message)s], PathName:[%(filename)s], ThreadId:[%(thread)d], ThreadName:[%(threadName)s]}
class=logging.Formatter

I am running fastAPI wrapped around gunicorn.

Is there any way I can add request and response bodies in my access logfile?

I tried adding sys.stdout to the args parameter in handler_error_file, handler_access_file but that is throwing the error.

Traceback (most recent call last):
  File "/usr/bin/gunicorn", line 11, in <module>
    load_entry_point('gunicorn==20.0.4', 'console_scripts', 'gunicorn')()
  File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 58, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 228, in run
    super().run()
  File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 58, in __init__
    self.setup(app)
  File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 93, in setup
    self.log = self.cfg.logger_class(app.cfg)
  File "/usr/lib/python3/dist-packages/gunicorn/glogging.py", line 195, in __init__
    self.setup(cfg)
  File "/usr/lib/python3/dist-packages/gunicorn/glogging.py", line 246, in setup
    fileConfig(cfg.logconfig, defaults=defaults,
  File "/usr/lib/python3.8/logging/config.py", line 79, in fileConfig
    handlers = _install_handlers(cp, formatters)
  File "/usr/lib/python3.8/logging/config.py", line 145, in _install_handlers
    h = klass(*args, **kwargs)
  File "/usr/lib/python3.8/logging/__init__.py", line 1147, in __init__
    StreamHandler.__init__(self, self._open())
  File "/usr/lib/python3.8/logging/__init__.py", line 1176, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
TypeError: open() argument 'mode' must be str, not _io.TextIOWrapper
0 Answers
Related