Skip to content

Commit

Permalink
fix: default log is being used to for request/response logs
Browse files Browse the repository at this point in the history
  • Loading branch information
noxecane committed Aug 14, 2020
1 parent 03fe35a commit ce79bde
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"net/http"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

// SendSuccess sends a JSON success message with status code 200
func SendSuccess(r *http.Request, w http.ResponseWriter, v interface{}) {
raw := getJSON(r, v)
log := zerolog.Ctx(r.Context())
raw := getJSON(log, v)

log.Info().Msg("")

Expand All @@ -23,7 +23,8 @@ func SendSuccess(r *http.Request, w http.ResponseWriter, v interface{}) {

// SendError sends a JSON error message
func SendError(r *http.Request, w http.ResponseWriter, err APIError) {
raw := getJSON(r, err)
log := zerolog.Ctx(r.Context())
raw := getJSON(log, err)

log.Err(err).Msg("")

Expand All @@ -33,11 +34,9 @@ func SendError(r *http.Request, w http.ResponseWriter, err APIError) {
_, _ = w.Write(raw)
}

func getJSON(r *http.Request, v interface{}) []byte {
func getJSON(log *zerolog.Logger, v interface{}) []byte {
raw, _ := json.Marshal(v)

log := zerolog.Ctx(r.Context())

// log API responses
if v != nil {
log.UpdateContext(func(ctx zerolog.Context) zerolog.Context {
Expand Down

0 comments on commit ce79bde

Please sign in to comment.