From a7b4e70867134f8f11cc621e19fac311c0f9273e Mon Sep 17 00:00:00 2001 From: paadi Date: Mon, 20 Apr 2026 12:11:47 +0000 Subject: [PATCH] optimze package and remove logging subpackage --- logger_test.go | 6 +-- logging/cgf.go | 19 -------- logging/logging.go | 107 --------------------------------------------- 3 files changed, 2 insertions(+), 130 deletions(-) delete mode 100644 logging/cgf.go delete mode 100644 logging/logging.go diff --git a/logger_test.go b/logger_test.go index fc1447c..06eb407 100644 --- a/logger_test.go +++ b/logger_test.go @@ -4,14 +4,12 @@ import ( "errors" "os" "testing" - - "gitea.tecamino.local/paadi/tecamino-logger/logging" ) func TestLogger(t *testing.T) { t.Log("start logger test") logFile := "Test.log" - log, err := logging.NewLogger(logFile, nil) + log, err := NewLogger(logFile, nil) if err != nil { t.Fatal(err) } @@ -33,7 +31,7 @@ func TestLogger(t *testing.T) { t.Log(string(f)) t.Log("test if log file name empty") - log, err = logging.NewLogger("", nil) + log, err = NewLogger("", nil) if err != nil { t.Fatal(err) } diff --git a/logging/cgf.go b/logging/cgf.go deleted file mode 100644 index 4b55671..0000000 --- a/logging/cgf.go +++ /dev/null @@ -1,19 +0,0 @@ -package logging - -type Config struct { - MaxSize int // max. size of file in MB - MaxBackup int // - MaxAge int - Debug bool - TerminalOut bool -} - -func DefaultConfig() *Config { - return &Config{ - Debug: false, - TerminalOut: false, - MaxSize: 1, - MaxBackup: 3, - MaxAge: 28, - } -} diff --git a/logging/logging.go b/logging/logging.go deleted file mode 100644 index 33487f6..0000000 --- a/logging/logging.go +++ /dev/null @@ -1,107 +0,0 @@ -package logging - -import ( - "fmt" - "os" - "path/filepath" - "strings" - "time" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "gopkg.in/natefinch/lumberjack.v2" -) - -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(file string, cfg *Config) (*Logger, error) { - - if cfg == nil { - cfg = DefaultConfig() - } - - if file == "" { - file = os.Args[0] - if filepath.Ext(file) != "" { - file = strings.Replace(filepath.Base(file), filepath.Ext(file), ".log", 1) - } else { - file = filepath.Base(file) + ".log" - } - } - - 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) - - fileSyncer := zapcore.AddSync(&lumberjack.Logger{ - Filename: file, - MaxSize: cfg.MaxSize, // megabytes - MaxBackups: cfg.MaxBackup, - MaxAge: cfg.MaxAge, // days - }) - - // Create core list - var cores []zapcore.Core - - // File core (always added) - cores = append(cores, zapcore.NewCore(encoder, fileSyncer, zapcore.InfoLevel)) - - // Optional stdout core - if cfg.TerminalOut { - stdoutSyncer := zapcore.AddSync(zapcore.Lock(os.Stdout)) - cores = append(cores, zapcore.NewCore(encoder, stdoutSyncer, zapcore.InfoLevel)) - } - - combinedCore := zapcore.NewTee(cores...) - - // If debugging, add a separate debug core (to both file and stdout if requested) - var debugCores []zapcore.Core - debugCores = append(debugCores, zapcore.NewCore(encoder, fileSyncer, zapcore.DebugLevel)) - if cfg.TerminalOut { - stdoutSyncer := zapcore.AddSync(zapcore.Lock(os.Stdout)) - debugCores = append(debugCores, zapcore.NewCore(encoder, stdoutSyncer, zapcore.DebugLevel)) - } - debugCore := zapcore.NewTee(debugCores...) - - // Build the logger - return &Logger{ - log: zap.New(combinedCore), - debug: zap.New(debugCore), - debugging: cfg.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 string, msg any) { - l.log.Error(fmt.Sprint(msg), zap.String("caller", caller)) -} - -func (l *Logger) Debug(caller, msg string) { - if !l.debugging { - return - } - l.debug.Debug(msg, zap.String("caller", caller)) -}