Combining wrap and time_key in log directive

I want to rename the ts field in logs emitted by Caddy in order to be able to ingest them into Loki, a log ingestion tool that uses the ts field to note down the ingest time of the log. This is easily achieved through the time_key option as shown below:

test.example.app {
    reverse_proxy 127.0.0.1:8080
    log {
        format json {
            time_key timestamp
        }
        output file /var/log/caddy/access.log {
            roll_size 4MiB
            roll_keep 3
        }
    }
}

However, I also don’t want the common_log field and want to delete it, but I’m not sure how to get both format filter { wrap } and the time_key option working. I tried the following but it doesn’t work:

test.example.app {
    reverse_proxy 127.0.0.1:8080
    log {
        format filter {
            wrap json {
                fields {
                    common_log delete
                }
                time_key timestamp
            }
        }
        output file /var/log/caddy/access.log {
            roll_size 4MiB
            roll_keep 3
        }
    }
}

Is there a way to combine these two directives?

1 Like

Yeah, just put time_key just above wrap, not inside of wrap

1 Like

Also, in v2.5 we’ve removed the common_log field :grin:

1 Like

On a related note, any way to use time_format with format filter?

Tried this but it gives caddyfile parsing error:

(clog) {
	log {
		output file accesslogs/{args.0}.log.json
		format filter {
			time_format iso8601
			wrap json
			fields {
				resp_headers>X-Robots-Tag delete
				resp_headers>X-Content-Type-Options delete
				resp_headers>Referrer-Policy delete
				resp_headers>Permissions-Policy delete
				resp_headers>Strict-Transport-Security delete
				resp_headers>Content-Security-Policy delete
			}
		}
	}
}

I’m not sure, but doesn’t it need to be:

wrap json {
    time_format iso8601
}

That worked! Thanks Matt.

1 Like

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