Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging w/ logrus #2

Open
wants to merge 1 commit into
base: config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -13,21 +12,27 @@ import (
"github.com/GOLANG-NINJA/crud-app/pkg/database"

_ "github.com/lib/pq"

log "github.com/sirupsen/logrus"
)

const (
CONFIG_DIR = "configs"
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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
16 changes: 14 additions & 2 deletions internal/transport/rest/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 6 additions & 3 deletions internal/transport/rest/middleware.go
Original file line number Diff line number Diff line change
@@ -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)
})
}