Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bminer opened this issue Oct 14, 2024 · 0 comments
Open

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

bminer opened this issue Oct 14, 2024 · 0 comments

Comments

@bminer
Copy link

bminer commented Oct 14, 2024

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant