Maint: pass only single strings to logger methods
- Do not assume formatting from logger's interface - Allow to change golibs in the future to accept only strings for logger methods
This commit is contained in:
@@ -47,7 +47,7 @@ func (h *dnsHandler) getStatus(w http.ResponseWriter) {
|
||||
encoder := json.NewEncoder(w)
|
||||
data := statusWrapper{Status: string(status)}
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func (h *dnsHandler) setStatus(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
encoder := json.NewEncoder(w)
|
||||
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -40,19 +40,19 @@ func (h *handlerV0) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/v1/version", http.StatusPermanentRedirect)
|
||||
case "/openvpn/actions/restart":
|
||||
outcome, _ := h.openvpn.ApplyStatus(h.ctx, constants.Stopped)
|
||||
h.logger.Info("openvpn: %s", outcome)
|
||||
h.logger.Info("openvpn: " + outcome)
|
||||
outcome, _ = h.openvpn.ApplyStatus(h.ctx, constants.Running)
|
||||
h.logger.Info("openvpn: %s", outcome)
|
||||
h.logger.Info("openvpn: " + outcome)
|
||||
if _, err := w.Write([]byte("openvpn restarted, please consider using the /v1/ API in the future.")); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
}
|
||||
case "/unbound/actions/restart":
|
||||
outcome, _ := h.dns.ApplyStatus(h.ctx, constants.Stopped)
|
||||
h.logger.Info("dns: %s", outcome)
|
||||
h.logger.Info("dns: " + outcome)
|
||||
outcome, _ = h.dns.ApplyStatus(h.ctx, constants.Running)
|
||||
h.logger.Info("dns: %s", outcome)
|
||||
h.logger.Info("dns: " + outcome)
|
||||
if _, err := w.Write([]byte("dns restarted, please consider using the /v1/ API in the future.")); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
}
|
||||
case "/openvpn/portforwarded":
|
||||
http.Redirect(w, r, "/v1/openvpn/portforwarded", http.StatusPermanentRedirect)
|
||||
@@ -60,11 +60,11 @@ func (h *handlerV0) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/v1/openvpn/settings", http.StatusPermanentRedirect)
|
||||
case "/updater/restart":
|
||||
outcome, _ := h.updater.SetStatus(h.ctx, constants.Stopped)
|
||||
h.logger.Info("updater: %s", outcome)
|
||||
h.logger.Info("updater: " + outcome)
|
||||
outcome, _ = h.updater.SetStatus(h.ctx, constants.Running)
|
||||
h.logger.Info("updater: %s", outcome)
|
||||
h.logger.Info("updater: " + outcome)
|
||||
if _, err := w.Write([]byte("updater restarted, please consider using the /v1/ API in the future.")); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
}
|
||||
default:
|
||||
http.Error(w, "unversioned API: requested URI not found", http.StatusNotFound)
|
||||
|
||||
@@ -52,7 +52,7 @@ func (h *handlerV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *handlerV1) getVersion(w http.ResponseWriter) {
|
||||
encoder := json.NewEncoder(w)
|
||||
if err := encoder.Encode(h.buildInfo); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -34,8 +35,10 @@ func (m *logMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
statefulWriter := &statefulResponseWriter{httpWriter: w}
|
||||
m.childHandler.ServeHTTP(statefulWriter, r)
|
||||
duration := m.timeNow().Sub(tStart)
|
||||
m.logger.Info("%d %s %s wrote %dB to %s in %s",
|
||||
statefulWriter.statusCode, r.Method, r.RequestURI, statefulWriter.length, r.RemoteAddr, duration)
|
||||
m.logger.Info(strconv.Itoa(statefulWriter.statusCode) + " " +
|
||||
r.Method + " " + r.RequestURI +
|
||||
" wrote " + strconv.Itoa(statefulWriter.length) + "B to " +
|
||||
r.RemoteAddr + " in " + duration.String())
|
||||
}
|
||||
|
||||
func (m *logMiddleware) setEnabled(enabled bool) {
|
||||
|
||||
@@ -61,7 +61,7 @@ func (h *openvpnHandler) getStatus(w http.ResponseWriter) {
|
||||
encoder := json.NewEncoder(w)
|
||||
data := statusWrapper{Status: string(status)}
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func (h *openvpnHandler) setStatus(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
encoder := json.NewEncoder(w)
|
||||
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func (h *openvpnHandler) getSettings(w http.ResponseWriter) {
|
||||
settings.Password = "redacted"
|
||||
encoder := json.NewEncoder(w)
|
||||
if err := encoder.Encode(settings); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -109,7 +109,7 @@ func (h *openvpnHandler) getPortForwarded(w http.ResponseWriter) {
|
||||
encoder := json.NewEncoder(w)
|
||||
data := portWrapper{Port: port}
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func (h *publicIPHandler) getPublicIP(w http.ResponseWriter) {
|
||||
encoder := json.NewEncoder(w)
|
||||
data := publicIPWrapper{PublicIP: publicIP.String()}
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -47,12 +47,12 @@ func (s *server) Run(ctx context.Context, done chan<- struct{}) {
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownGraceDuration)
|
||||
defer cancel()
|
||||
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||
s.logger.Error("failed shutting down: %s", err)
|
||||
s.logger.Error("failed shutting down: " + err.Error())
|
||||
}
|
||||
}()
|
||||
s.logger.Info("listening on %s", s.address)
|
||||
s.logger.Info("listening on " + s.address)
|
||||
err := server.ListenAndServe()
|
||||
if err != nil && errors.Is(ctx.Err(), context.Canceled) {
|
||||
s.logger.Error(err)
|
||||
s.logger.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (h *updaterHandler) getStatus(w http.ResponseWriter) {
|
||||
encoder := json.NewEncoder(w)
|
||||
data := statusWrapper{Status: string(status)}
|
||||
if err := encoder.Encode(data); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -74,7 +74,7 @@ func (h *updaterHandler) setStatus(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
encoder := json.NewEncoder(w)
|
||||
if err := encoder.Encode(outcomeWrapper{Outcome: outcome}); err != nil {
|
||||
h.logger.Warn(err)
|
||||
h.logger.Warn(err.Error())
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user