Logging doesn't work in 2.0 beta 13?

1. My Caddy version (caddy -version):

v2.0.0-beta.13 h1:QL0JAepFvLVtOatABqniuDRQ4HmtvWuuSWZW24qVVtk=

2. How I run Caddy:

Binary downloaded from https://github.com/caddyserver/caddy/releases/download/v2.0.0-beta.13/caddy2_beta13_linux_amd64
Running on arch linux with systemd

a. System environment:

Arch linux

b. Command:

(systemd)

c. Service/unit/compose file:

[Unit]
Description=Caddy2 server

[Service]
User=caddy
WorkingDirectory=/var/lib/caddy
ExecStart=/usr/local/bin/caddy start -config /etc/caddy/Caddyfile
Type=forking

d. My complete Caddyfile:

{
    http_port 8080
    https_port 8443
}

mydomain.foo.bar {
    log /var/log/caddy/log
    errors /var/log/caddy/errors

    reverse_proxy {
        to 10.0.0.1:80
    }
}

3. The problem I’m having:

It doesn’t start, complaining about “log” being unrecognized directive

4. Error messages and/or full log output:

Jan 25 22:09:02 proxy caddy[15444]: 2020/01/25 22:09:02.575        INFO        using provided configuration        {"config_file": "/etc/caddy/Caddyfile", "config_adapter": ""}
Jan 25 22:09:02 proxy caddy[15444]: run: adapting config using caddyfile: /etc/caddy/Caddyfile:7: unrecognized directive: log
Jan 25 22:09:02 proxy caddy[15440]: start: caddy process exited with error: exit status 1

5. What I already tried:

it works properly after removing the “log” and “errors” lines. But I would like to have logs :slight_smile:
I tried to move the “log” and “errors” into the global section and also into the domain section (out of the “reverse_proxy” context), neither of them helped.

Is this expected to work? Did logging changed (and docs/examples not updated?)

Thanks.

The documentation for Caddy v2’s Caddyfile is found at Caddyfile Directives — Caddy Documentation

Caddy v1 and Caddy v2 configurations are not compatible, there have been many significant changes to the internal functionality of the server that required breaking changes.

Unfortunately, because it’s still in beta, Caddyfile directives for logging haven’t been implemented yet. It will probably be added in the next few releases.

For now, I if you need logs, you’ll need to use JSON config (you can dump your existing config with the caddy adapt command as a starting point). All you have to do is set the “logs” field of the server: JSON Config Structure - Caddy Documentation. For default access logs, just set it to an empty object ( {} ).

2 Likes

Any update on how I can enable logging and write to a specific file as of 2.0 beta 13? The updated documentation doesn’t show much as it relates to logging.

You need to set the appropriate values inside the logging key in the JSON config. You’ll have something like this in your config:

{

	"logging": {
		"sink": {
			"writer": {
				"output": "file",
				"filename": "path-to-your-log-file-with-name-for-unstructured-logs",
				"roll": false,
				"roll_size_mb": 0,
				"roll_gzip": false,
				"roll_local_time": false,
				"roll_keep": 0,
				"roll_keep_days": 0
			}
		},
		"logs": {
			"default": {
				"writer": {
					"output": "file",
					"filename": "path-to-your-log-file-with-name-for-structured-logs",
					"roll": false,
					"roll_size_mb": 0,
					"roll_gzip": false,
					"roll_local_time": false,
					"roll_keep": 0,
					"roll_keep_days": 0
				}
			}
		}
	},
	"apps": {}
}

Check the details in the logging config docs: JSON Config Structure - Caddy Documentation

1 Like

Thanks Mohammed90. I’ve got it working with the example you posted above. Does this logging key also enable me to get access logs or do I need to use the “logs” key under apps/http/servers? In Caddy 1 I was able to achieve this by simply using the “log” directive.

To write only access logs, you’d set an include rule for only http.log.access. See logs/include on JSON Config Structure - Caddy Documentation and the description of logs/exclude which mentions http.log.access.

Documentation will definitely be improved in this area over time.

1 Like

Thank you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.