aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-02-04 18:24:56 +0530
committerBobby <[email protected]>2026-02-04 18:24:56 +0530
commitb0f01eea9d61aa4d05b0fe253c8a32e35fa95e28 (patch)
tree51fce22ec40a3a78707c0b3a68a3b4017fdc36bc
parent45426e39a36701dabb5326ec4a84444894badfd2 (diff)
downloadmetachan-b0f01eea9d61aa4d05b0fe253c8a32e35fa95e28.tar.xz
metachan-b0f01eea9d61aa4d05b0fe253c8a32e35fa95e28.zip
Refactor logger: enhance logging structure with zap integration and remove unused LogOptions
-rw-r--r--utils/logger/logger.go162
-rw-r--r--utils/logger/types.go9
2 files changed, 100 insertions, 71 deletions
diff --git a/utils/logger/logger.go b/utils/logger/logger.go
index 3982010..41e7f1e 100644
--- a/utils/logger/logger.go
+++ b/utils/logger/logger.go
@@ -5,99 +5,133 @@ import (
"os"
"strings"
"time"
+
+ "go.uber.org/zap"
+ "go.uber.org/zap/zapcore"
)
const prefixWidth = 15
-func getTimestamp() string {
- return time.Now().Format(time.RFC3339)
+var (
+ loggerInstance *zap.Logger
+ level zap.AtomicLevel
+)
+
+func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
+ enc.AppendString(Gray + t.Format(time.RFC3339) + Reset)
}
-func getLevelColor(level LogLevel) string {
- switch level {
- case Info:
- return LevelColorInfo
- case Warn:
- return LevelColorWarn
- case Error:
- return LevelColorError
- case Debug:
- return LevelColorDebug
- case Success:
- return LevelColorSuccess
+func levelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
+ switch l {
+ case zapcore.DebugLevel:
+ enc.AppendString(LevelColorDebug)
+ case zapcore.WarnLevel:
+ enc.AppendString(LevelColorWarn)
+ case zapcore.ErrorLevel:
+ enc.AppendString(LevelColorError)
default:
- return LevelColorInfo
+ enc.AppendString(LevelColorInfo)
}
}
-func getMessageColor(level LogLevel) string {
+func formatPrefix(prefix string) string {
+ if prefix == "" {
+ return ""
+ }
+
+ padding := ""
+ if len(prefix) < prefixWidth {
+ padding = strings.Repeat(" ", prefixWidth-len(prefix))
+ }
+
+ return Cyan + "[" + prefix + "]" + Reset + padding
+}
+
+func colorMessage(level LogLevel, msg string) string {
switch level {
- case Info:
- return MessageColorInfo
+ case Debug:
+ return MessageColorDebug + msg + Reset
case Warn:
- return MessageColorWarn
+ return MessageColorWarn + msg + Reset
case Error:
- return MessageColorError
- case Debug:
- return MessageColorDebug
+ return MessageColorError + msg + Reset
case Success:
- return MessageColorSuccess
+ return MessageColorSuccess + msg + Reset
default:
- return MessageColorInfo
+ return MessageColorInfo + msg + Reset
}
}
-func Log(message any, options LogOptions) {
- var builder strings.Builder
+func Init() {
+ level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
- if options.Timestamp {
- builder.WriteString(Gray)
- builder.WriteString(getTimestamp())
- builder.WriteString(Reset)
- builder.WriteString(" ")
+ encoderCfg := zapcore.EncoderConfig{
+ TimeKey: "ts",
+ LevelKey: "level",
+ MessageKey: "msg",
+ LineEnding: "\n",
+ EncodeTime: timeEncoder,
+ EncodeLevel: levelEncoder,
}
- builder.WriteString(getLevelColor(options.Level))
- builder.WriteString(" ")
+ encoder := zapcore.NewConsoleEncoder(encoderCfg)
+
+ stdout := zapcore.AddSync(os.Stdout)
+ stderr := zapcore.AddSync(os.Stderr)
- if options.Prefix != "" {
- totalWidth := len(options.Prefix)
- padding := ""
+ core := zapcore.NewTee(
+ zapcore.NewCore(encoder, stdout, zap.LevelEnablerFunc(func(l zapcore.Level) bool {
+ return l < zapcore.WarnLevel && level.Enabled(l)
+ })),
+ zapcore.NewCore(encoder, stderr, zap.LevelEnablerFunc(func(l zapcore.Level) bool {
+ return l >= zapcore.WarnLevel && level.Enabled(l)
+ })),
+ )
- if totalWidth < prefixWidth {
- padding = strings.Repeat(" ", prefixWidth-totalWidth)
- }
+ loggerInstance = zap.New(core, zap.AddCaller())
+}
- builder.WriteString(Cyan)
- builder.WriteString("[")
- builder.WriteString(options.Prefix)
- builder.WriteString("]")
- builder.WriteString(Reset)
- builder.WriteString(padding)
- builder.WriteString(" ")
+func SetDebug(enabled bool) {
+ if enabled {
+ level.SetLevel(zapcore.DebugLevel)
+ } else {
+ level.SetLevel(zapcore.InfoLevel)
}
+}
- builder.WriteString(getMessageColor(options.Level))
+func Debugf(prefix, format string, args ...any) {
+ log(Debug, zapcore.DebugLevel, prefix, fmt.Sprintf(format, args...))
+}
- switch msg := message.(type) {
- case error:
- builder.WriteString(msg.Error())
- case string:
- builder.WriteString(msg)
- default:
- fmt.Fprintf(&builder, "%v", msg)
- }
+func Infof(prefix, format string, args ...any) {
+ log(Info, zapcore.InfoLevel, prefix, fmt.Sprintf(format, args...))
+}
- builder.WriteString(Reset)
- builder.WriteString("\n")
+func Successf(prefix, format string, args ...any) {
+ log(Success, zapcore.InfoLevel, prefix, fmt.Sprintf(format, args...))
+}
- if options.Level == Error || options.Level == Warn {
- os.Stderr.WriteString(builder.String())
- } else {
- os.Stdout.WriteString(builder.String())
- }
+func Warnf(prefix, format string, args ...any) {
+ log(Warn, zapcore.WarnLevel, prefix, fmt.Sprintf(format, args...))
+}
+
+func Errorf(prefix, format string, args ...any) {
+ log(Error, zapcore.ErrorLevel, prefix, fmt.Sprintf(format, args...))
+}
- if options.Fatal {
- os.Exit(1)
+func Fatalf(prefix, format string, args ...any) {
+ log(Error, zapcore.ErrorLevel, prefix, fmt.Sprintf(format, args...))
+ os.Exit(1)
+}
+
+func log(levelLabel LogLevel, zapLevel zapcore.Level, prefix string, msg any) {
+ if loggerInstance == nil {
+ panic("logger.Init() was not called")
}
+
+ message := fmt.Sprint(msg)
+ colored := colorMessage(levelLabel, message)
+
+ loggerInstance.Check(zapLevel, colored).
+ Write(zap.String("prefix", formatPrefix(prefix)))
}
diff --git a/utils/logger/types.go b/utils/logger/types.go
index 5ef310f..9a92b01 100644
--- a/utils/logger/types.go
+++ b/utils/logger/types.go
@@ -8,7 +8,9 @@ const (
Warn LogLevel = "warn"
Error LogLevel = "error"
Success LogLevel = "success"
+)
+const (
Reset = "\033[0m"
Cyan = "\033[36m"
Gray = "\033[90m"
@@ -25,10 +27,3 @@ const (
MessageColorDebug = "\033[90m"
MessageColorSuccess = "\033[32m"
)
-
-type LogOptions struct {
- Timestamp bool
- Prefix string
- Level LogLevel
- Fatal bool
-}