Skip to content

Commit 00f201e

Browse files
committed
Refactor error logging in defaultLogger.LogError() method
1 parent c4a9f84 commit 00f201e

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

logger/zaplogger_logfields.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ func (d *defaultLogger) LogRequestEnd(method string, url string, statusCode int,
3838
// LogError logs an error that occurs during the processing of an HTTP request if the current log level permits.
3939
func (d *defaultLogger) LogError(method string, url string, statusCode int, err error, stacktrace string) {
4040
if d.logLevel <= LogLevelError {
41+
errorMessage := ""
42+
if err != nil {
43+
errorMessage = err.Error()
44+
}
45+
4146
fields := []zap.Field{
4247
zap.String("event", "request_error"),
4348
zap.String("method", method),
4449
zap.String("url", url),
4550
zap.Int("status_code", statusCode),
46-
zap.String("error_message", err.Error()),
51+
zap.String("error_message", errorMessage),
4752
zap.String("stacktrace", stacktrace),
4853
}
4954
d.logger.Error("Error during HTTP request", fields...)

0 commit comments

Comments
 (0)