From c2666086cbf68df5140a3096922af054a39c5b47 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 22 May 2020 16:03:59 +0200 Subject: [PATCH] Use structured logging for demo app --- cmd/server/container.go | 9 +++--- cmd/server/main.go | 68 +++++++++++++++++++++++++++++++++------ internal/config/config.go | 11 +++++++ internal/route/logout.go | 10 +++++- 4 files changed, 83 insertions(+), 15 deletions(-) diff --git a/cmd/server/container.go b/cmd/server/container.go index dd30acb..c806e02 100644 --- a/cmd/server/container.go +++ b/cmd/server/container.go @@ -2,9 +2,9 @@ package main import ( "context" - "log" "net/http" + "gitlab.com/wpetit/goweb/logger" "gitlab.com/wpetit/goweb/template/html" oidc "forge.cadoles.com/wpetit/goweb-oidc" @@ -18,7 +18,7 @@ import ( "gitlab.com/wpetit/goweb/session/gorilla" ) -func getServiceContainer(conf *config.Config) (*service.Container, error) { +func getServiceContainer(ctx context.Context, conf *config.Config) (*service.Container, error) { // Initialize and configure service container ctn := service.NewContainer() @@ -26,7 +26,7 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) { // Generate random cookie authentication key if none is set if conf.HTTP.CookieAuthenticationKey == "" { - log.Println("could not find cookie authentication key. generating one...") + logger.Info(ctx, "could not find cookie authentication key. generating one...") cookieAuthenticationKey, err := gorilla.GenerateRandomBytes(64) if err != nil { @@ -38,7 +38,7 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) { // Generate random cookie encryption key if none is set if conf.HTTP.CookieEncryptionKey == "" { - log.Println("could not find cookie encryption key. generating one...") + logger.Info(ctx, "could not find cookie encryption key. generating one...") cookieEncryptionKey, err := gorilla.GenerateRandomBytes(32) if err != nil { @@ -75,7 +75,6 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) { // Create and expose config service provider ctn.Provide(config.ServiceName, config.ServiceProvider(conf)) - ctx := context.Background() provider, err := oidc.NewProvider(ctx, conf.OIDC.IssuerURL) if err != nil { return nil, errors.Wrap(err, "could not create oidc provider") diff --git a/cmd/server/main.go b/cmd/server/main.go index f6555cd..d8e3c5b 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "net/http" "forge.cadoles.com/wpetit/goweb-oidc/internal/config" @@ -17,6 +18,7 @@ import ( "os" "github.com/pkg/errors" + "gitlab.com/wpetit/goweb/logger" ) //nolint: gochecknoglobals @@ -43,6 +45,8 @@ func init() { } func main() { + ctx := context.Background() + flag.Parse() if version { @@ -54,10 +58,23 @@ func main() { // Switch to new working directory if defined if workdir != "" { if err := os.Chdir(workdir); err != nil { - log.Fatalf("%+v", errors.Wrapf(err, "could not change working directory to '%s'", workdir)) + logger.Fatal( + ctx, + "could not change working directory", + logger.E(err), + logger.F("workdir", workdir), + ) } } + logger.Info( + ctx, + "starting", + logger.F("gitRef", GitRef), + logger.F("projectVersion", ProjectVersion), + logger.F("buildDate", BuildDate), + ) + // Load configuration file if defined, use default configuration otherwise var conf *config.Config @@ -66,7 +83,13 @@ func main() { if configFile != "" { conf, err = config.NewFromFile(configFile) if err != nil { - log.Fatalf("%+v", errors.Wrapf(err, "could not load config file '%s'", configFile)) + log.Fatalf("%+v", errors.Wrapf(err, " '%s'", configFile)) + logger.Fatal( + ctx, + "could not load config file", + logger.E(err), + logger.F("configFile", configFile), + ) } } else { if dumpConfig { @@ -80,20 +103,38 @@ func main() { // Dump configuration if asked if dumpConfig { if err := config.Dump(conf, os.Stdout); err != nil { - log.Fatalf("%+v", errors.Wrap(err, "could not dump config")) + logger.Fatal( + ctx, + "could not dump config", + logger.E(err), + ) } os.Exit(0) } if err := config.WithEnvironment(conf); err != nil { - log.Fatalf("%+v", errors.Wrap(err, "could not override config with environment")) + logger.Fatal( + ctx, + "could not override config with environment", + logger.E(err), + ) } + logger.Debug(ctx, "setting log format", logger.F("format", conf.Log.Format)) + logger.SetFormat(conf.Log.Format) + + logger.Debug(ctx, "setting log level", logger.F("level", conf.Log.Level.String())) + logger.SetLevel(conf.Log.Level) + // Create service container - ctn, err := getServiceContainer(conf) + ctn, err := getServiceContainer(ctx, conf) if err != nil { - log.Fatalf("%+v", errors.Wrap(err, "could not create service container")) + logger.Fatal( + ctx, + "could not create service container", + logger.E(err), + ) } r := chi.NewRouter() @@ -107,11 +148,20 @@ func main() { // Define routes if err := route.Mount(r, conf); err != nil { - log.Fatalf("%+v", errors.Wrap(err, "could not mount http routes")) + logger.Fatal( + ctx, + "could not mount http routes", + logger.E(err), + ) } - log.Printf("listening on '%s'", conf.HTTP.Address) + logger.Info(ctx, "listening", logger.F("address", conf.HTTP.Address)) if err := http.ListenAndServe(conf.HTTP.Address, r); err != nil { - log.Fatalf("%+v", errors.Wrapf(err, "could not listen on '%s'", conf.HTTP.Address)) + logger.Fatal( + ctx, + "could not listen", + logger.E(err), + logger.F("address", conf.HTTP.Address), + ) } } diff --git a/internal/config/config.go b/internal/config/config.go index 6f4074e..22a68fc 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,12 +6,14 @@ import ( "time" "github.com/pkg/errors" + "gitlab.com/wpetit/goweb/logger" "github.com/caarlos0/env/v6" "gopkg.in/yaml.v2" ) type Config struct { + Log LogConfig `yaml:"log"` HTTP HTTPConfig `yaml:"http"` OIDC OIDCConfig `yaml:"oidc"` } @@ -49,6 +51,11 @@ type OIDCConfig struct { PostLogoutRedirectURL string `yaml:"postLogoutRedirectURL" env:"OIDC_POST_LOGOUT_REDIRECT_URL"` } +type LogConfig struct { + Level logger.Level `yaml:"level" env:"LOG_LEVEL"` + Format logger.Format `yaml:"format" env:"LOG_FORMAT"` +} + func NewDumpDefault() *Config { config := NewDefault() return config @@ -56,6 +63,10 @@ func NewDumpDefault() *Config { func NewDefault() *Config { return &Config{ + Log: LogConfig{ + Level: logger.LevelWarn, + Format: logger.FormatHuman, + }, HTTP: HTTPConfig{ Address: ":3002", CookieAuthenticationKey: "", diff --git a/internal/route/logout.go b/internal/route/logout.go index e252b19..c40d0e3 100644 --- a/internal/route/logout.go +++ b/internal/route/logout.go @@ -6,12 +6,14 @@ import ( oidc "forge.cadoles.com/wpetit/goweb-oidc" "forge.cadoles.com/wpetit/goweb-oidc/internal/config" "github.com/pkg/errors" + "gitlab.com/wpetit/goweb/logger" "gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/service/session" ) func handleLogout(w http.ResponseWriter, r *http.Request) { - ctn := container.Must(r.Context()) + ctx := r.Context() + ctn := container.Must(ctx) conf := config.Must(ctn) sess, err := session.Must(ctn).Get(w, r) @@ -25,5 +27,11 @@ func handleLogout(w http.ResponseWriter, r *http.Request) { client := oidc.Must(ctn) + logger.Info( + ctx, + "logging out user", + logger.F("postLogoutURL", conf.OIDC.PostLogoutRedirectURL), + ) + client.Logout(w, r, conf.OIDC.PostLogoutRedirectURL) }