Skip to main content

File Logger

The file-logger plugin writes gateway request and response logs as newline-delimited JSON files for local collection and troubleshooting. It supports detailed default records, focused custom formats, and conditional logging.

Log files are local to each gateway instance. Mount writable persistent storage when logs must survive container or pod replacement.

Examples​

The examples show how to write a default access record and how to log selected requests with a focused custom format.

For Docker verification, set GATEWAY_CONTAINER to the running APISIX or API7 Gateway container. The Kubernetes inspection tabs use kubectl instead.

export GATEWAY_CONTAINER=replace-with-gateway-container-name

Write a Default Access Log​

Configure a route to append its access records to a local file:

curl "http://127.0.0.1:9180/apisix/admin/routes/file-logger-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"uri": "/anything/file-logger",
"plugins": {
"file-logger": {
"path": "/tmp/file-logger-example.log"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Send a request with a query parameter:

curl "http://127.0.0.1:9080/anything/file-logger?source=docs" \
--silent \
--show-error \
--fail \
--output /dev/null

Read the most recent entry and select stable fields from the default format:

docker exec "$GATEWAY_CONTAINER" \
sh -lc 'tail -n 1 /tmp/file-logger-example.log' | \
jq '{
method: .request.method,
uri: .request.uri,
status: .response.status
}'

The entry contains the request and response details:

{
"method": "GET",
"uri": "/anything/file-logger?source=docs",
"status": 200
}

Log Selected Requests in a Custom Format​

Update the route to replace the default entry with selected fields and write a log only when the log query parameter equals yes:

curl "http://127.0.0.1:9180/apisix/admin/routes/file-logger-route/plugins/file-logger" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"path": "/tmp/file-logger-example.log",
"log_format": {
"method": "$request_method",
"uri": "$request_uri",
"status": "$status"
},
"match": [
[
["arg_log", "==", "yes"]
]
]
}'

Clear the example file:

docker exec "$GATEWAY_CONTAINER" \
sh -lc ': > /tmp/file-logger-example.log'

Send one request that does not match and one that does:

curl "http://127.0.0.1:9080/anything/file-logger?log=no" \
--silent \
--show-error \
--fail \
--output /dev/null

curl "http://127.0.0.1:9080/anything/file-logger?log=yes" \
--silent \
--show-error \
--fail \
--output /dev/null

Count the entries and inspect the logged request:

docker exec "$GATEWAY_CONTAINER" \
sh -lc 'printf "Log entries: "; wc -l < /tmp/file-logger-example.log'

docker exec "$GATEWAY_CONTAINER" \
sh -lc 'tail -n 1 /tmp/file-logger-example.log' | \
jq '{method, uri, status}'

Only the matching request is logged in the custom format:

Log entries: 1
{
"method": "GET",
"uri": "/anything/file-logger?log=yes",
"status": 200
}

Protect and Rotate Log Files​

The configured parent directory must exist and be writable by the gateway process. A write failure is recorded in the gateway error log and does not fail the proxied request.

Default records contain request, response, upstream, latency, route, service, consumer, client, and gateway details. Headers and query parameters can contain credentials, cookies, and API keys. Restrict access to log files and volumes, and use a focused custom format or masking when sensitive values should not be stored.

The gateway caches open file handles. After rename-based rotation, send the gateway's NGINX log-reopen signal (USR1) so it closes the renamed file and opens the configured path. Truncating a file in place keeps the same handle, and subsequent entries continue in the truncated file without a reopen signal.