Skip to content

Commit

Permalink
allow users to disable health check logging
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhammond committed Nov 3, 2023
1 parent ca54790 commit af408fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ level = info
output = stdout
format = json
request_logging = true
log_health_checks = true
[server]
base_path = /
Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,24 @@ func New(cfg *config.Config) {
engine.Debug = true
}

cfg.Server.BasePath = normalizeBasePath(cfg.Server.BasePath)

engine.Pre(middleware.RemoveTrailingSlashWithConfig(middleware.TrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
}))

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 {
Expand All @@ -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,
Expand Down

0 comments on commit af408fd

Please sign in to comment.