remodel whole logger

This commit is contained in:
Adrian Zürcher
2025-04-16 09:23:20 +02:00
parent eac917fa58
commit cdbeaf3940
3 changed files with 76 additions and 69 deletions

70
logger.go Normal file
View File

@@ -0,0 +1,70 @@
package main
import (
"fmt"
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Level zapcore.Level
const (
Info Level = 0
Warning Level = 1
Error Level = 2
Debug Level = -1
)
type Logger struct {
log *zap.Logger
debug *zap.Logger
debugging bool
}
func NewLogger(name string, debug bool) (*Logger, error) {
if name == "" {
name = "NewLogger"
}
file, err := os.OpenFile(fmt.Sprintf("%s.log", name), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "timestamp"
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02T15:04:05.000"))
}
encoder := zapcore.NewJSONEncoder(encoderConfig)
// Build the logger
return &Logger{
log: zap.New(zapcore.NewCore(encoder, zapcore.AddSync(file), zapcore.InfoLevel)),
debug: zap.New(zapcore.NewCore(encoder, zapcore.AddSync(file), zapcore.DebugLevel)),
debugging: debug,
},
nil
}
func (l *Logger) Info(caller, msg string) {
l.log.Info(msg, zap.String("caller", caller))
}
func (l *Logger) Warning(caller, msg string) {
l.log.Warn(msg, zap.String("caller", caller))
}
func (l *Logger) Error(caller, msg string) {
l.log.Error(msg, zap.String("caller", caller))
}
func (l *Logger) Debug(caller, msg string) {
if !l.debugging {
return
}
l.debug.Debug(msg, zap.String("caller", caller))
}

View File

@@ -1,62 +0,0 @@
package logger
import (
"fmt"
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Level zapcore.Level
const (
Info Level = 0
Warning Level = 1
Error Level = 2
Debug Level = -1
)
type Logger struct {
log *zap.Logger
debug *zap.Logger
}
func NewLogger(name string) (*Logger, error) {
if name == "" {
name = "NewLogger"
}
file, err := os.OpenFile(fmt.Sprintf("%s.log", name), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "timestamp"
encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02T15:04:05.000"))
}
encoder := zapcore.NewJSONEncoder(encoderConfig)
// Build the logger
return &Logger{
log: zap.New(zapcore.NewCore(encoder, zapcore.AddSync(file), zapcore.InfoLevel)),
debug: zap.New(zapcore.NewCore(encoder, zapcore.AddSync(file), zapcore.DebugLevel)),
},
nil
}
func (l *Logger) Log(logLevel Level, caller, msg string) {
switch logLevel {
case Info:
l.log.Info(msg, zap.String("caller", caller))
case Warning:
l.log.Warn(msg, zap.String("caller", caller))
case Error:
l.log.Error(msg, zap.String("caller", caller))
case Debug:
l.debug.Debug(msg, zap.String("caller", caller))
}
}

13
main.go
View File

@@ -1,14 +1,13 @@
package main
import "github.com/tecamino/tecamino-logger/logger"
func main() {
log, err := logger.NewLogger("Test")
debug := false
log, err := NewLogger("Test", debug)
if err != nil {
panic(err)
}
log.Log(logger.Info, "line 10", "hallo")
log.Log(logger.Warning, "line 11", "vello")
log.Log(logger.Error, "line 12", "hie")
log.Log(logger.Debug, "line 13", "isch")
log.Info("line 10", "hallo")
log.Warning("line 11", "vello")
log.Error("line 12", "hie")
log.Debug("line 13", "isch")
}