How to hide JSON in logs?

1. Caddy version (caddy version):

v2.4.1 h1:kAJ0JB5Xk5gPdTH/27S5cyoMGqD5lBAe9yZ8zTjVJa0=

2. How I run Caddy:

I’m just running a simple file server with the command and Caddyfile below.

a. System environment:

Windows 10 x64

b. Command:

caddy run -watch

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

localhost

root * content
file_server
templates

3. The problem I’m having:

I’m running a very simple file server with Caddy but what bothers me is the structured JSON logging data in log output. Each line has some JSON at the end and some lines, like HTML template errors have very big blocks of it that make everything kind of unreadable.

This looks especially bad with line wrapping as is the default behavior on many terminals.

I would like to just disable printing of full JSON data at the end of each log message.

4. Error messages and/or full log output:

Here’s an example of what I mean. These are just the default messages that are printed when running and terminating Caddy, along with an HTML template error.

If you look at this with line wrapping on it will be particularly hard to read.

2021/06/02 23:24:38.681 INFO    using adjacent Caddyfile
2021/06/02 23:24:38.682 WARN    input is not formatted with 'caddy fmt' {"adapter": "caddyfile", "file": "Caddyfile", "line": 5}
2021/06/02 23:24:38.689 INFO    admin   admin endpoint started  {"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", "127.0.0.1:2019"]}
2021/06/02 23:24:38.689 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc0000a1650"}
2021/06/02 23:24:38.689 INFO    http    server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2021/06/02 23:24:38.690 INFO    http    enabling automatic HTTP->HTTPS redirects        {"server_name": "srv0"}
2021/06/02 23:24:38.702 INFO    tls     cleaning storage unit   {"description": "FileStorage:C:\\Users\\jeffr\\AppData\\Roaming\\Caddy"}
2021/06/02 23:24:38.705 INFO    tls     finished cleaning storage units
2021/06/02 23:24:38.729 INFO    pki.ca.local    root certificate is already trusted by system   {"path": "storage:pki/authorities/local/root.crt"}
2021/06/02 23:24:38.731 INFO    http    enabling automatic TLS certificate management   {"domains": ["localhost"]}
2021/06/02 23:24:38.734 WARN    tls     stapling OCSP   {"error": "no OCSP stapling for [localhost]: no OCSP server specified in certificate"}
2021/06/02 23:24:38.735 INFO    autosaved config (load with --resume flag)      {"file": "C:\\Users\\jeffr\\AppData\\Roaming\\Caddy\\autosave.json"}
2021/06/02 23:24:38.735 INFO    serving initial configuration
2021/06/02 23:24:40.853 ERROR   http.log.error  template: /layout.html:12: function "templateError" not defined {"request": {"remote_addr": "[::1]:2029", "proto": "HTTP/2.0", "method": "GET", "host": "localhost", "uri": "/layout.html", "headers": {"Cookie": ["session-id=s%3AKEWFiVG5t7QmRngIKCse2cQrhkGsd022.nvtq1ZXx8tC8H3efQxsP9fEe1gHn0XkgoLCZzZgnttM"], "Sec-Ch-Ua-Mobile": ["?0"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Accept-Encoding": ["gzip, deflate, br"], "Sec-Ch-Ua": ["\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\""], "Upgrade-Insecure-Requests": ["1"], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "Accept-Language": ["en-US,en;q=0.9,fr;q=0.8"], "Dnt": ["1"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"], "Cache-Control": ["max-age=0"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "localhost"}}, "duration": 0.1319475, "status": 500, "err_id": "78jiqpg0p", "err_trace": "templates.(*Templates).executeTemplate (templates.go:315)"}
2021/06/02 23:24:42.329 INFO    shutting down   {"signal": "SIGINT"}
2021/06/02 23:24:42.329 WARN    exiting; byeee!! 👋     {"signal": "SIGINT"}
2021/06/02 23:24:42.355 INFO    tls.cache.maintenance   stopped background certificate maintenance      {"cache": "0xc0000a1650"}
2021/06/02 23:24:42.370 INFO    admin   stopped previous server {"address": "tcp/localhost:2019"}
2021/06/02 23:24:42.370 INFO    shutdown complete       {"signal": "SIGINT", "exit_code": 0}

5. What I already tried:

format console says it’s supposed to be human readable, but it still appends JSON which is not what I want.

I tried format single_field common_log but it looks really old fashioned and clunky, and it doesn’t affect stuff like HTTP template errors and the stuff that prints on startup. What I want is simple human readable output, like v1 had, and the default formatter is so close to that except for the JSON dumping.

Logging doesn’t work that way. The important information is found in the JSON.

I understand that it’s very relevant, but that HTML template error, for example, contains the entire request object including all the headers, cookies, the common_log line, etc., just a lot of not very relevant information that makes it harder to find the actual template syntax error.

Visual Studio Code doesn’t currently allow disabling line wrap in the integrated terminal, so it’s impossible to decipher Caddy’s logs in it. This screenshot is just 5 log lines:

The messages here contain all the information I actually need, and without the JSON the logs would be much more parsable:

I still want to log JSON stuff to a file for detailed debugging, but keep the terminal output just short messages and timestamps and stuff as shown above. Is that not possible?

That’s being removed in a future version FYI

Just use grep and/or jq to parse the logs and omit stuff you don’t want.

Ah, that’ll have to do. Thanks anyway.

1 Like

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