Use structured logging consistently

This commit is contained in:
Jan Dittberner 2023-07-29 18:34:40 +02:00
parent 21c2411cf5
commit 1b0c4a7470
6 changed files with 31 additions and 13 deletions

View file

@ -47,6 +47,12 @@ const (
sessionAuthKeyLength = 64
)
var (
version = "local"
commit = "unknown"
date = "unknown"
)
func main() {
logger := log.New()
@ -76,6 +82,11 @@ func main() {
logger.SetFormatter(&log.JSONFormatter{})
}
logger.WithFields(log.Fields{
"version": version, "commit": commit, "date": date,
}).Info("Starting CAcert OpenID Connect Identity Provider")
logger.Infoln("Server is starting")
bundle, catalog := services.InitI18n(logger, config.Strings("i18n.languages"))
services.AddMessages(catalog)
@ -119,7 +130,7 @@ func main() {
indexHandler, err := handlers.NewIndexHandler(bundle, catalog, ui.Templates, oidcInfo, publicURL)
if err != nil {
logger.Fatalf("could not initialize index handler: %v", err)
logger.WithError(err).Fatal("could not initialize index handler")
}
callbackHandler := handlers.NewCallbackHandler(logger, oidcInfo.KeySet, oidcInfo.OAuth2Config)
@ -145,7 +156,7 @@ func main() {
errorMiddleware, err := handlers.ErrorHandling(logger, ui.Templates, bundle, catalog)
if err != nil {
logger.Fatalf("could not initialize request error handling: %v", err)
logger.WithError(err).Fatal("could not initialize request error handling")
}
tlsConfig := &tls.Config{

View file

@ -32,7 +32,7 @@ type AfterLogoutHandler struct {
func (h *AfterLogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session, err := services.GetSessionStore().Get(r, sessionName)
if err != nil {
h.logger.Errorf("could not get session: %v", err)
h.logger.WithError(err).Error("could not get session")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -41,7 +41,7 @@ func (h *AfterLogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
session.Options.MaxAge = -1
if err = session.Save(r, w); err != nil {
h.logger.Errorf("could not save session: %v", err)
h.logger.WithError(err).Error("could not save session")
}
w.Header().Set("Location", "/")

View file

@ -55,7 +55,7 @@ func StartApplication(
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
logger.Fatalf("Could not gracefully shutdown the server: %v\n", err)
logger.WithError(err).Fatal("Could not gracefully shutdown the server")
}
close(done)
@ -67,7 +67,10 @@ func StartApplication(
if err := server.ListenAndServeTLS(
config.String("server.certificate"), config.String("server.key"),
); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalf("Could not listen on %s: %v\n", server.Addr, err)
logger.WithError(err).WithField(
"server_address",
server.Addr,
).Fatal("Could not listen on requested address")
}
<-done

View file

@ -70,17 +70,18 @@ func (m *MessageCatalog) LookupErrorMessage(
value interface{},
localizer *i18n.Localizer,
) string {
message, ok := m.messages[fmt.Sprintf("%s-%s", field, tag)]
fieldTag := fmt.Sprintf("%s-%s", field, tag)
message, ok := m.messages[fieldTag]
if !ok {
m.logger.Infof("no specific error message %s-%s", field, tag)
m.logger.WithField("field_tag", fieldTag).Info("no specific error message for field and tag")
message, ok = m.messages[tag]
if !ok {
m.logger.Infof("no specific error message %s", tag)
m.logger.WithField("tag", tag).Info("no specific error message for tag")
message, ok = m.messages["unknown"]
if !ok {
m.logger.Warnf("no default translation found")
m.logger.Warn("no default translation found")
return tag
}
@ -94,7 +95,7 @@ func (m *MessageCatalog) LookupErrorMessage(
},
})
if err != nil {
m.logger.Error(err)
m.logger.WithError(err).Error("localization failed")
return tag
}

View file

@ -60,7 +60,10 @@ type OIDCInformation struct {
func DiscoverOIDC(logger *log.Logger, params *OidcParams) (*OIDCInformation, error) {
discoveryURL, err := url.Parse(params.OidcServer)
if err != nil {
logger.Fatalf("could not parse oidc.server parameter value %s: %s", params.OidcServer, err)
logger.WithError(err).WithField(
"oidc.server",
params.OidcServer,
).Fatal("could not parse parameter value")
} else {
discoveryURL.Path = "/.well-known/openid-configuration"
}

View file

@ -32,7 +32,7 @@ func InitSessionStore(logger *log.Logger, sessionPath string, keys ...[]byte) {
if _, err := os.Stat(sessionPath); err != nil {
if os.IsNotExist(err) {
if err = os.MkdirAll(sessionPath, 0700); err != nil { //nolint:gomnd
logger.Fatalf("could not create session store directory: %s", err)
logger.WithError(err).Fatal("could not create session store director")
}
}
}