Logging setup using json config

1. Output of caddy version:

v2.6.0 h1:lHDynvM+sTOi9Aq4Y15b4FtkqzPB36WbUrZvVdwzTCA=

2. How I run Caddy:

running directly in vm, no docker

a. System environment:

Amazon Linux 2

b. Command:

caddy adapt Caddyfile

c. Service/unit/compose file:

d. My complete Caddy config:

localhost {
        encode zstd gzip
        templates
        file_server browse
        log {
            output file /var/log/access.log {
                roll_size 1gb
                roll_keep 5
                roll_keep_for 720h
            }
         }

}

Trying to configure a server to log using the json configuration instead of caddyfile however the documentation for json configuration is not very helpful so I tried adapting a stock caddyfile to json and the resultant output isn’t well documented (see json output below).

The first thing that’s non obvious is why is there a “default” logs which excludes “http.log.access.log0”. Why and what does that do? Not seeing any documentation for that here JSON Config Structure - Caddy Documentation it simply references “http.log.access”

      "default": {
        "exclude": [
          "http.log.access.log0"
        ]
      },

The log0 setting goes and includes “http.log.access.log0”? Why is that? There is no documentation at JSON Config Structure - Caddy Documentation which would identify what the other possible options are beyond: “admin.api”

      "log0": {
        "writer": {
          "filename": "/var/log/access.log",
          "output": "file",
          "roll_keep": 5,
          "roll_keep_days": 30,
          "roll_size_mb": 954
        },
        "include": [
          "http.log.access.log0"
        ]
      }

Within the “apps” “http” directive there’s a “logs” which references a “logger_names” of “localhost”: “log0”. Is “localhost” always supposed to be the name of the host?

          "logs": {
            "logger_names": {
              "localhost": "log0"
            }
          }

This doesn’t seem to track because if you use a caddyfile where you replace localhost with :443 you get an entirely different “logs” section

       "logs": {
                  "default_logger_name": "log0"
                  }

4. Error messages and/or full log output:

{
  "logging": {
    "logs": {
      "default": {
        "exclude": [
          "http.log.access.log0"
        ]
      },
      "log0": {
        "writer": {
          "filename": "/var/log/access.log",
          "output": "file",
          "roll_keep": 5,
          "roll_keep_days": 30,
          "roll_size_mb": 954
        },
        "include": [
          "http.log.access.log0"
        ]
      }
    }
  },
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "localhost"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "encodings": {
                            "gzip": {},
                            "zstd": {}
                          },
                          "handler": "encode",
                          "prefer": [
                            "zstd",
                            "gzip"
                          ]
                        },
                        {
                          "handler": "templates"
                        },
                        {
                          "browse": {},
                          "handler": "file_server",
                          "hide": [
                            "./Caddyfile"
                          ]
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          "logs": {
            "logger_names": {
              "localhost": "log0"
            }
          }
        }
      }
    }
  }
}

5. What I already tried:

6. Links to relevant resources:

I really need to expand the “How Logging Works” page of Caddy’s documentation…

That said, let me try to answer some questions.

The answer to both of these is because logs for requests to localhost are assigned the name of log0. Since all access logs are under http.log.access, the resulting logger name is http.log.access.log0. The custom name is to ensure that the log directive you used for your site named localhost applies precisely to logs for that site, localhost – and none others.

Yes, or whatever the Host header of the request is. It tells Caddy which log configuration to use for which hostnames.

Does that help clear things up?

1 Like

Thanks for the reply and for caddy too. It still leaves open what should the setting be when there is no host name. If adapting a caddyfile in that case, the setting is

"logs": {
        "default_logger_name": "log0"
        }

would that work when a hostname is set in the listen directive? Also why does the “default” log exclude “http.log.access.log0”. Would it need to exclude any other logs setup (log1, log2, etc.)?

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