-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: knqyf263 <[email protected]>
- Loading branch information
Showing
26 changed files
with
244 additions
and
129 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,110 @@ | ||
package log | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
var Logger *zap.SugaredLogger | ||
// Logger is an alias for slog.Logger | ||
type Logger = slog.Logger | ||
|
||
var defaultLogger *Logger | ||
|
||
// Convenience variables to match slog's API | ||
var ( | ||
String = slog.String | ||
Int = slog.Int | ||
Int64 = slog.Int64 | ||
Bool = slog.Bool | ||
Any = slog.Any | ||
) | ||
|
||
// Package-level logging functions | ||
func Info(msg string, args ...any) { | ||
defaultLogger.Info(msg, args...) | ||
} | ||
|
||
func Warn(msg string, args ...any) { | ||
defaultLogger.Warn(msg, args...) | ||
} | ||
|
||
func Error(msg string, args ...any) { | ||
defaultLogger.Error(msg, args...) | ||
} | ||
|
||
func Debug(msg string, args ...any) { | ||
defaultLogger.Debug(msg, args...) | ||
} | ||
|
||
func Err(err error) slog.Attr { | ||
return slog.Attr{Key: "error", Value: slog.AnyValue(err)} | ||
} | ||
|
||
func FilePath(path string) slog.Attr { | ||
return slog.Attr{Key: "file_path", Value: slog.AnyValue(path)} | ||
} | ||
|
||
func DirPath(path string) slog.Attr { | ||
return slog.Attr{Key: "dir_path", Value: slog.AnyValue(path)} | ||
} | ||
|
||
// PrefixHandler is a simple wrapper around slog.Handler that adds a prefix to all messages | ||
type PrefixHandler struct { | ||
prefix string | ||
handler slog.Handler | ||
} | ||
|
||
func init() { | ||
conf := zap.NewDevelopmentConfig() | ||
conf.DisableCaller = true | ||
conf.DisableStacktrace = true | ||
conf.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder | ||
opts := &slog.HandlerOptions{ | ||
Level: slog.LevelDebug, | ||
} | ||
baseHandler := slog.NewTextHandler(os.Stdout, opts) | ||
prefixHandler := &PrefixHandler{ | ||
prefix: "", | ||
handler: baseHandler, | ||
} | ||
defaultLogger = slog.New(prefixHandler) | ||
} | ||
|
||
// WithPrefix returns a new logger with the specified prefix | ||
func WithPrefix(prefix string) *Logger { | ||
return slog.New(&PrefixHandler{ | ||
prefix: prefix, | ||
handler: defaultLogger.Handler(), | ||
}) | ||
} | ||
|
||
func SetLogger(l *Logger) { | ||
defaultLogger = l | ||
} | ||
|
||
// Handle implements slog.Handler interface | ||
func (h *PrefixHandler) Handle(ctx context.Context, r slog.Record) error { | ||
if h.prefix != "" { | ||
r.Message = fmt.Sprintf("[%s] %s", h.prefix, r.Message) | ||
} | ||
return h.handler.Handle(ctx, r) | ||
} | ||
|
||
// WithAttrs implements slog.Handler interface | ||
func (h *PrefixHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
return &PrefixHandler{ | ||
prefix: h.prefix, | ||
handler: h.handler.WithAttrs(attrs), | ||
} | ||
} | ||
|
||
logger, _ := conf.Build() | ||
Logger = logger.Sugar() | ||
// WithGroup implements slog.Handler interface | ||
func (h *PrefixHandler) WithGroup(name string) slog.Handler { | ||
return &PrefixHandler{ | ||
prefix: h.prefix, | ||
handler: h.handler.WithGroup(name), | ||
} | ||
} | ||
|
||
func SetLogger(l *zap.SugaredLogger) { | ||
Logger = l | ||
// Enabled implements slog.Handler interface | ||
func (h *PrefixHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return h.handler.Enabled(ctx, level) | ||
} |
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
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
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
Oops, something went wrong.