// Copyright 2015 Matthew Holt and The Caddy Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package caddyhttp import ( "testing" "go.uber.org/zap" "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" ) // TestServerErrorLoggerLevels verifies that recovered net/http handler panics // written to http.Server.ErrorLog surface at ERROR level (so they're visible // at the default log level), while other standard library server messages stay // at DEBUG. Regression test for #7923. func TestServerErrorLoggerLevels(t *testing.T) { for _, tc := range []struct { name string message string wantLevel zapcore.Level }{ { name: "recovered handler panic logs at error", message: "http: panic serving 127.0.0.1:12345: boom\ngoroutine 1 [running]:\nmain.handler()", wantLevel: zapcore.ErrorLevel, }, { name: "other server message logs at debug", message: "http: TLS handshake error from 127.0.0.1:12345: EOF", wantLevel: zapcore.DebugLevel, }, } { t.Run(tc.name, func(t *testing.T) { core, logs := observer.New(zapcore.DebugLevel) serverLogger := serverErrorLogger(zap.New(core)) serverLogger.Print(tc.message) entries := logs.All() if len(entries) != 1 { t.Fatalf("expected exactly 1 log entry, got %d", len(entries)) } got := entries[0] if got.Level != tc.wantLevel { t.Errorf("expected level %s, got %s", tc.wantLevel, got.Level) } if got.Message != tc.message { t.Errorf("expected message %q, got %q", tc.message, got.Message) } }) } }