-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't print stack trace of error for certain logs to prevent spam (
#2904) * reduce stack trace spam by wrapping errors in custom type * differentiate logs * move custom error to log package * address feedback * add uts * address linter issues
- Loading branch information
Showing
4 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package log | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
type ErrorWithoutStackTrace struct { | ||
error | ||
} | ||
|
||
func (l *ErrorWithoutStackTrace) Error() string { | ||
if l.error == nil { | ||
return "" | ||
} | ||
return l.error.Error() | ||
} | ||
|
||
func (l *ErrorWithoutStackTrace) Format(s fmt.State, verb rune) { | ||
// if the error is nil, nothing should happen | ||
if l.error == nil { | ||
return | ||
} | ||
v := verb | ||
// replace uses of %v with %s | ||
if v == 'v' { | ||
v = 's' | ||
} | ||
// if the error implements formatter (which it should) | ||
var formatter fmt.Formatter | ||
if errors.As(l.error, &formatter) { | ||
formatter.Format(s, v) | ||
} else { | ||
_, _ = io.WriteString(s, l.error.Error()) | ||
} | ||
} | ||
|
||
func (l *ErrorWithoutStackTrace) Unwrap() error { | ||
return l.error | ||
} | ||
|
||
func NewErrorWithoutStackTrace(err error) *ErrorWithoutStackTrace { | ||
return &ErrorWithoutStackTrace{err} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var errInternal = errors.New("internal error") | ||
|
||
func TestLoggerError(t *testing.T) { | ||
require := require.New(t) //nolint:gocritic | ||
|
||
var buf bytes.Buffer | ||
|
||
// Create a zap core that writes logs to the buffer | ||
core := zapcore.NewCore( | ||
zapcore.NewJSONEncoder(zapcore.EncoderConfig{}), | ||
zapcore.AddSync(&buf), | ||
zapcore.DebugLevel, | ||
) | ||
logger := zap.New(core) | ||
|
||
wrappedError := errors.Wrap(errInternal, "wrapped message") | ||
errorNoStack := NewErrorWithoutStackTrace(wrappedError) | ||
|
||
logger.Info("Error", zap.Error(wrappedError)) | ||
require.Contains(buf.String(), "errorVerbose") | ||
fmt.Println(buf.String()) | ||
buf.Reset() | ||
|
||
// Error verbose field should be omitted from the error without stack trace error | ||
logger.Info("ErrorWithoutStackTrace", zap.Error(errorNoStack)) | ||
require.NotContains(buf.String(), "errorVerbose") | ||
require.Contains(buf.String(), "wrapped message") | ||
require.Contains(buf.String(), "internal error") | ||
fmt.Println(buf.String()) | ||
buf.Reset() | ||
|
||
// Even if the embedded error is nil, the error should still display an empty string and not panic | ||
logger.Info("ErrorWithoutStackTrace nil internal error", zap.Error(NewErrorWithoutStackTrace(nil))) | ||
require.Contains(buf.String(), "\"error\":\"\"") | ||
fmt.Println(buf.String()) | ||
buf.Reset() | ||
|
||
// should be able to unwrap the error without a stack trace | ||
require.ErrorIs(errorNoStack, errInternal) | ||
// Even if the embedded error is nil, should function properly | ||
require.NotErrorIs(&ErrorWithoutStackTrace{error: nil}, errorNoStack) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters