caddy icon indicating copy to clipboard operation
caddy copied to clipboard

[log_skip] Suppress logging for HTTP errors raised via `error` directive

Open bminer opened this issue 1 year ago • 3 comments

Opening issue per @francislavoie here: https://caddy.community/t/suppress-logging-for-http-errors-raised-via-error-directive/25959

Problem

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.

Proposed Solution

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 as described above, this does not work at the moment. Another option might be to suppress logging when using the error directive, for example:

error 404 {
  log_skip
}

Example Log Entry

{
  "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)"
}

Other Info

Caddy v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk= Installed on Ubuntu using apt package manager: APT-Sources: https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version/main amd64 Packages Ubuntu 22.04.5 LTS x86_64 Launched with systemd 249

Example Caddyfile

{
	# 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
}

bminer avatar Oct 14 '24 19:10 bminer

@francislavoie - Was just checking in to see if you had an opportunity to investigate this.

I must also commend everyone on the Caddy team for somehow keeping the percentage of open issues below 10% on a very popular open source project. Thanks for all of your great work!

bminer avatar Jan 07 '25 01:01 bminer

I really, really wonder if we "just" need a log ingestion module for Caddy. It could do all these post-processing steps we get requests for. Not sure it'd be built into the core distribution, but I dunno, seems like an elegant way to solve a bunch of problems/requests.

mholt avatar Jan 07 '25 04:01 mholt

Maybe, although my second suggestion of adding a log_skip flag when emitting errors might be sufficient and still fall under the "log emitter" category.

error 404 {
  log_skip
}

I'm obviously open to whatever you guys think is best.

bminer avatar Jan 14 '25 12:01 bminer