4 Commits

Author SHA1 Message Date
Adrian Zürcher
40099fffc6 fix .log ending for empty parameter in linux systems 2026-01-18 21:42:36 +01:00
Adrian Zürcher
62238574cb change empty filepath to executable name 2025-12-12 10:01:11 +01:00
Adrian Zürcher
a022a9a8ba replace main to test modul 2025-12-12 10:00:39 +01:00
Adrian Zuercher
c31761dc10 reo name change 2025-08-06 22:28:09 +02:00
4 changed files with 54 additions and 25 deletions

2
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/tecamino/tecamino-logger module gitea.tecamino.com/paadi/tecamino-logger
go 1.21.0 go 1.21.0

42
logger_test.go Normal file
View File

@@ -0,0 +1,42 @@
package tecaminologger
import (
"errors"
"os"
"testing"
"gitea.tecamino.com/paadi/tecamino-logger/logging"
)
func TestLogger(t *testing.T) {
t.Log("start logger test")
logFile := "Test.log"
log, err := logging.NewLogger(logFile, nil)
if err != nil {
t.Fatal(err)
}
log.Info("line 10", "hallo")
log.Warning("line 11", "vello")
log.Error("line 12", "hie")
err = errors.New("test error")
log.Error("line 14", err)
log.Debug("line 15", "isch")
t.Log("read created log file")
f, err := os.ReadFile(logFile)
if err != nil {
t.Fatal(err)
}
t.Log("read created log file")
t.Log(string(f))
t.Log("test if log file name empty")
log, err = logging.NewLogger("", nil)
if err != nil {
t.Fatal(err)
}
log.Info("test", "this is a text")
}

View File

@@ -3,6 +3,8 @@ package logging
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings"
"time" "time"
"go.uber.org/zap" "go.uber.org/zap"
@@ -32,7 +34,12 @@ func NewLogger(file string, cfg *Config) (*Logger, error) {
} }
if file == "" { if file == "" {
file = "NewLogger.log" 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 := zap.NewProductionEncoderConfig()
@@ -44,9 +51,9 @@ func NewLogger(file string, cfg *Config) (*Logger, error) {
fileSyncer := zapcore.AddSync(&lumberjack.Logger{ fileSyncer := zapcore.AddSync(&lumberjack.Logger{
Filename: file, Filename: file,
MaxSize: 1, // megabytes MaxSize: cfg.MaxSize, // megabytes
MaxBackups: 3, MaxBackups: cfg.MaxBackup,
MaxAge: 28, // days MaxAge: cfg.MaxAge, // days
}) })
// Create core list // Create core list

20
main.go
View File

@@ -1,20 +0,0 @@
package main
import (
"errors"
"github.com/tecamino/tecamino-logger/logging"
)
func main() {
log, err := logging.NewLogger("Test.log", nil)
if err != nil {
panic(err)
}
log.Info("line 10", "hallo")
log.Warning("line 11", "vello")
log.Error("line 12", "hie")
err = errors.New("test error")
log.Error("line 14", err)
log.Debug("line 15", "isch")
}