Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c31761dc10 | ||
![]() |
29f3ee26f9 | ||
![]() |
6f963ae0a4 | ||
![]() |
cdbeaf3940 | ||
![]() |
eac917fa58 | ||
![]() |
a6eba03146 | ||
![]() |
3aecfe9832 | ||
![]() |
f9471b91c0 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.log
|
@@ -1 +0,0 @@
|
|||||||
**/*.log
|
|
7
go.mod
7
go.mod
@@ -1,7 +1,10 @@
|
|||||||
module github.com/tecamino/tecamino-logger.git
|
module gitea.tecamino.com/paadi/tecamino-logger
|
||||||
|
|
||||||
go 1.21.0
|
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
|
require go.uber.org/multierr v1.10.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@@ -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/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 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@@ -1,62 +0,0 @@
|
|||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
)
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLogger(name string) (*Logger, error) {
|
|
||||||
|
|
||||||
if name == "" {
|
|
||||||
name = "NewLogger"
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
// 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)),
|
|
||||||
},
|
|
||||||
nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Logger) Log(logLevel Level, caller, msg string) {
|
|
||||||
switch logLevel {
|
|
||||||
case Info:
|
|
||||||
l.log.Info(msg, zap.String("caller", caller))
|
|
||||||
case Warning:
|
|
||||||
l.log.Warn(msg, zap.String("caller", caller))
|
|
||||||
case Error:
|
|
||||||
l.log.Error(msg, zap.String("caller", caller))
|
|
||||||
case Debug:
|
|
||||||
l.debug.Debug(msg, zap.String("caller", caller))
|
|
||||||
}
|
|
||||||
}
|
|
19
logging/cgf.go
Normal file
19
logging/cgf.go
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
100
logging/logging.go
Normal file
100
logging/logging.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
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))
|
||||||
|
}
|
18
main.go
18
main.go
@@ -1,14 +1,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/tecamino/tecamino-logger.git/logger"
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"gitea.tecamino.com/paadi/tecamino-logger/logging"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log, err := logger.NewLogger("Test")
|
log, err := logging.NewLogger("Test.log", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
log.Log(logger.Info, "line 10", "hallo")
|
log.Info("line 10", "hallo")
|
||||||
log.Log(logger.Warning, "line 11", "vello")
|
log.Warning("line 11", "vello")
|
||||||
log.Log(logger.Error, "line 12", "hie")
|
log.Error("line 12", "hie")
|
||||||
log.Log(logger.Debug, "line 13", "isch")
|
err = errors.New("test error")
|
||||||
|
log.Error("line 14", err)
|
||||||
|
log.Debug("line 15", "isch")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user