Switch to a proper logging library

Structured logs can be enabled with `-log.json`.
This commit is contained in:
Conrad Hoffmann 2022-12-01 12:00:42 +01:00
parent 03fce79e57
commit 40bae8dc31
9 changed files with 75 additions and 64 deletions

View file

@ -1,13 +1,12 @@
package auth
import (
"log"
"net/http"
"github.com/rs/zerolog/log"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-sasl"
"git.sr.ht/~sircmpwn/tokidoki/debug"
)
type IMAPProvider struct {
@ -20,7 +19,7 @@ func NewIMAP(addr string, tls bool) AuthProvider {
prov := &IMAPProvider{addr, tls}
conn, err := prov.dial()
if err != nil {
log.Fatalf("Error dialing configured IMAP auth server: %s", err.Error())
log.Fatal().Err(err).Msg("error dialing configured IMAP auth server")
}
conn.Close()
return prov
@ -45,7 +44,7 @@ func (prov *IMAPProvider) doAuth(next http.Handler,
conn, err := prov.dial()
if err != nil {
debug.Printf("Auth dial error: %v", err)
log.Debug().Err(err).Msg("auth dial error")
http.Error(w, "Temporary authentication error, try again later", http.StatusServiceUnavailable)
return
}
@ -53,7 +52,7 @@ func (prov *IMAPProvider) doAuth(next http.Handler,
auth := sasl.NewPlainClient("", user, pass)
if err := conn.Authenticate(auth); err != nil {
debug.Printf("Auth error: %v", err)
log.Debug().Err(err).Msg("auth error")
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}