zap logger updates

This commit is contained in:
Pavel 2024-01-19 13:17:04 +03:00
parent 96ac6a6940
commit 194e9c40e9
6 changed files with 44 additions and 11 deletions

View File

@ -95,7 +95,15 @@ func (n *callbackLogger) WithGroup(name string) logger.Logger {
return n return n
} }
func (n *callbackLogger) ForAccount(handler, conn, acc any) logger.Logger { func (n *callbackLogger) ForHandler(handler any) logger.Logger {
return n
}
func (n *callbackLogger) ForConnection(conn any) logger.Logger {
return n
}
func (n *callbackLogger) ForAccount(acc any) logger.Logger {
return n return n
} }

View File

@ -20,7 +20,9 @@ type Logger interface {
DPanic(msg string, fields ...zap.Field) DPanic(msg string, fields ...zap.Field)
Panic(msg string, fields ...zap.Field) Panic(msg string, fields ...zap.Field)
Fatal(msg string, fields ...zap.Field) Fatal(msg string, fields ...zap.Field)
ForAccount(handler, conn, acc any) Logger ForHandler(handler any) Logger
ForConnection(conn any) Logger
ForAccount(acc any) Logger
Sync() error Sync() error
} }
@ -42,8 +44,16 @@ func (l *Default) WithLazy(fields ...zap.Field) Logger {
return l.WithLazy(fields...).(Logger) return l.WithLazy(fields...).(Logger)
} }
func (l *Default) ForAccount(handler, conn, acc any) Logger { func (l *Default) ForHandler(handler any) Logger {
return l.WithLazy(zap.Any(HandlerAttr, handler), zap.Any(ConnectionAttr, conn), zap.Any(AccountAttr, acc)) return l.WithLazy(zap.Any(HandlerAttr, handler))
}
func (l *Default) ForConnection(conn any) Logger {
return l.WithLazy(zap.Any(ConnectionAttr, conn))
}
func (l *Default) ForAccount(acc any) Logger {
return l.WithLazy(zap.Any(AccountAttr, acc))
} }
func AnyZapFields(args []interface{}) []zap.Field { func AnyZapFields(args []interface{}) []zap.Field {

View File

@ -43,7 +43,15 @@ func (l *Nil) Panic(msg string, fields ...zap.Field) {}
func (l *Nil) Fatal(msg string, fields ...zap.Field) {} func (l *Nil) Fatal(msg string, fields ...zap.Field) {}
func (l *Nil) ForAccount(handler, conn, acc any) Logger { func (l *Nil) ForHandler(handler any) Logger {
return l
}
func (l *Nil) ForConnection(conn any) Logger {
return l
}
func (l *Nil) ForAccount(acc any) Logger {
return l return l
} }

View File

@ -150,7 +150,7 @@ func (s *Sentry) obtainErrorLogger(c *gin.Context) logger.Logger {
connectionID := "{no connection ID}" connectionID := "{no connection ID}"
accountID := "{no account ID}" accountID := "{no account ID}"
if s.SentryLoggerConfig.TagForConnection == "" && s.SentryLoggerConfig.TagForAccount == "" { if s.SentryLoggerConfig.TagForConnection == "" && s.SentryLoggerConfig.TagForAccount == "" {
return s.Logger.ForAccount("Sentry", connectionID, accountID) return s.Logger.ForHandler("Sentry").ForConnection(connectionID).ForAccount(accountID)
} }
for tag := range s.tagsFromContext(c) { for tag := range s.tagsFromContext(c) {
@ -162,7 +162,7 @@ func (s *Sentry) obtainErrorLogger(c *gin.Context) logger.Logger {
} }
} }
return s.Logger.ForAccount("Sentry", connectionID, accountID) return s.Logger.ForHandler("Sentry").ForConnection(connectionID).ForAccount(accountID)
} }
// tagsSetterMiddleware sets event tags into Sentry events. // tagsSetterMiddleware sets event tags into Sentry events.

View File

@ -277,7 +277,7 @@ func (s *SentryTest) TestSentry_CaptureException() {
func (s *SentryTest) TestSentry_obtainErrorLogger_Existing() { func (s *SentryTest) TestSentry_obtainErrorLogger_Existing() {
ctx, _ := s.ginCtxMock() ctx, _ := s.ginCtxMock()
log := testutil.NewBufferedLogger().ForAccount("component", "conn", "acc") log := testutil.NewBufferedLogger().ForHandler("component").ForConnection("conn").ForAccount("acc")
ctx.Set("logger", log) ctx.Set("logger", log)
s.Assert().Equal(log, s.sentry.obtainErrorLogger(ctx)) s.Assert().Equal(log, s.sentry.obtainErrorLogger(ctx))

View File

@ -57,9 +57,16 @@ func (l *BufferLogger) WithLazy(fields ...zapcore.Field) logger.Logger {
} }
} }
func (l *BufferLogger) ForAccount(handler, conn, acc any) logger.Logger { func (l *BufferLogger) ForHandler(handler any) logger.Logger {
return l.WithLazy( return l.WithLazy(zap.Any(logger.HandlerAttr, handler))
zap.Any(logger.HandlerAttr, handler), zap.Any(logger.ConnectionAttr, conn), zap.Any(logger.AccountAttr, acc)) }
func (l *BufferLogger) ForConnection(conn any) logger.Logger {
return l.WithLazy(zap.Any(logger.ConnectionAttr, conn))
}
func (l *BufferLogger) ForAccount(acc any) logger.Logger {
return l.WithLazy(zap.Any(logger.AccountAttr, acc))
} }
// Read bytes from the logger buffer. io.Reader implementation. // Read bytes from the logger buffer. io.Reader implementation.