diff --git a/.gitingnore b/.gitingnore new file mode 100644 index 0000000..15a55c1 --- /dev/null +++ b/.gitingnore @@ -0,0 +1 @@ +**/*.log \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3a99027 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module github.com/tecamino/tecamino-logger.git + +go 1.21.0 + +require go.uber.org/zap v1.27.0 + +require go.uber.org/multierr v1.10.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2d29b57 --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +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/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..d6e1fd0 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,62 @@ +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)) + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..249f904 --- /dev/null +++ b/main.go @@ -0,0 +1,14 @@ +package main + +import "github.com/tecamino/tecamino-logger.git/logger" + +func main() { + log, err := logger.NewLogger("Test") + if err != nil { + panic(err) + } + log.Log(logger.Info, "line 10", "hallo") + log.Log(logger.Warning, "line 11", "vello") + log.Log(logger.Error, "line 12", "hie") + log.Log(logger.Debug, "line 13", "isch") +}