Skip to content
Snippets Groups Projects
Commit 5c3d3659 authored by gjahn's avatar gjahn
Browse files

Add log level configurable via environment variable

Available levels for now: debug, error. While the former is the
most verbose one and the latter the least verbose.

It sets it to 'debug' if in development mode.
parent 7af81375
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,8 @@ const BODY_SIZE_LIMIT = 32 * 1024 * 1024 // 32 MB, in bytes ...@@ -14,6 +14,8 @@ const BODY_SIZE_LIMIT = 32 * 1024 * 1024 // 32 MB, in bytes
type Config struct { type Config struct {
Version string `env:"VERSION" envDefault:"N/A"` Version string `env:"VERSION" envDefault:"N/A"`
LogLevel string `env:"LOG_LEVE" envDefault:"error"`
Environment string `env:"ENV_NAME" envDefault:"development"` Environment string `env:"ENV_NAME" envDefault:"development"`
Host string `env:"HOST" envDefault:"127.0.0.1"` Host string `env:"HOST" envDefault:"127.0.0.1"`
Port int16 `env:"PORT" envDefault:"3000"` Port int16 `env:"PORT" envDefault:"3000"`
...@@ -44,5 +46,19 @@ func New() ( *Config, error ){ ...@@ -44,5 +46,19 @@ func New() ( *Config, error ){
) )
} }
if cfg.Environment == "development" { cfg.LogLevel = "debug" }
possibleLogLevels := map[ string ] bool {
"error": true,
"debug": true,
}
if _, ok := possibleLogLevels[ cfg.LogLevel ]; !ok {
return nil, errors.New(
fmt.Sprintf( "Invalid log level: %s", cfg.LogLevel ),
)
}
return &cfg, nil return &cfg, nil
} }
...@@ -25,6 +25,18 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, ...@@ -25,6 +25,18 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store,
log.Fatal( err ) log.Fatal( err )
} }
if config.LogLevel == "debug" {
router.All( "*", func( c *f.Ctx ) error {
log.Printf( "%s %s mime:%s agent:%s",
c.Path(),
c.Method(),
c.Get( f.HeaderContentType ),
c.Get( f.HeaderUserAgent ),
)
return c.Next()
})
}
router.Get( "/", func( c *f.Ctx ) error { router.Get( "/", func( c *f.Ctx ) error {
headers := c.GetReqHeaders() headers := c.GetReqHeaders()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment