Logging filter rename not working

1. Output of caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

FROM caddy:2.6.2-alpine

RUN apk update && apk add curl
RUN rm -rf /var/cache/apk/*

COPY --from=builder /usr/bin/caddy /usr/bin/caddy
COPY Caddyfile /etc/caddy/Caddyfile
COPY cert-event.sh /usr/bin/cert-event.sh

a. System environment:


b. Command:

c. Service/unit/compose file:

Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane. -->

d. My complete Caddy config:

(json_log) {
    log {
        format filter {
            wrap   json
            fields {
                request>headers>Cf-Connecting-Ip rename cf_conn_ip
                request>headers>Cf-Ipcountry     rename cf_ip_country
                request>headers>X-Forwarded-For  rename x_forwarded_for
                request>headers>User-Agent       rename user_agent

                request>uri    rename uri
                request>host   rename host
                request>proto  rename proto
                request>method rename method

                tls             delete
                resp_headers    delete
                request>headers delete

3. The problem I’m having:

Log produced:

{"level":"error","ts":1671250922.0369964,"logger":"http.log.access.log1","msg":"handled request",
 "request": {"remote_ip":"","remote_port":"54232","proto":"HTTP/1.1","method":"GET","host":"readacted","uri":"/"},

rename fields are not present as expected.

1 Like

You’re copying from builder, but I don’t see a builder stage there. Are you building with plugins?

Renaming cannot move where something is in the tree. It can’t hoist something deep in an array to be a new top-level key in the logs. It can only rename a log field, in-place.

Since you later delete the request headers, you’re wiping out anything the rename might have done, so it looks like it did nothing.

What you’re trying to do isn’t possible, unfortunately, with the existing tools. If you need to restructure the JSON logs, you might need to use something like GitHub - caddyserver/transform-encoder: Log encoder module for custom log formats to do it. Although that’s not optimized for outputting JSON logs.

1 Like

Yes, just a few plugins, that is irrelevant to this case I think.

Ah, that makes all the sense now. I think I’m gonna leave it as is now, instead of complect it even more.

Thanks for your detailed reply, @francislavoie .

1 Like

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