Access logs
Customize Envoy's per-request log format.
Why Envoy's logs are special
Most reverse proxies log a single line per request and call it a day. Envoy gives you that, plus over 100 command operators you can compose into your own format — and the option to emit structured JSON so your log pipeline can index every field individually.
The result: when you're debugging "why is the p99 spiking?" you can answer it from logs alone, without touching the application.
Plain text format
Add an access_log block to the HTTP Connection Manager:
access_log:
- name: envoy.access_loggers.stdout
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
log_format:
text_format_source:
inline_string: |
[%START_TIME%] "%REQ(:METHOD)% %REQ(X-ENVOY-ORIGINAL-PATH?:PATH)% %PROTOCOL%" %RESPONSE_CODE% %DURATION%ms upstream=%UPSTREAM_HOST% flags=%RESPONSE_FLAGS%
The most useful operators:
| Operator | What it shows |
|---|---|
%START_TIME% | When Envoy received the request |
%REQ(:METHOD)% | HTTP method (GET, POST, …) |
%REQ(:PATH)% | Path including query string |
%RESPONSE_CODE% | HTTP status returned to the client |
%DURATION% | Total request time in ms |
%UPSTREAM_HOST% | Which backend served it (priceless for LB debugging) |
%RESPONSE_FLAGS% | Two-letter codes: UH, UT, UO, URX, etc. |
%REQ(USER-AGENT)% | Any request header — substitute the name |
%RESP(X-CACHE)% | Any response header |
JSON format (the one you actually want in production)
access_log:
- name: envoy.access_loggers.stdout
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
log_format:
json_format:
ts: "%START_TIME%"
method: "%REQ(:METHOD)%"
path: "%REQ(:PATH)%"
status: "%RESPONSE_CODE%"
duration_ms: "%DURATION%"
upstream: "%UPSTREAM_HOST%"
flags: "%RESPONSE_FLAGS%"
ua: "%REQ(USER-AGENT)%"
bytes_out: "%BYTES_SENT%"
One JSON object per request. Loki, Datadog, Splunk — all happy.
%RESPONSE_FLAGS% deserves to be in every format you ever
write. Two letters that tell you exactly why a request looked weird:
UH = no healthy upstream, UO = circuit-breaker
overflow, UT = upstream timeout,
URX = retry limit exceeded, NR = no route. When
something fails, this is where you start.
Hands-on: structured logs into jq
Use the Lesson 5 hello-world Envoy that proxies to httpbin.org. Add the
JSON access_log block above. Restart.
Send a few requests:
for i in 1 2 3 4 5; do curl -s -o /dev/null localhost:10000/get; done
curl -s -o /dev/null localhost:10000/status/503
curl -s -o /dev/null localhost:10000/delay/2
Pipe Envoy's stdout through jq in another terminal:
docker logs -f <envoy-container> 2>&1 | jq 'select(.status >= 400)'
You should see the 503 (and possibly the 504 from the delay if your timeout fires). Now compute a quick p95 over your last batch:
docker logs <envoy-container> 2>&1 \
| jq -r '.duration_ms // empty' \
| sort -n \
| awk 'BEGIN{c=0} {a[c++]=$1} END{print a[int(c*0.95)]}'
That's a one-liner p95 with no monitoring stack at all.
Logging unbounded headers is how you accidentally write a 10MB log line.
Use the truncation form: %REQ(USER-AGENT?MAX:128)% caps that
field at 128 chars. Apply liberally to any user-controlled header.
Up next
Logs are great for individual requests; metrics are great for the shape of the whole system. Lesson 13 turns on Prometheus.
Which command formatter shows the upstream host that handled the request?