Log server details only if log level is INFO or lower

This commit is contained in:
ByteDream 2023-02-22 00:33:51 +01:00
parent 192d0377c6
commit 8c219712f0
3 changed files with 52 additions and 42 deletions

33
main.go
View file

@ -88,21 +88,24 @@ func init() {
util.WgConfTemplate = flagWgConfTemplate util.WgConfTemplate = flagWgConfTemplate
util.BasePath = util.ParseBasePath(flagBasePath) util.BasePath = util.ParseBasePath(flagBasePath)
// print app information // print only if log level is INFO or lower
fmt.Println("Wireguard UI") if lvl, _ := util.ParseLogLevel(util.LookupEnvOrString(util.LogLevel, "INFO")); lvl <= log.INFO {
fmt.Println("App Version\t:", appVersion) // print app information
fmt.Println("Git Commit\t:", gitCommit) fmt.Println("Wireguard UI")
fmt.Println("Git Ref\t\t:", gitRef) fmt.Println("App Version\t:", appVersion)
fmt.Println("Build Time\t:", buildTime) fmt.Println("Git Commit\t:", gitCommit)
fmt.Println("Git Repo\t:", "https://github.com/ngoduykhanh/wireguard-ui") fmt.Println("Git Ref\t\t:", gitRef)
fmt.Println("Authentication\t:", !util.DisableLogin) fmt.Println("Build Time\t:", buildTime)
fmt.Println("Bind address\t:", util.BindAddress) fmt.Println("Git Repo\t:", "https://github.com/ngoduykhanh/wireguard-ui")
//fmt.Println("Sendgrid key\t:", util.SendgridApiKey) fmt.Println("Authentication\t:", !util.DisableLogin)
fmt.Println("Email from\t:", util.EmailFrom) fmt.Println("Bind address\t:", util.BindAddress)
fmt.Println("Email from name\t:", util.EmailFromName) //fmt.Println("Sendgrid key\t:", util.SendgridApiKey)
//fmt.Println("Session secret\t:", util.SessionSecret) fmt.Println("Email from\t:", util.EmailFrom)
fmt.Println("Custom wg.conf\t:", util.WgConfTemplate) fmt.Println("Email from name\t:", util.EmailFromName)
fmt.Println("Base path\t:", util.BasePath+"/") //fmt.Println("Session secret\t:", util.SessionSecret)
fmt.Println("Custom wg.conf\t:", util.WgConfTemplate)
fmt.Println("Base path\t:", util.BasePath+"/")
}
} }
func main() { func main() {

View file

@ -112,38 +112,28 @@ func New(tmplBox *rice.Box, extraData map[string]string, secret []byte) *echo.Ec
templates["wake_on_lan_hosts.html"] = template.Must(template.New("wake_on_lan_hosts").Funcs(funcs).Parse(tmplBaseString + tmplWakeOnLanHostsString)) templates["wake_on_lan_hosts.html"] = template.Must(template.New("wake_on_lan_hosts").Funcs(funcs).Parse(tmplBaseString + tmplWakeOnLanHostsString))
templates["about.html"] = template.Must(template.New("about").Funcs(funcs).Parse(tmplBaseString + aboutPageString)) templates["about.html"] = template.Must(template.New("about").Funcs(funcs).Parse(tmplBaseString + aboutPageString))
var lvl log.Lvl lvl, err := util.ParseLogLevel(util.LookupEnvOrString(util.LogLevel, "INFO"))
switch strings.ToLower(util.LookupEnvOrString(util.LogLevel, "INFO")) { if err != nil {
case "debug": log.Fatal(err)
lvl = log.DEBUG }
case "info": logConfig := middleware.DefaultLoggerConfig
lvl = log.INFO logConfig.Skipper = func(c echo.Context) bool {
case "warn": resp := c.Response()
lvl = log.WARN if resp.Status >= 500 && lvl > log.ERROR { // do not log if response is 5XX but log level is higher than ERROR
case "error": return true
lvl = log.ERROR } else if resp.Status >= 400 && lvl > log.WARN { // do not log if response is 4XX but log level is higher than WARN
case "off": return true
lvl = log.OFF } else if lvl > log.DEBUG { // do not log if log level is higher than DEBUG
default: return true
log.Fatalf("not a valid log level: %s", util.LookupEnvOrString(util.LogLevel, "INFO")) }
return false
} }
e.Logger.SetLevel(lvl) e.Logger.SetLevel(lvl)
e.Pre(middleware.RemoveTrailingSlash()) e.Pre(middleware.RemoveTrailingSlash())
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { e.Use(middleware.LoggerWithConfig(logConfig))
return func(c echo.Context) error {
resp := c.Response()
if resp.Status >= 500 && lvl > log.ERROR { // do not log if response is 5XX but log level is higher than ERROR
return next(c)
} else if resp.Status >= 400 && lvl > log.WARN { // do not log if response is 4XX but log level is higher than WARN
return next(c)
} else if lvl > log.DEBUG { // do not log if log level is higher than DEBUG
return next(c)
}
return middleware.Logger()(next)(c)
}
})
e.HideBanner = true e.HideBanner = true
e.HidePort = lvl > log.INFO // hide the port output if the log level is higher than INFO
e.Validator = NewValidator() e.Validator = NewValidator()
e.Renderer = &TemplateRegistry{ e.Renderer = &TemplateRegistry{
templates: templates, templates: templates,

View file

@ -462,3 +462,20 @@ func LookupEnvOrStrings(key string, defaultVal []string) []string {
} }
return defaultVal return defaultVal
} }
func ParseLogLevel(lvl string) (log.Lvl, error) {
switch strings.ToLower(lvl) {
case "debug":
return log.DEBUG, nil
case "info":
return log.INFO, nil
case "warn":
return log.WARN, nil
case "error":
return log.ERROR, nil
case "off":
return log.OFF, nil
default:
return log.DEBUG, fmt.Errorf("not a valid log level: %s", lvl)
}
}