diff --git a/config/config.go b/config/config.go index dd009a3..d545f5a 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ level = info output = stdout format = json request_logging = true +log_health_checks = true [server] base_path = / @@ -36,10 +37,11 @@ type appConfig struct { } type loggingConfig struct { - Level string `mapstructure:"level"` - Output string `mapstructure:"output"` - Format string `mapstructure:"format"` - RequestLogging bool `mapstructure:"request_logging"` + Level string `mapstructure:"level"` + Output string `mapstructure:"output"` + Format string `mapstructure:"format"` + RequestLogging bool `mapstructure:"request_logging"` + LogHealthChecks bool `mapstructure:"log_health_checks"` } type serverConfig struct { diff --git a/defaults.ini b/defaults.ini index 94424cd..1cbbc38 100644 --- a/defaults.ini +++ b/defaults.ini @@ -20,6 +20,9 @@ destination = stdout # Log all web requests (not just errors) request_logging = true +# Log requests to the health check endpoint (/api/health). Has no effect unless request_logging is enabled +log_health_checks = true + [server] # To serve from a sub path (e.g. in a reverse proxy configuration), specify the sub path here. # If accessed without the sub path, chefbrowser will redirect the user to the sub path. diff --git a/internal/app/app.go b/internal/app/app.go index 838abba..2ff3086 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -41,6 +41,8 @@ func New(cfg *config.Config) { engine.Debug = true } + cfg.Server.BasePath = normalizeBasePath(cfg.Server.BasePath) + engine.Pre(middleware.RemoveTrailingSlashWithConfig(middleware.TrailingSlashConfig{ RedirectCode: http.StatusMovedPermanently, })) @@ -48,8 +50,15 @@ func New(cfg *config.Config) { engine.Use(middleware.Recover()) if cfg.Logging.RequestLogging { - // todo: replace with our own logger - engine.Use(middleware.Logger()) + logger.Debug("request logging is enabled") + logCfg := middleware.DefaultLoggerConfig + if !cfg.Logging.LogHealthChecks { + logger.Debug("log_health_checks = false; requests to health check endpoint will not be logged") + logCfg.Skipper = func(c echo.Context) bool { + return c.Path() == cfg.Server.BasePath+"/api/health" + } + } + engine.Use(middleware.LoggerWithConfig(logCfg)) } if cfg.Server.EnableGzip { @@ -74,8 +83,6 @@ func New(cfg *config.Config) { chefService := chef.New(cfg, logger) - cfg.Server.BasePath = normalizeBasePath(cfg.Server.BasePath) - app := AppService{ Log: logger, Chef: chefService,