diff --git a/core/job_manager_test.go b/core/job_manager_test.go index 6de4fc5..ae3fd82 100644 --- a/core/job_manager_test.go +++ b/core/job_manager_test.go @@ -95,7 +95,15 @@ func (n *callbackLogger) WithGroup(name string) logger.Logger { 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 } diff --git a/core/logger/default.go b/core/logger/default.go index dc92980..62f866d 100644 --- a/core/logger/default.go +++ b/core/logger/default.go @@ -20,7 +20,9 @@ type Logger interface { DPanic(msg string, fields ...zap.Field) Panic(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 } @@ -42,8 +44,16 @@ func (l *Default) WithLazy(fields ...zap.Field) Logger { return l.WithLazy(fields...).(Logger) } -func (l *Default) ForAccount(handler, conn, acc any) Logger { - return l.WithLazy(zap.Any(HandlerAttr, handler), zap.Any(ConnectionAttr, conn), zap.Any(AccountAttr, acc)) +func (l *Default) ForHandler(handler any) Logger { + 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 { diff --git a/core/logger/nil.go b/core/logger/nil.go index 74d0257..282a13d 100644 --- a/core/logger/nil.go +++ b/core/logger/nil.go @@ -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) 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 } diff --git a/core/sentry.go b/core/sentry.go index 27b2057..2e3330c 100644 --- a/core/sentry.go +++ b/core/sentry.go @@ -150,7 +150,7 @@ func (s *Sentry) obtainErrorLogger(c *gin.Context) logger.Logger { connectionID := "{no connection ID}" accountID := "{no account ID}" 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) { @@ -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. diff --git a/core/sentry_test.go b/core/sentry_test.go index fe574f2..b919d86 100644 --- a/core/sentry_test.go +++ b/core/sentry_test.go @@ -277,7 +277,7 @@ func (s *SentryTest) TestSentry_CaptureException() { func (s *SentryTest) TestSentry_obtainErrorLogger_Existing() { ctx, _ := s.ginCtxMock() - log := testutil.NewBufferedLogger().ForAccount("component", "conn", "acc") + log := testutil.NewBufferedLogger().ForHandler("component").ForConnection("conn").ForAccount("acc") ctx.Set("logger", log) s.Assert().Equal(log, s.sentry.obtainErrorLogger(ctx)) diff --git a/core/util/testutil/buffer_logger.go b/core/util/testutil/buffer_logger.go index a7d0723..08e887b 100644 --- a/core/util/testutil/buffer_logger.go +++ b/core/util/testutil/buffer_logger.go @@ -57,9 +57,16 @@ func (l *BufferLogger) WithLazy(fields ...zapcore.Field) logger.Logger { } } -func (l *BufferLogger) ForAccount(handler, conn, acc any) logger.Logger { - return l.WithLazy( - zap.Any(logger.HandlerAttr, handler), zap.Any(logger.ConnectionAttr, conn), zap.Any(logger.AccountAttr, acc)) +func (l *BufferLogger) ForHandler(handler any) logger.Logger { + return l.WithLazy(zap.Any(logger.HandlerAttr, handler)) +} + +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.