Caddyfile log customizing examples?

1. Caddy version (caddy version):


2. How I run Caddy:

currently use localhost

a. System environment:

Windows 11

b. Command:

caddy start

c. Service/unit/compose file:


d. My complete Caddyfile or JSON config:


 :80, localhost, http:// {
    root * B:/tests
	file_server browse
	log {
		output file site.log
		format json

3. The problem I’m having:

I went through the documentation for format-modules and couldn’t figure out how to customize the information that appears in the log. I only wanted the most basic information, just URI and referrer, and I know that the ts, level, logger, and msg fields also get included. I bolded the fields that I wanted to keep in the output below.

I saw documentation for calling the log formatter (like json) and saw the json documentation for log in the link I included below, but I have no idea how to combine the two to get any customization. I tried compiling with the transform-encoder module as well and couldn’t get it to work.

If you could provide an example of what I should include in my caddyfile that would help so much. Thanks for your time.

4. Error messages and/or full log output:

{“level”:“info”,“ts”:1650378795.636168,“logger”:“http.log.access.log0”,“msg”:"handled request",“request”:{“remote_ip”:"::1",“remote_port”:“63751”,“proto”:“HTTP/2.0”,“method”:“GET”,“host”:“localhost”,“uri”:"/folder%202/",“headers”:{“Upgrade-Insecure-Requests”:[“1”],“User-Agent”:[“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36”],“Accept”:[“text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9”],“Accept-Language”:[“en-US,en;q=0.9”],“Accept-Encoding”:[“gzip, deflate, br”],“Sec-Gpc”:[“1”],“Sec-Fetch-Site”:[“same-origin”],“Sec-Fetch-Mode”:[“navigate”],“Sec-Fetch-User”:["?1"],“Sec-Fetch-Dest”:[“document”],“Referer”:[“https://localhost/folder%202/”]},“tls”:{“resumed”:false,“version”:772,“cipher_suite”:4865,“proto”:“h2”,“server_name”:“localhost”}},“user_id”:"",“duration”:0.0013091,“size”:15645,“status”:200,“resp_headers”:{“Server”:[“Caddy”],“Content-Type”:[“text/html; charset=utf-8”]}}
{“level”:“info”,“ts”:1650378800.5284705,“logger”:“http.log.access.log0”,“msg”:"handled request",“request”:{“remote_ip”:"::1",“remote_port”:“63751”,“proto”:“HTTP/2.0”,“method”:“GET”,“host”:“localhost”,“uri”:"/folder%202/New%20Bitmap%20image.bmp",“headers”:{“User-Agent”:[“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36”],“Sec-Gpc”:[“1”],“Sec-Fetch-Mode”:[“navigate”],“Referer”:[“https://localhost/folder%202/”],“Accept-Encoding”:[“gzip, deflate, br”],“Upgrade-Insecure-Requests”:[“1”],“Accept”:[“text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9”],“Sec-Fetch-Site”:[“same-origin”],“Sec-Fetch-User”:["?1"],“Sec-Fetch-Dest”:[“document”],“Accept-Language”:[“en-US,en;q=0.9”]},“tls”:{“resumed”:false,“version”:772,“cipher_suite”:4865,“proto”:“h2”,“server_name”:“localhost”}},“user_id”:"",“duration”:0.0210946,“size”:0,“status”:200,“resp_headers”:{“Content-Type”:[“image/bmp”],“Last-Modified”:[“Fri, 08 Apr 2022 18:23:07 GMT”],“Accept-Ranges”:[“bytes”],“Content-Length”:[“0”],“Server”:[“Caddy”],“Etag”:["“ra19qj0"”]}}

5. What I already tried:

I tried using a build with the transform-encoder but didn’t get anywhere with that. Here was the config I tried with that:

log {
output file site.log
format formatted “{ts} {level} {request>uri}”

6. Links to relevant resources:

1 Like

The syntax is format transform, as noted in the README of the plugin. There’s examples in the README.

Thanks. I tried adding the module to my custom build, but I can’t figure out how to register it with caddy.

I placed the plugin in the module/logging/ directory. After I build it and use the format transform directive in the caddyfile, I get the following error when trying to start it up:

2022/04/19 23:13:47.212 INFO    using adjacent Caddyfile
run: adapting config using caddyfile: parsing caddyfile tokens for 'log': Caddyfile:14 - Error during parsing: getting module named 'caddy.logging.encoders.transform': module not registered: caddy.logging.encoders.transform
start: caddy process exited with error: exit status 1

Sorry for the noob question.

How exactly did you try to build it?

The right way to do it is using xcaddy which is a tool we provide to make it easy to make builds of Caddy with plugins, specifying the plugin’s module name as --with arguments, or you can download a build from the Download Caddy page with the plugin you want selected.

  • I downloaded the caddy source code
  • made some superficial changes to an html file
  • tried adding the plugin to the module/logging directory
  • then I execute go build from the cmd/caddy directory.

I’m not sure what you did in this step, exactly.

Either way, use xcaddy to build with plugins. It’s the recommended way.

Thanks for your help. I’m stupid new to Go and figured out I forgot to initialize the transform-encoder when building the un-recommended way.

I’m enjoying caddy, nice work!


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