From 6bd4f4c9f95380e54663de000310917b7faa8e81 Mon Sep 17 00:00:00 2001 From: zhashkevych Date: Tue, 21 Sep 2021 10:15:42 +0300 Subject: [PATCH] added logrus --- cmd/main.go | 15 ++++++++++----- go.mod | 1 + go.sum | 2 ++ internal/transport/rest/handler.go | 16 ++++++++++++++-- internal/transport/rest/middleware.go | 9 ++++++--- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 3a58899..1c71bf3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,9 +2,8 @@ package main import ( "fmt" - "log" "net/http" - "time" + "os" "github.com/GOLANG-NINJA/crud-app/internal/config" "github.com/GOLANG-NINJA/crud-app/internal/repository/psql" @@ -13,6 +12,8 @@ import ( "github.com/GOLANG-NINJA/crud-app/pkg/database" _ "github.com/lib/pq" + + log "github.com/sirupsen/logrus" ) const ( @@ -20,14 +21,18 @@ const ( CONFIG_FILE = "main" ) +func init() { + log.SetFormatter(&log.JSONFormatter{}) + log.SetOutput(os.Stdout) + log.SetLevel(log.InfoLevel) +} + func main() { cfg, err := config.New(CONFIG_DIR, CONFIG_FILE) if err != nil { log.Fatal(err) } - log.Printf("config: %+v\n", cfg) - // init db db, err := database.NewPostgresConnection(database.ConnectionInfo{ Host: cfg.DB.Host, @@ -53,7 +58,7 @@ func main() { Handler: handler.InitRouter(), } - log.Println("SERVER STARTED AT", time.Now().Format(time.RFC3339)) + log.Info("SERVER STARTED") if err := srv.ListenAndServe(); err != nil { log.Fatal(err) diff --git a/go.mod b/go.mod index eabcb23..46b2865 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/magiconair/properties v1.8.5 // indirect github.com/mitchellh/mapstructure v1.4.2 // indirect github.com/pelletier/go-toml v1.9.4 // indirect + github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect diff --git a/go.sum b/go.sum index 84a1bb1..877f1bb 100644 --- a/go.sum +++ b/go.sum @@ -229,6 +229,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/sagikazarmark/crypt v0.1.0/go.mod h1:B/mN0msZuINBtQ1zZLEQcegFJJf9vnYIR88KRMEuODE= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= diff --git a/internal/transport/rest/handler.go b/internal/transport/rest/handler.go index 2974b02..47586b0 100644 --- a/internal/transport/rest/handler.go +++ b/internal/transport/rest/handler.go @@ -5,10 +5,11 @@ import ( "encoding/json" "errors" "io/ioutil" - "log" "net/http" "strconv" + log "github.com/sirupsen/logrus" + "github.com/GOLANG-NINJA/crud-app/internal/domain" "github.com/gorilla/mux" @@ -82,19 +83,30 @@ func (h *Handler) getBookByID(w http.ResponseWriter, r *http.Request) { func (h *Handler) createBook(w http.ResponseWriter, r *http.Request) { reqBytes, err := ioutil.ReadAll(r.Body) if err != nil { + log.WithFields(log.Fields{ + "handler": "createBook", + "problem": "reading request body", + }).Error(err) w.WriteHeader(http.StatusBadRequest) return } var book domain.Book if err = json.Unmarshal(reqBytes, &book); err != nil { + log.WithFields(log.Fields{ + "handler": "createBook", + "problem": "unmarshaling request", + }).Error(err) w.WriteHeader(http.StatusBadRequest) return } err = h.booksService.Create(context.TODO(), book) if err != nil { - log.Println("createBook() error:", err) + log.WithFields(log.Fields{ + "handler": "createBook", + "problem": "service error", + }).Error(err) w.WriteHeader(http.StatusInternalServerError) return } diff --git a/internal/transport/rest/middleware.go b/internal/transport/rest/middleware.go index 3f59f35..4b8a53a 100644 --- a/internal/transport/rest/middleware.go +++ b/internal/transport/rest/middleware.go @@ -1,14 +1,17 @@ package rest import ( - "log" "net/http" - "time" + + log "github.com/sirupsen/logrus" ) func loggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - log.Printf("%s: [%s] - %s ", time.Now().Format(time.RFC3339), r.Method, r.RequestURI) + log.WithFields(log.Fields{ + "method": r.Method, + "uri": r.RequestURI, + }).Info() next.ServeHTTP(w, r) }) }