file-logger

Description

file-logger is a plugin that pushes a stream of log data to a specified location. For example, the output path can be customized: logs/file.log.

Attributes

NameTypeRequirementDefaultValidDescription
pathstringrequiredCustomise the output file path

How To Enable

This is an example of how to enable the file-logger plugin for a specific route.

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "file-logger": {
  5. "path": "logs/file.log"
  6. }
  7. },
  8. "upstream": {
  9. "type": "roundrobin",
  10. "nodes": {
  11. "127.0.0.1:9001": 1
  12. }
  13. },
  14. "uri": "/hello"
  15. }'

Test Plugin

success:

  1. $ curl -i http://127.0.0.1:9080/hello
  2. HTTP/1.1 200 OK
  3. ...
  4. hello, world

You can then find the file.log file in the corresponding logs directory.

Metadata

NameTypeRequirementDefaultValidDescription
log_formatobjectoptional{“host”: “$host”, “@timestamp”: “$time_iso8601”, “client_ip”: “$remote_addr”}Log format declared as key value pair in JSON format. Only string is supported in the value part. If the value starts with $, it means to get APISIX variable or Nginx variable.

Example

  1. curl http://127.0.0.1:9080/apisix/admin/plugin_metadata/file-logger -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "log_format": {
  4. "host": "$host",
  5. "@timestamp": "$time_iso8601",
  6. "client_ip": "$remote_addr"
  7. }
  8. }'

It is expected to see some logs like that:

  1. {"host":"localhost","@timestamp":"2020-09-23T19:05:05-04:00","client_ip":"127.0.0.1","route_id":"1"}
  2. {"host":"localhost","@timestamp":"2020-09-23T19:05:05-04:00","client_ip":"127.0.0.1","route_id":"1"}

Disable Plugin

Remove the corresponding json configuration in the plugin configuration to disable the file-logger. APISIX plugins are hot-reloaded, therefore no need to restart APISIX.

  1. $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/hello",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:9001": 1
  10. }
  11. }
  12. }'