Suppress logging for HTTP errors raised via `error` directive

1. The problem I’m having:

Caddy always emits a log to http.log.error when the error directive is used. I would like to ignore specific errors like error 404 and error 403, for example. It appears that log_skip and log_name only work for access logs (i.e. http.log.access), not for error logs (i.e. http.log.error). To be clear, I still want DEBUG level logs for http.log.error (because things like bad Go templates emit DEBUG level errors), but I don’t want 404 errors.

Ideally, I’d like to add something to my error_handler like:

@noLog expression `{err.status_code} in [301, 302, 307, 308, 403, 404]`
log_skip @noLog

…but this does not work at the moment. Another option is to suppress logging when using the error directive, for example:

error 404 {
  log_skip
}

2. Error messages and/or full log output:

{
  "level": "debug",
  "ts": "2024-10-11T18:15:00.093Z",
  "logger": "http.log.error",
  "msg": "",
  "request": {
    "remote_ip": "REDACTED",
    "remote_port": "REDACTED",
    "client_ip": "REDACTED",
    "proto": "HTTP/2.0",
    "method": "POST",
    "host": "example.blakeminer.com",
    "uri": "/404",
    "headers": {
      "User-Agent": [
        "curl/7.81.0"
      ],
      "Accept": [
        "*/*"
      ],
      "Content-Type": [
        "application/json"
      ],
      "Content-Length": [
        "2"
      ]
    },
    "tls": {
      "resumed": false,
      "version": 772,
      "cipher_suite": 4865,
      "proto": "h2",
      "server_name": "example.blakeminer.com"
    }
  },
  "duration": 9.6901e-5,
  "status": 404,
  "err_id": "REDACTED",
  "err_trace": "caddyhttp.StaticError.ServeHTTP (staticerror.go:109)"
}

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

Installed on Ubuntu using apt package manager.
APT-Sources: https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version/main amd64 Packages

a. System environment:

Ubuntu 22.04.5 LTS
x86_64
Launched with systemd 249

d. My complete Caddy config:

{
	# grace period for clients on server reload
	grace_period 60s
	log default {
		output file /var/log/caddy/default.log {
			roll_size 10MiB
			roll_keep 5
			roll_keep_for 30d
		}
		format json {
			time_format iso8601
		}
		exclude http.log.access http.log.error
	}
	log error {
		output file /var/log/caddy/errors.log {
			roll_size 10MiB
			roll_keep 2
			roll_keep_for 30d
		}
		format json {
			time_format iso8601
		}
		level DEBUG
		include http.log.error
	}
}

(hsts) {
	@enabled not vars noHSTS true
	# Default age is 180 days
	header @enabled Strict-Transport-Security "max-age=15552000"
}

(main_site) {
	import hsts
	encode zstd gzip
	handle_errors {
		# Just respond with 404.html or error.html
		root * /var/www/default
		import no_cache
		try_files /{host}-{http.error.status_code}.html /{http.error.status_code}.html /error.html
		templates
		file_server

		# For redirects, just redirect
		@redir {
			expression `{err.status_code} in [301, 302, 307, 308]`
			expression `{http.response.header.Location} != ""`
		}
		respond @redir ""

		# For JSON or non-GET requests, respond with JSON error message
		@json not {
			not header_regexp Content-Type \/json$
			method GET
		}
		route @json {
			header Content-Type application/json
			respond `\{"message":"{http.error.status_text}","status":{http.error.status_code}\}`
		}
	}
}

example.blakeminer.com {
	import main_site
	log_skip
	error 404
}

Interesting, didn’t think of that when I implemented log_skip. Can you open a Github issue for this? I’ll need to dig into it soon.

2 Likes

Added issue here: [log_skip] Suppress logging for HTTP errors raised via `error` directive · Issue #6625 · caddyserver/caddy · GitHub

1 Like

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