Multiple format directives inside log section

1. Caddy version (caddy version):

v2.4.1 h1:kAJ0JB5Xk5gPdTH/27S5cyoMGqD5lBAe9yZ8zTjVJa0=
(docker image caddy:alpine)

2. How I run Caddy:

caddy run --config /etc/caddy/Caddyfile --adapter caddyfile
(default entrypoint)

a. System environment:

Docker

b. Command:

docker run -d -p 80:80 caddy:apline

c. Service/unit/compose file:

No additional files

d. My complete Caddyfile or JSON config:

{
	auto_https off
}

:80 {
	root * /usr/share/caddy
	try_files {path} /index.html
	log {
		output stdout
		format filter {
			wrap json
			fields {
				common_log delete
				resp_headers delete
				request>headers>Accept delete
			}
		}
		format json {
			time_format iso8601
			time_key timestamp
		}
	}
	file_server
}

3. The problem Iā€™m having:

I want to change some log fields and delete another ones. But the only last format directive takes precedence

4. Error messages and/or full log output:

{"level":"info","timestamp":"2021-05-30T17:11:27.348Z","logger":"http.log.access.log0","msg":"handled request","request":{"remote_addr":"172.17.0.1:63574","proto":"HTTP/1.1","method":"GET","host":"127.1","uri":"/","headers":{"User-Agent":["curl/7.64.1"],"Accept":["*/*"]}},"common_log":"172.17.0.1 - - [30/May/2021:17:11:27 +0000] \"GET / HTTP/1.1\" 200 3192","duration":0.0004962,"size":3192,"status":200,"resp_headers":{"Server":["Caddy"],"Etag":["\"qtvplx2go\""],"Content-Type":["text/html; charset=utf-8"],"Last-Modified":["Sat, 29 May 2021 17:22:45 GMT"],"Accept-Ranges":["bytes"],"Content-Length":["3192"]}}

5. What I already tried:

I tried use comma between format directives but got syntax error obviously

6. Links to relevant resources:

Put your json format settings under the wrap. The filter formatter wraps another format which is nested within it.

	log {
		output stdout
		format filter {
			wrap json {
				time_format iso8601
				time_key timestamp
			}
			fields {
				common_log delete
				resp_headers delete
				request>headers>Accept delete
			}
		}
	}
3 Likes

Thank you, @francislavoie! It works!

1 Like

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