caddyserver / caddy

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
https://caddyserver.com
Apache License 2.0
55.72k stars 3.92k forks source link

v2: log not writing to file #3185

Closed wille-io closed 4 years ago

wille-io commented 4 years ago

I hope I just don't get my head wrapped around v2 or the documentation is incomplete, but: Telling caddy to write logs to a file does not create a file but logs to stdout.

I used caddy2_beta19_linux_amd64 from the releases page.

Caddyfile:

:8000 {
  root .
  log {
    output file /home/wille/test.log
  }
}

Started with: caddy2_beta19_linux_amd64 run

The resulting json, curled from /config/:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":8000"
          ],
          "logs": {},
          "routes": [
            {
              "handle": [
                {
                  "handler": "vars",
                  "root": "."
                }
              ]
            }
          ]
        }
      }
    }
  },
  "logging": {
    "logs": {
      "log0": {
        "include": [
          "http.log.access.log0"
        ],
        "writer": {
          "filename": "/home/wille/test.log",
          "output": "file"
        }
      }
    }
  }
}

Visiting 127.0.0.1:8000 generates a log entry on the console:

2020/03/24 10:19:47.236 INFO    http.log.access handled request {"request": {"method": "GET", "uri": "/", "proto": "HTTP/1.1", "remote_addr": "127.0.0.1:56772", "host": "127.0.0.1:8000", "headers": {"Upgrade-Insecure-Requests": ["1"], "User-Agent": ["Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-Mode": ["navigate"], "Accept-Encoding": ["gzip, deflate, br"], "Connection": ["keep-alive"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Accept-Language": ["de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7"], "If-None-Match": ["\"11cf90bd0f803f7ad36230bcab3bf14a\""], "If-Modified-Since": ["Sun, 15 Mar 2020 20:36:50 GMT"]}}, "common_log": "127.0.0.1 - - [24/Mar/2020:11:19:47 +0100] \"GET / HTTP/1.1\" 0 0", "latency": 0.000013899, "size": 0, "status": 0, "resp_headers": {"Server": ["Caddy"]}}

But: tail -f /home/wille/test.log tail: cannot open '/home/wille/test.log' for reading: No such file or directory

Did I miss something?

mholt commented 4 years ago

Oh... yeah, this is because the site block doesn't have a hostname, only a port. So it can't map any particular host to a particular log. Let me see what can be done about this. Thanks for the report.

mholt commented 4 years ago

Okie, I changed it so that it will register a logger name mapping for "" in the case of no hostname, as a kind of "default" log for that server. Should work now. I'm gonna tag beta 20.