Improve logging

This commit is contained in:
Thorben Günther 2022-10-10 14:55:22 +02:00
parent 5cf4add40b
commit 61ed59cefa
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED

12
main.go
View file

@ -37,12 +37,12 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
if rcv.cfg.User != "" && rcv.cfg.Password != "" { if rcv.cfg.User != "" && rcv.cfg.Password != "" {
user, pass, ok := r.BasicAuth() user, pass, ok := r.BasicAuth()
if !ok { if !ok {
rcv.logger.Error("basic auth failure") rcv.logger.Debug("basic auth failure")
return return
} }
if user != rcv.cfg.User || pass != rcv.cfg.Password { if user != rcv.cfg.User || pass != rcv.cfg.Password {
rcv.logger.Info("basic auth: wrong user or password") rcv.logger.Debug("basic auth: wrong user or password")
return return
} }
} }
@ -50,19 +50,19 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
rcv.logger.Errorf("illegal HTTP method: expected %q, got %q", "POST", r.Method) rcv.logger.Debugf("illegal HTTP method: expected %q, got %q", "POST", r.Method)
return return
} }
contentType := r.Header.Get("Content-Type") contentType := r.Header.Get("Content-Type")
if contentType != "application/json" { if contentType != "application/json" {
rcv.logger.Errorf("illegal content type: %s", contentType) rcv.logger.Debugf("illegal content type: %s", contentType)
return return
} }
var event payload var event payload
if err := json.NewDecoder(r.Body).Decode(&event); err != nil { if err := json.NewDecoder(r.Body).Decode(&event); err != nil {
rcv.logger.Error(err) rcv.logger.Debug(err)
return return
} }
@ -117,6 +117,7 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
rcv.logger.Errorf("ntfy: received status code %d", resp.StatusCode) rcv.logger.Errorf("ntfy: received status code %d", resp.StatusCode)
return
} }
} }
@ -139,5 +140,6 @@ func main() {
receiver := &receiver{cfg: cfg, logger: logger} receiver := &receiver{cfg: cfg, logger: logger}
http.HandleFunc("/", receiver.handleWebhooks) http.HandleFunc("/", receiver.handleWebhooks)
logger.Infof("Listening on %s", cfg.HTTPAddress)
logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil)) logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil))
} }