Use structured logging consistently
This commit is contained in:
parent
21c2411cf5
commit
1b0c4a7470
6 changed files with 31 additions and 13 deletions
|
@ -47,6 +47,12 @@ const (
|
||||||
sessionAuthKeyLength = 64
|
sessionAuthKeyLength = 64
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
version = "local"
|
||||||
|
commit = "unknown"
|
||||||
|
date = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
logger := log.New()
|
logger := log.New()
|
||||||
|
|
||||||
|
@ -76,6 +82,11 @@ func main() {
|
||||||
logger.SetFormatter(&log.JSONFormatter{})
|
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"))
|
bundle, catalog := services.InitI18n(logger, config.Strings("i18n.languages"))
|
||||||
|
|
||||||
services.AddMessages(catalog)
|
services.AddMessages(catalog)
|
||||||
|
@ -119,7 +130,7 @@ func main() {
|
||||||
|
|
||||||
indexHandler, err := handlers.NewIndexHandler(bundle, catalog, ui.Templates, oidcInfo, publicURL)
|
indexHandler, err := handlers.NewIndexHandler(bundle, catalog, ui.Templates, oidcInfo, publicURL)
|
||||||
if err != nil {
|
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)
|
callbackHandler := handlers.NewCallbackHandler(logger, oidcInfo.KeySet, oidcInfo.OAuth2Config)
|
||||||
|
@ -145,7 +156,7 @@ func main() {
|
||||||
|
|
||||||
errorMiddleware, err := handlers.ErrorHandling(logger, ui.Templates, bundle, catalog)
|
errorMiddleware, err := handlers.ErrorHandling(logger, ui.Templates, bundle, catalog)
|
||||||
if err != nil {
|
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{
|
tlsConfig := &tls.Config{
|
||||||
|
|
|
@ -32,7 +32,7 @@ type AfterLogoutHandler struct {
|
||||||
func (h *AfterLogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *AfterLogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := services.GetSessionStore().Get(r, sessionName)
|
session, err := services.GetSessionStore().Get(r, sessionName)
|
||||||
if err != nil {
|
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)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -41,7 +41,7 @@ func (h *AfterLogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
session.Options.MaxAge = -1
|
session.Options.MaxAge = -1
|
||||||
|
|
||||||
if err = session.Save(r, w); err != nil {
|
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", "/")
|
w.Header().Set("Location", "/")
|
||||||
|
|
|
@ -55,7 +55,7 @@ func StartApplication(
|
||||||
server.SetKeepAlivesEnabled(false)
|
server.SetKeepAlivesEnabled(false)
|
||||||
|
|
||||||
if err := server.Shutdown(ctx); err != nil {
|
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)
|
close(done)
|
||||||
|
@ -67,7 +67,10 @@ func StartApplication(
|
||||||
if err := server.ListenAndServeTLS(
|
if err := server.ListenAndServeTLS(
|
||||||
config.String("server.certificate"), config.String("server.key"),
|
config.String("server.certificate"), config.String("server.key"),
|
||||||
); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
); 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
|
<-done
|
||||||
|
|
|
@ -70,17 +70,18 @@ func (m *MessageCatalog) LookupErrorMessage(
|
||||||
value interface{},
|
value interface{},
|
||||||
localizer *i18n.Localizer,
|
localizer *i18n.Localizer,
|
||||||
) string {
|
) 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 {
|
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]
|
message, ok = m.messages[tag]
|
||||||
if !ok {
|
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"]
|
message, ok = m.messages["unknown"]
|
||||||
if !ok {
|
if !ok {
|
||||||
m.logger.Warnf("no default translation found")
|
m.logger.Warn("no default translation found")
|
||||||
|
|
||||||
return tag
|
return tag
|
||||||
}
|
}
|
||||||
|
@ -94,7 +95,7 @@ func (m *MessageCatalog) LookupErrorMessage(
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.logger.Error(err)
|
m.logger.WithError(err).Error("localization failed")
|
||||||
|
|
||||||
return tag
|
return tag
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,10 @@ type OIDCInformation struct {
|
||||||
func DiscoverOIDC(logger *log.Logger, params *OidcParams) (*OIDCInformation, error) {
|
func DiscoverOIDC(logger *log.Logger, params *OidcParams) (*OIDCInformation, error) {
|
||||||
discoveryURL, err := url.Parse(params.OidcServer)
|
discoveryURL, err := url.Parse(params.OidcServer)
|
||||||
if err != nil {
|
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 {
|
} else {
|
||||||
discoveryURL.Path = "/.well-known/openid-configuration"
|
discoveryURL.Path = "/.well-known/openid-configuration"
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ func InitSessionStore(logger *log.Logger, sessionPath string, keys ...[]byte) {
|
||||||
if _, err := os.Stat(sessionPath); err != nil {
|
if _, err := os.Stat(sessionPath); err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
if err = os.MkdirAll(sessionPath, 0700); err != nil { //nolint:gomnd
|
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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue