file-logger
Writes request logs to a file on the engine's node.
file-loggerPriority 399What it does
file-logger writes a record of each request that reaches the route or other object it is added to, to a file on the gateway node named under Path. Each line of the file is one record, written as JSON.
The path is relative to the gateway's own directory, logs/orders-access.log for example, and its folder has to exist already. A path the gateway cannot open is saved without complaint, and every line is lost.
When to use it
- The orders API should keep its records in a file of its own on the gateway node,
logs/orders-access.log. - The operations team already collects the log files of the gateway nodes with an agent of its own.
- Every route that carries the plugin should write to one shared file, named once in the plugin metadata.
- A route under test should leave a trace of each request without a collector to send it to.
Not for records sent to an HTTP endpoint — see http-logger (Sends request logs to an HTTP endpoint in batches), records sent to a syslog server — see syslog (Sends request logs to a syslog server) or records kept in Elasticsearch — see elasticsearch-logger (Sends request logs to an Elasticsearch index).
Set it up
Write a record of each request to a file with the fields you choose
The path is relative to the gateway's own directory, and its directory must already exist.
Under Basics set Path to logs/orders-access.log. Under Behavior fill Log format as in the JSON below.
{
"path": "logs/orders-access.log",
"log_format": {
"host": "$host",
"client": "$remote_addr",
"method": "$request_method",
"uri": "$uri",
"status": "$status",
"at": "$time_iso8601"
}
}
More scenarios
Write the records of every route that carries the plugin to one file unless the route names its ownenvironment-wide settings
The path is relative to the gateway's own directory, and its directory must already exist.
Fill Log format as in the JSON below and set Path to logs/requests.log.
{
"path": "logs/requests.log",
"log_format": {
"host": "$host",
"client": "$remote_addr",
"status": "$status",
"at": "$time_iso8601"
}
}
Things to know
- Log format — With the fields of the example, a
GETanswered200is written withmethodset toGETandstatusset to200. The line also carries the route's id underroute_id. - Path — A path the gateway cannot open, whether empty or in a missing folder, is saved without complaint and every line is lost. The environment-wide path in this plugin's metadata is used only when this box is removed through Raw JSON.
- A route whose file-logger carries no path of its own writes to the path in the plugin metadata: the example sends the records of such a route to
logs/requests.log. - Path — Every route carrying this plugin without a path of its own writes here. An empty path is accepted and never opened, and every line those routes would have written is lost.
- Path — A file-logger without a path, here or in the plugin metadata, is refused when you save.
- Match — The console’s form draws one level of conditions; a value that nests deeper is kept as it is and edited in Raw JSON.
- Match — More than twenty groups are refused when you save.
- Log format extra — Added to the default record only: while a log format is set, on this object or in the plugin's metadata, the engine writes that format alone and reads nothing here.
- Include request body when — Not checked at save: an expression the engine cannot read is stored, and the body is then never logged. Only the gateway's error log says so.
- Include response body when — Not checked at save: an expression the engine cannot read is stored, and the body is then never logged. Only the gateway's error log says so.
- AdviceRotate the file with a tool on the gateway node, and where the gateway runs on several nodes, prefer a logger that sends the records to one place.
Settings
The fields as the console shows them, section by section.
Basics
pathstringRequiredA file the gateway can write to, such as logs/access.log, which is relative to the gateway's own directory.
Behavior
log_formatobject{"host":"$host","client":"$remote_addr","status":"$status","at":"$time_iso8601"}matcharraylog_format_extraobject{"zone":"eu-west","gateway":"$hostname"}include_req_bodybooleandefault falseinclude_req_body_exprarrayinclude_resp_bodybooleandefault falseinclude_resp_body_exprarraymax_req_body_bytesintegerdefault 524288max_resp_body_bytesintegerdefault 524288Environment-wide settings
Set once per environment, under Plugins › file-logger. Every file-logger in the environment uses these.
log_formatobject{"host":"$host","client":"$remote_addr","status":"$status","at":"$time_iso8601"}log_format_extraobject{"zone":"eu-west","gateway":"$hostname"}pathstring