Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a7b4e70867 | |||
| b53f0a09b2 | |||
| 40099fffc6 | |||
| 62238574cb | |||
| a022a9a8ba | |||
| c31761dc10 |
@@ -1,4 +1,4 @@
|
|||||||
module github.com/tecamino/tecamino-logger
|
module gitea.tecamino.local/paadi/tecamino-logger
|
||||||
|
|
||||||
go 1.21.0
|
go 1.21.0
|
||||||
|
|
||||||
|
|||||||
@@ -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")
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
package logging
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"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 = "NewLogger.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: 1, // megabytes
|
|
||||||
MaxBackups: 3,
|
|
||||||
MaxAge: 28, // 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))
|
|
||||||
}
|
|
||||||
@@ -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")
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user