V2 Custom Logging with Caddyfile

1. Caddy version (caddy version):

v2.0.0

2. How I run Caddy:

a. System environment:

macOS 10.14.6

b. Command:

caddy run

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

(encoding) {
  encode zstd gzip
}

(logs) {
  log {
    format json
    output stderr
  }
}

localhost {
  import logs
  import encoding

  # Routes by services
  import ./routes/common/customers

3. The problem I’m having:

Is there a way to create custom log output using directives within the Caddyfile?
I would like to create logs with the following structure, removing sensitive fields (e.g., Authorization from the headers):

{
  "time": "2020-05-14T09:31:48+00:00",
  "request_method": "POST",
  "request_uri": "/v1/customers",
  ..........
}

4. Error messages and/or full log output:

5. What I already tried:

I have tried using the API and using filtering in order to remove sensitive fields (e.g., Authorization header). For instance,

{
  "default": {
    "exclude": [
      "http.log.access.log0"
    ]
  },
  "log0": {
    "encoder": {
      "format": "filter",
      "wrap": {
        "format": "json"
      },
      "fields": {
        "request>headers>Authorization": {
          "filter": "delete"
        }
      }
    },
    "include": [
      "http.log.access.log0"
    ],
    "writer": {
      "output": "stderr"
    }
  }
}

This removes the fields that I don’t want to appear in the logs but this doesn’t seem to be achievable using a Caddyfile. I can see from the docs that the filter format is not available through the log directive.

I’ve also tried using single_field as described by @francislavoie in a previous post but I could not get this to work as desired. For instance, specifying the following in the Caddyfile did not produce any log output at all

(logs) {
  log {
    format single_field {uri}
    output stderr
  }
}

Can anyone suggest how to generate logs with a custom json structure including only a specific set of fields?

6. Links to relevant resources:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.