Implemented logger, write to file on disk

This commit is contained in:
2022-12-17 14:22:56 +01:00
parent 4c1686cf49
commit 576a4e1579
6 changed files with 160 additions and 28 deletions

View File

@@ -2,15 +2,16 @@ package handler
import (
"encoding/json"
log "github.com/kevinmidboe/planetposen-images/logger"
"net/http"
log "github.com/sirupsen/logrus"
)
var logger = log.InitLogger()
// handleError - Logs the error (if shouldLog is true), and outputs the error message (msg)
func handleError(w http.ResponseWriter, err error, msg string, statusCode int, shouldLog bool) {
if shouldLog {
log.WithField("err", err).Error(msg)
logger.Error(msg, err)
}
w.Header().Set("Content-Type", "application/json")
@@ -21,4 +22,19 @@ func handleError(w http.ResponseWriter, err error, msg string, statusCode int, s
Error: msg,
})
w.Write(errorJSON)
}
}
func handleGoogleApiError(w http.ResponseWriter, err error, msg string, statusCode int, shouldLog bool) {
if shouldLog {
logger.GoogleApiError(msg, err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
errorJSON, _ := json.Marshal(struct {
Error string `json:"error"`
}{
Error: msg,
})
w.Write(errorJSON)
}