Gunicorn Access Log Format
Gunicorn access logs provide detailed information about HTTP requests processed by the server. The access log format can be customized using Gunicorn's --access-logformat
option. By default, Gunicorn uses a common log format, but you can specify your format string to include additional details or structure the logs differently.
Default Access Log Format
The default access log format in Gunicorn is similar to the Apache combined log format:
%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
Here's what each variable represents:
%(h)s
: Remote host (IP address of the client).%(l)s
: Remote log name (usually ``, since ident lookups are generally disabled).%(u)s
: Remote user (username of the authenticated user, if any).%(t)s
: Date and time of the request.%(r)s
: First line of the request (method, path, and HTTP version).%(s)s
: Status code of the response.%(b)s
: Size of the response body in bytes, excluding headers.%(f)s
: Referer URL (where the request originated).%(a)s
: User-agent string (browser or client information).
Customizing the Access Log Format
You can customize the access log format using the --access-logformat
option or by setting it in the Gunicorn configuration file. Below are some examples of how you can modify the format.
Example 1: Simple Custom Log Format
To include only the essential details like the remote IP, request line, status code, and response size:
gunicorn --access-logfile - --access-logformat '%(h)s "%(r)s" %(s)s %(b)s' myapp:app
This would log entries in the following format:
192.168.1.1 "GET /index.html HTTP/1.1" 200 1234
Example 2: Adding Response Time
To include response time, you can add %(L)s
(request time in seconds):
gunicorn --access-logfile - --access-logformat '%(h)s %(t)s "%(r)s" %(s)s %(b)s %(L)s' myapp:app
Output example:
192.168.1.1 [13/Sep/2024:12:34:56 +0000] "GET /index.html HTTP/1.1" 200 1234 0.123
Example 3: Detailed Format with Request ID
For including the request ID (if your application is setting it) and more details:
gunicorn --access-logfile - --access-logformat '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(L)s %(p)s' myapp:app
%(p)s
: Process ID of the worker handling the request.%(L)s
: Request time in seconds.
Output example:
192.168.1.1 - - [13/Sep/2024:12:34:56 +0000] "GET /index.html HTTP/1.1" 200 1234 "<http://example.com>" "Mozilla/5.0" 0.123 12345
Complete List of Format Variables
%(h)s
: Remote host.%(l)s
: Remote log name.%(u)s
: Remote user.%(t)s
: Date and time of the request.%(r)s
: Request line.%(m)s
: Request method.%(U)s
: URL path without query string.%(q)s
: Query string.%(H)s
: Protocol.%(s)s
: Status code.%(B)s
: Response size in bytes, excluding headers (or `` if zero).%(b)s
: Response size in bytes (or0
).%(f)s
: Referer.%(a)s
: User-agent.%(L)s
: Request time in seconds.%(D)s
: Request time in microseconds.%(T)s
: Request time in seconds as integer.%(p)s
: Process ID of the worker.%(M)s
: Request method.
Setting the Access Log Format in a Configuration File
If you prefer using a Gunicorn configuration file (gunicorn.conf.py
), you can set the access log format like this:
# gunicorn.conf.py
accesslog = '-'
access_log_format = '%(h)s %(t)s "%(r)s" %(s)s %(b)s'
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github