How to change caddy itself's log output format while I using caddyfile with systemd

1. Caddy version (caddy version):

v2.3.0 h1:fnrqJLa3G5vfxcxmOH/+kJOcunPLhSBnjgIvjXV/QTA=

2. How I run Caddy:

a. System environment:

Debian 10, Linux 4.19.0-14-amd64 #1 SMP Debian 4.19.171-2 (2021-01-30) x86_64 GNU/Linux

b. Command:

Just an officially systemd service file

If command on terminal will be OK, it outputs console format as I wish.

c. Service/unit/compose file:

# /lib/systemd/system/caddy.service
# caddy.service
#
# For using Caddy with a config file.
#
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
#
# See https://caddyserver.com/docs/install for instructions.
#
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
User=caddy
Group=srv
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE
; Environment=XDG_DATA_HOME=/var
; Environment=XDG_CONFIG_HOME=/var

[Install]
WantedBy=multi-user.target


d. My complete Caddyfile or JSON config:


None about config file, I think. Just question for how to change caddy itselfā€™s output log format


3. The problem Iā€™m having:

Cannot set caddy itselfā€™s log format.

4. Error messages and/or full log output:

Current journalctl caddy.service log look like this, JSON format, human unfriendly:

Apr 19 02:44:23 srv caddy[32659]: {"level":"info","ts":1618771463.3738742,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":""}
Apr 19 02:44:23 srv caddy[32659]: {"level":"info","ts":1618771463.384001,"logger":"admin","msg":"admin endpoint started","address":"tcp/localhost:2019","enforce_origin":false,"origins":["127.0.0.1:2019","localhost:2019","[::1]:2019"]}
Apr 19 02:44:23 srv caddy[32659]: {"level":"info","ts":1618771463.3851924,"logger":"http","msg":"enabling automatic HTTP->HTTPS redirects","server_name":"srv0"}
Apr 19 02:44:23 srv caddy[32659]: {"level":"info","ts":1618771463.3854547,"logger":"http","msg":"enabling automatic HTTP->HTTPS redirects","server_name":"srv1"}
Apr 19 02:44:23 srv caddy[32659]: {"level":"info","ts":1618771463.3865013,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0xc000306000"}

5. What I already tried:

I have been read all reletave documents but looks only JSON file define log format, but I use caddyfile.

6. Links to relevant resources:

Itā€™s not currently possible in Caddy v2.3.0, but it will be possible in v2.4.0 (available since beta 2) via global options. It was added in this PR:

But you should be aware that Caddy emits structured logs, which donā€™t really have a way to be ā€œhuman readableā€, really. You generally should be using other tooling to format the logs how you like, for example jq which is a CLI tool for manipulating JSON inputs.

Thanks, my ā€œhuman readableā€ means just not like JSON add extra no-use information, just like general console log is enough, I only check it when I change my configuration. Anyway, thanks for your help!

1 Like

Well, JSON is pretty human readable. Just look for "msg":

But like I said, with v2.4.0-beta.2, you can configure global options to use the console writer which you might prefer.

The docs for that were added in this PR, but wonā€™t be live until v2.4.0 stable is out (but it already is pretty stable, we just havenā€™t merged in all the features and improvements we wanted to add yet)

Sounds good, the official siteā€™s current version is 2.4.0-beta1 and looks add DNS challenge plugin only available from site or compile by myself, but I donā€™t know how to compile, maybe I need to wait some days.

Itā€™s very easy to build Caddy. You just need to install Go (just follow the official instructions on the Go site, itā€™s simple), then install xcaddy (just a static binary, like Caddy), then run a single command to build Caddy with plugins.

1 Like

I will try, thanks for your help!

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