0

I have a gunicorn application running locally, and it does log requests to a text file. What I don't understand is how to log messages to the gunicorn log file from my gunicorn application. To start my app, I use a command like % gunicorn --workers=2 test:app --reload --log-file log.txt --log-level 'debug'

I'd like to have code like this:

def app(environ, start_response):

    path = environ.get("PATH_INFO")

    if path == "/abc":
        logging.info(f'user requested {path}')

And have gunicorn write an info log entry if that's appropriate to the level.

I checked the documentation, and I see that gunicorn uses a class gunicorn.glogging.Logger to log, but I don't see any documentation on how to instantiate that class or use it to log my own messages.

jaycer
  • 2,941
  • 2
  • 26
  • 36

1 Answers1

0

To log to the gunicorn log file, you must import logging and get a logger using the string "gunicorn.error"

import logging

gunicorn_logger = logging.getLogger('gunicorn.error')

and then you can do

gunicorn_logger.warning(f'returned 404 for: {path}')

Source How to use the logging module in Python with gunicorn

jaycer
  • 2,941
  • 2
  • 26
  • 36