Skip to content

Commit

Permalink
Import log pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarluedke committed Jan 11, 2020
0 parents commit b08d998
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# go-log
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
159 changes: 159 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit b08d998

Please sign in to comment.