1

I am using caddy 2 as my proxy. and I try to set my caddy log format.

localhost:80 {
    reverse_proxy example:80
    log {
        output net logstash:5140
        format single_field common_log
    }
}

on version 1 there are predefined format https://caddyserver.com/v1/docs/log (from old version) common_log and combined

however this config below doesn't work. I assumed, it is deprecated, like it is stated in their documentation.

localhost:80 {
    reverse_proxy example:80
    log {
        output net logstash:5140
        format single_field combined_log
    }
}

But, I would like to keep using the old format for the log instead of the json format. then i tried multiple format, but it is not really documented in their website, also couldn't find in the community forum either.

then i tried multiple solutions. but, it is not working.

localhost:80 {
    reverse_proxy example:80
    log {
        output net logstash:5140
        format single_field "{remote} - {user} [{when}] \"{method} {uri} {proto}\" {status} {size} \"{>Referer}\" \"{>User-Agent}\""
    }
}

Can someone help me out? What I expected ist, the format look like this.

"127.0.0.1 - - [10/Apr/2020:14:10:12 +0000] \"localhost\" \"GET / HTTP/1.1\" 200 236 \"-\" \"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0\""

And common_log giving me output

"10.0.0.2 - - [11/Apr/2020:08:50:01 +0000] \"GET / HTTP/1.1\" 200 236\n"

custom input

format single_field "{remote} - {user} [{when}] \"{method} {uri} {proto}\" {status} {size} \"{>Referer}\" \"{>User-Agent}\""

"45.143.220.111 - - [11/Apr/2020:09:00:20 +0000] \"127.0.0.1\" \"GET /vtigercrm/vtigerservice.php HTTP/1.1\" 444 0 \"-\" \"libwww-perl/6.43\""
"45.143.220.111 - - [11/Apr/2020:09:00:20 +0000] \"_\" \"\\x16\\x03\\x01\\x02\\x00\\x01\\x00\\x01\\xFC\\x03\\x03\\x97s\\xD21\\x91\\xF6\\x88;\\x05\\x9C\\xFEs\\x99\\xB4\\x06\\xB6\\xC07Jd.aLC\\x9AR\\xE6\\x07\\x09\\x98\\xD6\\x1F\\x00\\x00\\xAC\\xC00\\xC0,\\xC0(\\xC0$\\xC0\\x14\\xC0\" 400 157 \"-\" \"-\""

2 Answers 2

3

As of 2020/06/20, you can't use custom log format from official distribution.
But, they have a module called format-encoder can do this.

So you just need to build caddy with format-encoder.
Then change the log format to formatted, and append a template string.
The template's content is reference from caddy's structured log.

For example, with below config:

log {
  format formatted "{ts} {level} {request>headers>User-Agent}"
}

The log output will look like this:

1585597114.7687502 info ["curl/7.64.1"]
0

the current (as of 8/2023) correct(?!) way to get apache common logs is to use:

https://github.com/caddyserver/transform-encoder

you'll first need to add transform-encoder either by recompiling caddy or for 2.4.4 and up you can just install the package:

caddy add-package github.com/caddyserver/transform-encoder

then, in each server directive in your Caddyfile"

www {
 <other config>
 log {
   output stdout  #your prefered output location
   format transform "{common_log}"
 }
}

you can also do this will the format fully spelled out:

common log format:

    log  {
       output stdout
       format transform `{request>remote_ip} - {request>user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size}` {
            time_format "02/Jan/2006:15:04:05 -0700"
       }
    }

or combined format (there is no shorthand for this):

    log  {
       output stdout
       format transform `{request>remote_ip} - {request>user_id} [{ts}] "{request>method} {request>uri} {request>proto}" {status} {size} "{request>headers>Referer>[0]}" "{request>headers>User-Agent>[0]}" "host:{request>host}"` {
          time_format "02/Jan/2006:15:04:05 -0700"
       }
    }

in my testing it is ALSO possible to do this on a global level, but it breaks (formats it poorly) the other logging (not access logs) and I dont know how to fix it.

as best I can tell this has to be done per server directive. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.