6 Commits

Author SHA1 Message Date
paadi 53da3a9a6e optimze package and remove logging subpackage 2026-04-20 12:43:02 +00:00
paadi a7b4e70867 optimze package and remove logging subpackage 2026-04-20 12:11:47 +00:00
root b53f0a09b2 change to local domain 2026-04-20 11:28:00 +00:00
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
5 changed files with 54 additions and 27 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
package logging package tecaminologger
type Config struct { type Config struct {
MaxSize int // max. size of file in MB MaxSize int // max. size of file in MB
+1 -1
View File
@@ -1,4 +1,4 @@
module gitea.tecamino.com/paadi/tecamino-logger module gitea.tecamino.local/paadi/tecamino-logger
go 1.21.0 go 1.21.0
+12 -5
View File
@@ -1,8 +1,10 @@
package logging package tecaminologger
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
+40
View File
@@ -0,0 +1,40 @@
package tecaminologger
import (
"errors"
"os"
"testing"
)
func TestLogger(t *testing.T) {
t.Log("start logger test")
logFile := "Test.log"
log, err := 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 = NewLogger("", nil)
if err != nil {
t.Fatal(err)
}
log.Info("test", "this is a text")
}
-20
View File
@@ -1,20 +0,0 @@
package main
import (
"errors"
"gitea.tecamino.com/paadi/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")
}