Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/echo/tcp-echo/client/app/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (p *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
return 0, err
}
if p.H.Magic != echoPkgMagic {
log.Error("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
_ = log.Error("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
return 0, ErrIllegalMagic
}
if buf.Len() < (int)(p.H.Len) {
Expand Down
4 changes: 2 additions & 2 deletions examples/echo/tcp-echo/client/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (h *EchoMessageHandler) OnClose(session getty.Session) {
func (h *EchoMessageHandler) OnMessage(session getty.Session, pkg any) {
p, ok := pkg.(*EchoPackage)
if !ok {
log.Error("illegal packge{%#v}", pkg)
_ = log.Error("illegal packge{%#v}", pkg)
return
}

Expand All @@ -77,7 +77,7 @@ func (h *EchoMessageHandler) OnMessage(session getty.Session, pkg any) {
func (h *EchoMessageHandler) OnCron(session getty.Session) {
clientEchoSession, err := client.getClientEchoSession(session)
if err != nil {
log.Error("client.getClientSession(session{%s}) = error{%#v}", session.Stat(), err)
_ = log.Error("client.getClientSession(session{%s}) = error{%#v}", session.Stat(), err)
return
}
if conf.sessionTimeout.Nanoseconds() < time.Since(session.GetActive()).Nanoseconds() {
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/tcp-echo/client/app/readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *EchoPackageHandler) Write(ss getty.Session, pkg any) ([]byte, error) {

startTime = time.Now()
if echoPkg, ok = pkg.(*EchoPackage); !ok {
log.Error("illegal pkg:%+v\n", pkg)
_ = log.Error("illegal pkg:%+v\n", pkg)
return nil, errors.New("invalid echo package")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/echo/tcp-echo/server/app/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (p *EchoPackage) Unmarshal(buf *bytes.Buffer) (int, error) {
return 0, err
}
if p.H.Magic != echoPkgMagic {
log.Error("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
_ = log.Error("@p.H.Magic{%x}, right magic{%x}", p.H.Magic, echoPkgMagic)
return 0, ErrIllegalMagic
}
if buf.Len() < (int)(p.H.Len) {
Expand Down
4 changes: 2 additions & 2 deletions examples/echo/tcp-echo/server/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ func (h *EchoMessageHandler) OnClose(session getty.Session) {
func (h *EchoMessageHandler) OnMessage(session getty.Session, pkg any) {
p, ok := pkg.(*EchoPackage)
if !ok {
log.Error("illegal packge{%#v}", pkg)
_ = log.Error("illegal packge{%#v}", pkg)
return
}

handler, ok := h.handlers[p.H.Command]
if !ok {
log.Error("illegal command{%d}", p.H.Command)
_ = log.Error("illegal command{%d}", p.H.Command)
return
}
err := handler.Handle(session, p)
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/tcp-echo/server/app/readwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (h *EchoPackageHandler) Write(ss getty.Session, pkg any) ([]byte, error) {

startTime = time.Now()
if echoPkg, ok = pkg.(*EchoPackage); !ok {
log.Error("illegal pkg:%+v\n", pkg)
_ = log.Error("illegal pkg:%+v\n", pkg)
return nil, errors.New("invalid echo package")
}

Expand Down
4 changes: 2 additions & 2 deletions transport/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (t *callbacks) Add(handler, key any, callback func()) {

// Guard: avoid runtime panic on non-comparable types
if !isComparable(handler) || !isComparable(key) {
log.Error(perrors.New(fmt.Sprintf("callbacks.Add: non-comparable handler/key: %T, %T; ignored", handler, key)))
_ = log.Error(perrors.New(fmt.Sprintf("callbacks.Add: non-comparable handler/key: %T, %T; ignored", handler, key)))
return
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func (t *callbacks) Add(handler, key any, callback func()) {
func (t *callbacks) Remove(handler, key any) {
// Guard: avoid runtime panic on non-comparable types
if !isComparable(handler) || !isComparable(key) {
log.Error(perrors.New(fmt.Sprintf("callbacks.Remove: non-comparable handler/key: %T, %T; ignored", handler, key)))
_ = log.Error(perrors.New(fmt.Sprintf("callbacks.Remove: non-comparable handler/key: %T, %T; ignored", handler, key)))
return
}

Expand Down
2 changes: 1 addition & 1 deletion transport/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (t *gettyTCPConn) CloseConn(waitSec int) {
if t.conn != nil {
if writer, ok := t.writer.(*snappy.Writer); ok {
if err := writer.Close(); err != nil {
log.Errorf("snappy.Writer.Close() = error:%+v", err)
_ = log.Errorf("snappy.Writer.Close() = error:%+v", err)
}
}
if conn, ok := t.conn.(*net.TCPConn); ok {
Expand Down
6 changes: 3 additions & 3 deletions transport/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *server) stop() {
if err := s.server.Shutdown(ctx); err != nil {
// if the log output is "shutdown ctx: context deadline exceeded", it means that
// there are still some active connections.
log.Errorf("server shutdown ctx:%s error:%v", ctx, err)
_ = log.Errorf("server shutdown ctx:%s error:%v", ctx, err)
}
cancel()
}
Expand Down Expand Up @@ -424,7 +424,7 @@ func (s *server) runWSEventLoop(newSession NewSessionCallback) {
s.lock.Unlock()
err = server.Serve(s.streamListener)
if err != nil {
log.Errorf("http.server.Serve(addr{%s}) = err:%+v", s.addr, perrors.WithStack(err))
_ = log.Errorf("http.server.Serve(addr{%s}) = err:%+v", s.addr, perrors.WithStack(err))
}
}()
}
Expand Down Expand Up @@ -484,7 +484,7 @@ func (s *server) runWSSEventLoop(newSession NewSessionCallback) {
s.lock.Unlock()
err = server.Serve(tls.NewListener(s.streamListener, config))
if err != nil {
log.Errorf("http.server.Serve(addr{%s}) = err:%+v", s.addr, perrors.WithStack(err))
_ = log.Errorf("http.server.Serve(addr{%s}) = err:%+v", s.addr, perrors.WithStack(err))
panic(err)
}
}()
Expand Down
44 changes: 27 additions & 17 deletions transport/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ type session struct {
// callbacks
closeCallback callbacks
closeCallbackMutex sync.RWMutex

// wait
closeWait chan struct{}
}
Comment on lines +145 to 148

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

New closeWait channel isn’t reinitialized in Reset(); close(nil) panic risk.

handlePackage closes s.closeWait. After Reset(), closeWait becomes nil (zero value) and closing it will panic.

Apply this diff to Reset():

 func (s *session) Reset() {
     *s = session{
         name:   defaultSessionName,
         once:   &sync.Once{},
         done:   make(chan struct{}),
         period: period,
         wait:   pendingDuration,
-        attrs:  gxcontext.NewValuesContext(context.Background()),
+        attrs:  gxcontext.NewValuesContext(context.Background()),
+        closeWait: make(chan struct{}),
     }
 }

Also applies to: 161-166

🤖 Prompt for AI Agents
In transport/session.go around lines 145-148 and also touching Reset() around
161-166, the closeWait channel is left nil after Reset(), causing a panic when
handlePackage later calls close(s.closeWait); modify Reset() to reinitialize the
channel (e.g. assign a new non-nil channel to s.closeWait) and update any close
calls to guard against nil (or use a safe close pattern such as checking for
non-nil or using sync.Once) so closing cannot panic after a Reset().

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good advice


func newSession(endPoint EndPoint, conn Connection) *session {
Expand All @@ -155,10 +158,11 @@ func newSession(endPoint EndPoint, conn Connection) *session {

period: period,

once: &sync.Once{},
done: make(chan struct{}),
wait: pendingDuration,
attrs: gxcontext.NewValuesContext(context.Background()),
once: &sync.Once{},
done: make(chan struct{}),
wait: pendingDuration,
attrs: gxcontext.NewValuesContext(context.Background()),
closeWait: make(chan struct{}),
}

ss.Connection.SetSession(ss)
Expand Down Expand Up @@ -393,7 +397,7 @@ func (s *session) WritePkg(pkg any, timeout time.Duration) (pkgBytesLenth int, s
rBuf := make([]byte, size)
rBuf = rBuf[:runtime.Stack(rBuf, false)]
err = perrors.WithStack(fmt.Errorf("[session.WritePkg] panic session %s: err=%v\n%s", s.sessionToken(), r, rBuf))
log.Error(err)
_ = log.Error(err)
}
}()

Expand Down Expand Up @@ -553,14 +557,14 @@ func (s *session) run() {
if s.Connection == nil || s.listener == nil || s.writer == nil {
errStr := fmt.Sprintf("session{name:%s, conn:%#v, listener:%#v, writer:%#v}",
s.name, s.Connection, s.listener, s.writer)
log.Error(errStr)
_ = log.Error(errStr)
panic(errStr)
}

// call session opened
s.UpdateActive()
if err := s.listener.OnOpen(s); err != nil {
log.Errorf("[OnOpen] session %s, error: %#v", s.Stat(), err)
_ = log.Errorf("[OnOpen] session %s, error: %#v", s.Stat(), err)
s.Close()
return
}
Expand All @@ -578,7 +582,7 @@ func (s *session) addTask(pkg any) {
f := func() {
// If the session is closed, there is no need to perform CPU-intensive operations.
if s.IsClosed() {
log.Errorf("[Id:%d, name=%s, endpoint=%s] Session is closed", s.ID(), s.name, s.EndPoint())
_ = log.Errorf("[Id:%d, name=%s, endpoint=%s] Session is closed", s.ID(), s.name, s.EndPoint())
return
}
s.listener.OnMessage(s, pkg)
Expand All @@ -599,13 +603,16 @@ func (s *session) handlePackage() {
const size = 64 << 10
rBuf := make([]byte, size)
rBuf = rBuf[:runtime.Stack(rBuf, false)]
log.Errorf("[session.handlePackage] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf)
_ = log.Errorf("[session.handlePackage] panic session %s: err=%s\n%s", s.sessionToken(), r, rBuf)
}
grNum := s.grNum.Add(-1)
log.Infof("%s, [session.handlePackage] gr will exit now, left gr num %d", s.sessionToken(), grNum)
if grNum == 0 {
close(s.closeWait)
}
s.stop()
if err != nil {
log.Errorf("%s, [session.handlePackage] error:%+v", s.sessionToken(), perrors.WithStack(err))
_ = log.Errorf("%s, [session.handlePackage] error:%+v", s.sessionToken(), perrors.WithStack(err))
if s != nil || s.listener != nil {
s.listener.OnError(s, err)
}
Expand All @@ -618,7 +625,7 @@ func (s *session) handlePackage() {
if _, ok := s.Connection.(*gettyTCPConn); ok {
if s.reader == nil {
errStr := fmt.Sprintf("session{name:%s, conn:%#v, reader:%#v}", s.name, s.Connection, s.reader)
log.Error(errStr)
_ = log.Error(errStr)
panic(errStr)
}

Expand Down Expand Up @@ -658,7 +665,7 @@ func (s *session) handleTCPPackage() error {
ctx, cancel := context.WithTimeout(context.Background(), tlsHandshaketime)
defer cancel()
if err := tlsConn.HandshakeContext(ctx); err != nil {
log.Errorf("[tlsConn.HandshakeContext] = error:%+v", err)
_ = log.Errorf("[tlsConn.HandshakeContext] = error:%+v", err)
return perrors.Wrap(err, "tlsConn.HandshakeContext")
}
}
Expand Down Expand Up @@ -695,7 +702,7 @@ func (s *session) handleTCPPackage() error {
}
break
}
log.Errorf("%s, [session.conn.read] = error:%+v", s.sessionToken(), perrors.WithStack(err))
_ = log.Errorf("%s, [session.conn.read] = error:%+v", s.sessionToken(), perrors.WithStack(err))
exit = true
}
break
Expand Down Expand Up @@ -768,14 +775,14 @@ func (s *session) handleUDPPackage() error {
continue
}
if err != nil {
log.Errorf("%s, [session.handleUDPPackage] = len:%d, error:%+v",
_ = log.Errorf("%s, [session.handleUDPPackage] = len:%d, error:%+v",
s.sessionToken(), bufLen, perrors.WithStack(err))
err = perrors.Wrapf(err, "conn.read()")
break
}

if bufLen == 0 {
log.Errorf("conn.read() = bufLen:%d, addr:%s, err:%+v", bufLen, addr, perrors.WithStack(err))
_ = log.Errorf("conn.read() = bufLen:%d, addr:%s, err:%+v", bufLen, addr, perrors.WithStack(err))
continue
}

Expand All @@ -795,7 +802,7 @@ func (s *session) handleUDPPackage() error {
continue
}
if pkgLen == 0 {
log.Errorf("s.reader.Read() = pkg:%#v, pkgLen:%d, err:%+v", pkg, pkgLen, perrors.WithStack(err))
_ = log.Errorf("s.reader.Read() = pkg:%#v, pkgLen:%d, err:%+v", pkg, pkgLen, perrors.WithStack(err))
continue
}

Expand Down Expand Up @@ -877,7 +884,7 @@ func (s *session) stop() {
rBuf = rBuf[:runtime.Stack(rBuf, false)]
err := perrors.WithStack(fmt.Errorf("[session.invokeCloseCallbacks] panic session %s: err=%v\n%s",
sessionToken, r, rBuf))
log.Error(err)
_ = log.Error(err)
}
}()

Expand Down Expand Up @@ -914,6 +921,9 @@ func (s *session) gc() {
// Close will be invoked by NewSessionCallback(if return error is not nil)
// or (session)handleLoop automatically. It's thread safe.
func (s *session) Close() {
if s.IsClosed() {
return
}
s.stop()
log.Infof("%s closed now. its current gr num is %d", s.sessionToken(), s.grNum.Load())
}
Expand Down
12 changes: 6 additions & 6 deletions transport/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *ServerTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) {
config *tls.Config
)
if certificate, err = tls.LoadX509KeyPair(s.ServerKeyCertChainPath, s.ServerPrivateKeyPath); err != nil {
log.Error(fmt.Sprintf("tls.LoadX509KeyPair(certs{%s}, privateKey{%s}) = err:%+v",
_ = log.Error(fmt.Sprintf("tls.LoadX509KeyPair(certs{%s}, privateKey{%s}) = err:%+v",
s.ServerKeyCertChainPath, s.ServerPrivateKeyPath, perrors.WithStack(err)))
return nil, err
}
Expand All @@ -68,12 +68,12 @@ func (s *ServerTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) {
if s.ServerTrustCertCollectionPath != "" {
certPem, err = os.ReadFile(s.ServerTrustCertCollectionPath)
if err != nil {
log.Error(fmt.Errorf("os.ReadFile(certFile{%s}) = err:%+v", s.ServerTrustCertCollectionPath, perrors.WithStack(err)))
_ = log.Error(fmt.Errorf("os.ReadFile(certFile{%s}) = err:%+v", s.ServerTrustCertCollectionPath, perrors.WithStack(err)))
return nil, err
}
certPool = x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM(certPem); !ok {
log.Error("failed to parse root certificate file")
_ = log.Error("failed to parse root certificate file")
return nil, err
}
Comment on lines 75 to 78

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix returning nil error on server cert parse failure

When AppendCertsFromPEM fails, err is still nil, so we return (nil, nil) and callers treat the TLS build as successful until they dereference a nil config. Please create and propagate a real error before returning.

-		if ok := certPool.AppendCertsFromPEM(certPem); !ok {
-			_ = log.Error("failed to parse root certificate file")
-			return nil, err
+		if ok := certPool.AppendCertsFromPEM(certPem); !ok {
+			err = perrors.New("failed to parse root certificate file")
+			_ = log.Error(err)
+			return nil, err
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ok := certPool.AppendCertsFromPEM(certPem); !ok {
log.Error("failed to parse root certificate file")
_ = log.Error("failed to parse root certificate file")
return nil, err
}
if ok := certPool.AppendCertsFromPEM(certPem); !ok {
err = perrors.New("failed to parse root certificate file")
_ = log.Error(err)
return nil, err
}
🤖 Prompt for AI Agents
In transport/tls.go around lines 75 to 78, the code logs a failure from
certPool.AppendCertsFromPEM but returns the nil err variable, causing callers to
receive (nil, nil); create a real error (e.g., fmt.Errorf or errors.New)
describing the root certificate parse failure, log that error, and return (nil,
err) so the caller receives a non-nil error indicating TLS config construction
failed.

config.ClientCAs = certPool
Expand All @@ -95,18 +95,18 @@ type ClientTlsConfigBuilder struct {
func (c *ClientTlsConfigBuilder) BuildTlsConfig() (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(c.ClientKeyCertChainPath, c.ClientPrivateKeyPath)
if err != nil {
log.Error(fmt.Sprintf("Unable to load X509 Key Pair %v", err))
_ = log.Error(fmt.Sprintf("Unable to load X509 Key Pair %v", err))
return nil, err
}
certBytes, err := os.ReadFile(c.ClientTrustCertCollectionPath)
if err != nil {
log.Error(fmt.Sprintf("Unable to read pem file: %s", c.ClientTrustCertCollectionPath))
_ = log.Error(fmt.Sprintf("Unable to read pem file: %s", c.ClientTrustCertCollectionPath))
return nil, err
}
clientCertPool := x509.NewCertPool()
ok := clientCertPool.AppendCertsFromPEM(certBytes)
if !ok {
log.Error("failed to parse root certificate")
_ = log.Error("failed to parse root certificate")
return nil, err
}
Comment on lines 108 to 111

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Propagate a real error when client cert parsing fails

Same issue here: err is nil when AppendCertsFromPEM returns false, so the function hands back (nil, nil) and masks the failure. Please instantiate and return a proper error.

-	ok := clientCertPool.AppendCertsFromPEM(certBytes)
-	if !ok {
-		_ = log.Error("failed to parse root certificate")
-		return nil, err
+	ok := clientCertPool.AppendCertsFromPEM(certBytes)
+	if !ok {
+		err = perrors.New("failed to parse root certificate")
+		_ = log.Error(err)
+		return nil, err
🤖 Prompt for AI Agents
In transport/tls.go around lines 108 to 111, when AppendCertsFromPEM returns
false the code currently logs an error but returns (nil, err) where err is nil,
causing a masked success; change this to create a real error (e.g. using
fmt.Errorf or errors.New with a clear message like "failed to parse root
certificate: AppendCertsFromPEM returned false"), log that error if desired, and
return (nil, thatError) so the caller receives a non-nil error.

return &tls.Config{
Expand Down
33 changes: 24 additions & 9 deletions util/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,29 @@ import (
type Logger interface {
Info(args ...any)
Warn(args ...any)
Error(args ...any)
Error(args ...any) error
Debug(args ...any)
Infof(fmt string, args ...any)
Warnf(fmt string, args ...any)
Errorf(fmt string, args ...any)
Errorf(fmt string, args ...any) error
Debugf(fmt string, args ...any)
}
Comment on lines +29 to 35

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

🧩 Analysis chain

Breaking change: Logger.Error/Errorf now return error. Provide a backward‑compat shim.

This breaks existing custom loggers passed via SetLogger. Add a LegacyLogger adapter to preserve compatibility.

Proposed additions (outside the hunks):

// LegacyLogger mirrors the old contract (no error returns).
type LegacyLogger interface {
    Info(args ...any)
    Warn(args ...any)
    Error(args ...any)
    Debug(args ...any)
    Infof(string, ...any)
    Warnf(string, ...any)
    Errorf(string, ...any)
    Debugf(string, ...any)
}

// Wrap a legacy logger so it satisfies the new Logger.
type legacyLoggerAdapter struct{ LegacyLogger }

func (l legacyLoggerAdapter) Error(args ...any) error  { l.LegacyLogger.Error(args...); return nil }
func (l legacyLoggerAdapter) Errorf(f string, a ...any) error {
    l.LegacyLogger.Errorf(f, a...); return nil
}

// SetLegacyLogger allows existing integrations to continue working.
func SetLegacyLogger(legacy LegacyLogger) { log = legacyLoggerAdapter{LegacyLogger: legacy} }

Search for external uses in this repo:


🏁 Script executed:

#!/bin/bash
rg -nP '\bSetLogger\(' -C2

Length of output: 183


Add LegacyLogger adapter for backward compatibility
Logger.Error and Errorf now return an error, which will break any existing custom loggers passed via SetLogger. Define a LegacyLogger interface and a legacyLoggerAdapter plus SetLegacyLogger shim to preserve the old (no-error) contract:

// LegacyLogger mirrors the old contract (no error returns).
type LegacyLogger interface {
    Info(args ...any)
    Warn(args ...any)
    Error(args ...any)
    Debug(args ...any)
    Infof(string, ...any)
    Warnf(string, ...any)
    Errorf(string, ...any)
    Debugf(string, ...any)
}

// legacyLoggerAdapter wraps a LegacyLogger to satisfy the new Logger.
type legacyLoggerAdapter struct{ LegacyLogger }

func (l legacyLoggerAdapter) Error(args ...any) error {
    l.LegacyLogger.Error(args...)
    return nil
}
func (l legacyLoggerAdapter) Errorf(f string, a ...any) error {
    l.LegacyLogger.Errorf(f, a...)
    return nil
}

// SetLegacyLogger installs a LegacyLogger for compatibility with SetLogger.
func SetLegacyLogger(legacy LegacyLogger) {
    log = legacyLoggerAdapter{LegacyLogger: legacy}
}
🤖 Prompt for AI Agents
In util/logger.go around lines 29 to 35, add a LegacyLogger interface mirroring
the old no-error methods and implement a legacyLoggerAdapter that wraps a
LegacyLogger and implements the new Logger by delegating calls and returning nil
for Error and Errorf; then add a SetLegacyLogger(legacy LegacyLogger) shim that
assigns log = legacyLoggerAdapter{LegacyLogger: legacy} so existing custom
loggers continue to work without modifying their signatures.


// zapLoggerAdapter adapts zap.SugaredLogger to the Logger interface
type zapLoggerAdapter struct {
*zap.SugaredLogger
}

func (l *zapLoggerAdapter) Error(args ...any) error {
l.SugaredLogger.Error(args...)
return nil
}

func (l *zapLoggerAdapter) Errorf(fmt string, args ...any) error {
l.SugaredLogger.Errorf(fmt, args...)
return nil
}

type LoggerLevel int8

const (
Expand Down Expand Up @@ -79,7 +94,7 @@ var (
func init() {
zapLoggerConfig.EncoderConfig = zapLoggerEncoderConfig
zapLogger, _ = zapLoggerConfig.Build()
log = zapLogger.Sugar()
log = &zapLoggerAdapter{zapLogger.Sugar()}

// todo: flushes buffer when redirect log to file.
// var exitSignal = make(chan os.Signal)
Expand Down Expand Up @@ -114,7 +129,7 @@ func SetLoggerLevel(level LoggerLevel) error {
if err != nil {
return err
}
log = zapLogger.Sugar()
log = &zapLoggerAdapter{zapLogger.Sugar()}
return nil
}

Expand All @@ -128,7 +143,7 @@ func SetLoggerCallerDisable() error {
if err != nil {
return err
}
log = zapLogger.Sugar()
log = &zapLoggerAdapter{zapLogger.Sugar()}
return nil
}

Expand Down Expand Up @@ -163,11 +178,11 @@ func Warnf(template string, args ...any) {
}

// Error
func Error(args ...any) {
log.Error(args...)
func Error(args ...any) error {
return log.Error(args...)
}

// Errorf
func Errorf(template string, args ...any) {
log.Errorf(template, args...)
func Errorf(template string, args ...any) error {
return log.Errorf(template, args...)
}
Loading