diff --git a/cmd/app/main.go b/cmd/app/main.go index 6cfe281..58cd3e9 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -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{ diff --git a/internal/handlers/after_logout.go b/internal/handlers/after_logout.go index 2b86743..6f087f3 100644 --- a/internal/handlers/after_logout.go +++ b/internal/handlers/after_logout.go @@ -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", "/") diff --git a/internal/handlers/startup.go b/internal/handlers/startup.go index 9cc9660..3497a45 100644 --- a/internal/handlers/startup.go +++ b/internal/handlers/startup.go @@ -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 diff --git a/internal/services/i18n.go b/internal/services/i18n.go index aacae77..e7c0fac 100644 --- a/internal/services/i18n.go +++ b/internal/services/i18n.go @@ -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 } diff --git a/internal/services/oidc.go b/internal/services/oidc.go index 2aab295..40e918d 100644 --- a/internal/services/oidc.go +++ b/internal/services/oidc.go @@ -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" } diff --git a/internal/services/session.go b/internal/services/session.go index 515107a..b8c10f0 100644 --- a/internal/services/session.go +++ b/internal/services/session.go @@ -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") } } }