Since https://github.com/elastic/elasticsearch/pull/55774 we log exceptions reaching the RestResponse class on their way back to the client in the "rest.suppressed" logger. Status codes <500 are logged on the Debug and >=500 codes are logged on the Warn level.
However there is some logic that doesn't log these errors if the user has requested "error_trace" in the request parameters.
In this case we don't log. To improve visibility of 500s on the server side logs and to be able to measure and alert on occasions of this status code we should remove this condition and always log 500s (and probably 400s on Debug like already done today).
server/src/test/java/org/elasticsearch/rest/RestResponseTests.java
new ElasticsearchSecurityException("failed authn", RestStatus.UNAUTHORIZED, new ElasticsearchException("cause"))
        )) {
            for (RestRequest request : List.of(
                new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(),
                new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(Map.of("error_trace", Boolean.toString(true))).build()
            )) {
                for (RestChannel channel : List.of(new SimpleExceptionRestChannel(request), new DetailedExceptionRestChannel(request))) {
                    RestResponse response = new RestResponse(channel, authnException);
                    assertThat(response.status(), is(RestStatus.UNAUTHORIZED));
                    assertThat(response.content().utf8ToString(), not(containsString(ElasticsearchException.STACK_TRACE)));
                }
            }
        }
    }
    public void testStackTrace() throws IOException {
        for (Exception exception : List.of(new ElasticsearchException("dummy"), new IllegalArgumentException("dummy"))) {
            for (RestRequest request : List.of(
                new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build(),
                new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(Map.of("error_trace", Boolean.toString(true))).build()
            )) {
                for (RestChannel channel : List.of(new SimpleExceptionRestChannel(request), new DetailedExceptionRestChannel(request))) {
                    RestResponse response = new RestResponse(channel, exception);
                    if (exception instanceof ElasticsearchException) {
                        assertThat(response.status(), is(RestStatus.INTERNAL_SERVER_ERROR));
                    } else {
                        assertThat(response.status(), is(RestStatus.BAD_REQUEST));
                    }
                    boolean traceExists = request.paramAsBoolean("error_trace", false) && channel.detailedErrorsEnabled();
                    if (traceExists) {
                        assertThat(response.content().utf8ToString(), containsString(ElasticsearchException.STACK_TRACE));