5 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
Adrian Zuercher
29f3ee26f9 new config options and error handles new input any 2025-04-21 12:59:16 +02:00
6 changed files with 118 additions and 30 deletions

7
go.mod
View File

@@ -1,7 +1,10 @@
module github.com/tecamino/tecamino-logger
module gitea.tecamino.com/paadi/tecamino-logger
go 1.21.0
require go.uber.org/zap v1.27.0
require (
go.uber.org/zap v1.27.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)
require go.uber.org/multierr v1.10.0 // indirect

2
go.sum
View File

@@ -10,5 +10,7 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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")
}

19
logging/cgf.go Normal file
View File

@@ -0,0 +1,19 @@
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,
}
}

View File

@@ -3,10 +3,13 @@ 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
@@ -24,14 +27,19 @@ type Logger struct {
debugging bool
}
func NewLogger(name string, debug bool) (*Logger, error) {
func NewLogger(file string, cfg *Config) (*Logger, error) {
if name == "" {
name = "NewLogger"
if cfg == nil {
cfg = DefaultConfig()
}
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
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()
@@ -41,13 +49,42 @@ func NewLogger(name string, debug bool) (*Logger, error) {
}
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(zapcore.NewCore(encoder, zapcore.AddSync(file), zapcore.InfoLevel)),
debug: zap.New(zapcore.NewCore(encoder, zapcore.AddSync(file), zapcore.DebugLevel)),
debugging: debug,
},
nil
log: zap.New(combinedCore),
debug: zap.New(debugCore),
debugging: cfg.Debug,
}, nil
}
func (l *Logger) Info(caller, msg string) {
@@ -58,8 +95,8 @@ 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) Error(caller string, msg any) {
l.log.Error(fmt.Sprint(msg), zap.String("caller", caller))
}
func (l *Logger) Debug(caller, msg string) {

15
main.go
View File

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