From b08d9985f0788722fbafe71ecbf044086a62b1c4 Mon Sep 17 00:00:00 2001 From: Josemar Luedke Date: Fri, 10 Jan 2020 17:03:32 -0800 Subject: [PATCH] Import log pkg --- README.md | 1 + go.mod | 9 ++++ go.sum | 20 +++++++ logger.go | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 logger.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..acdd90a --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# go-log diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..da569aa --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/nrfta/go-log + +go 1.12 + +require ( + github.com/go-chi/chi v4.0.3+incompatible + github.com/sirupsen/logrus v1.4.2 + golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6fa0cbc --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +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/go-chi/chi v4.0.3+incompatible h1:gakN3pDJnzZN5jqFV2TEdF66rTfKeITyR8qu6ekICEY= +github.com/go-chi/chi v4.0.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +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/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..8aba070 --- /dev/null +++ b/logger.go @@ -0,0 +1,159 @@ +package log + +import ( + "context" + "io" + "net/http" + "time" + + "github.com/go-chi/chi/middleware" + "github.com/sirupsen/logrus" +) + +//Fields Type to pass when we want to call WithFields for structured logging +type Fields map[string]interface{} + +// Logger interface +type Logger interface { + Info(args ...interface{}) + Infof(message string, args ...interface{}) + Debug(args ...interface{}) + Debugf(message string, args ...interface{}) + Error(args ...interface{}) + Errorf(message string, args ...interface{}) + Warn(args ...interface{}) + Warnf(message string, args ...interface{}) + Fatal(args ...interface{}) + Fatalf(message string, args ...interface{}) + Panic(args ...interface{}) + Panicf(message string, args ...interface{}) + Writer() *io.PipeWriter +} + +var ( + logger *logrus.Logger +) + +func init() { + New(false, "info") +} + +// New - Creates a new instance of logrus with customized configuration +func New(isProduction bool, logLevel string) *logrus.Logger { + var formatter logrus.Formatter + + formatter = &logrus.TextFormatter{ + ForceColors: true, + DisableLevelTruncation: true, + } + + if isProduction { + formatter = &logrus.JSONFormatter{} + } + log := logrus.New() + log.SetFormatter(formatter) + + switch logLevel { + case "panic": + log.SetLevel(logrus.PanicLevel) + case "fatal": + log.SetLevel(logrus.FatalLevel) + case "error": + log.SetLevel(logrus.ErrorLevel) + case "warn": + log.SetLevel(logrus.WarnLevel) + case "info": + log.SetLevel(logrus.InfoLevel) + case "debug": + log.SetLevel(logrus.DebugLevel) + } + + logger = log + return log +} + +// RequestLogger creates a logger with the request ID on it +func RequestLogger(ctx context.Context) Logger { + return logger.WithFields(logrus.Fields{ + "requestID": middleware.GetReqID(ctx), + }) +} + +func Writer() *io.PipeWriter { + return logger.Writer() +} + +func Info(args ...interface{}) { + logger.Info(args...) +} + +func Infof(message string, args ...interface{}) { + logger.Infof(message, args...) +} + +func Debug(args ...interface{}) { + logger.Debug(args...) +} + +func Debugf(message string, args ...interface{}) { + logger.Debugf(message, args...) +} + +func Error(args ...interface{}) { + logger.Error(args...) +} + +func Errorf(message string, args ...interface{}) { + logger.Errorf(message, args...) +} + +func Warn(args ...interface{}) { + logger.Warn(args...) +} + +func Warnf(message string, args ...interface{}) { + logger.Warnf(message, args...) +} + +func Fatal(args ...interface{}) { + logger.Fatal(args...) +} + +func Fatalf(message string, args ...interface{}) { + logger.Fatalf(message, args...) +} + +func Panic(args ...interface{}) { + logger.Panic(args...) +} + +func Panicf(message string, args ...interface{}) { + logger.Panicf(message, args...) +} + +// ServerLogger is a middleware that logs the start and end of each request, along +// with some useful data about what was requested, what the response status was, +// and how long it took to return. +func ServerLogger() func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor) + + t1 := time.Now() + defer func() { + logger.WithFields(logrus.Fields{ + "proto": r.Proto, + "path": r.URL.Path, + "duration": time.Since(t1), + "status": ww.Status(), + "size": ww.BytesWritten(), + "ip": r.RemoteAddr, + "requestID": middleware.GetReqID(r.Context()), + }).Info("Request Served") + }() + + next.ServeHTTP(ww, r) + } + return http.HandlerFunc(fn) + } +}