mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-07 00:46:58 +03:00
Log server details only if log level is INFO or lower
This commit is contained in:
parent
192d0377c6
commit
8c219712f0
3 changed files with 52 additions and 42 deletions
3
main.go
3
main.go
|
@ -88,6 +88,8 @@ func init() {
|
|||
util.WgConfTemplate = flagWgConfTemplate
|
||||
util.BasePath = util.ParseBasePath(flagBasePath)
|
||||
|
||||
// print only if log level is INFO or lower
|
||||
if lvl, _ := util.ParseLogLevel(util.LookupEnvOrString(util.LogLevel, "INFO")); lvl <= log.INFO {
|
||||
// print app information
|
||||
fmt.Println("Wireguard UI")
|
||||
fmt.Println("App Version\t:", appVersion)
|
||||
|
@ -104,6 +106,7 @@ func init() {
|
|||
fmt.Println("Custom wg.conf\t:", util.WgConfTemplate)
|
||||
fmt.Println("Base path\t:", util.BasePath+"/")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
db, err := jsondb.New("./db")
|
||||
|
|
|
@ -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["about.html"] = template.Must(template.New("about").Funcs(funcs).Parse(tmplBaseString + aboutPageString))
|
||||
|
||||
var lvl log.Lvl
|
||||
switch strings.ToLower(util.LookupEnvOrString(util.LogLevel, "INFO")) {
|
||||
case "debug":
|
||||
lvl = log.DEBUG
|
||||
case "info":
|
||||
lvl = log.INFO
|
||||
case "warn":
|
||||
lvl = log.WARN
|
||||
case "error":
|
||||
lvl = log.ERROR
|
||||
case "off":
|
||||
lvl = log.OFF
|
||||
default:
|
||||
log.Fatalf("not a valid log level: %s", util.LookupEnvOrString(util.LogLevel, "INFO"))
|
||||
lvl, err := util.ParseLogLevel(util.LookupEnvOrString(util.LogLevel, "INFO"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
logConfig := middleware.DefaultLoggerConfig
|
||||
logConfig.Skipper = func(c echo.Context) bool {
|
||||
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 true
|
||||
} else if resp.Status >= 400 && lvl > log.WARN { // do not log if response is 4XX but log level is higher than WARN
|
||||
return true
|
||||
} else if lvl > log.DEBUG { // do not log if log level is higher than DEBUG
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
e.Logger.SetLevel(lvl)
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
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.Use(middleware.LoggerWithConfig(logConfig))
|
||||
e.HideBanner = true
|
||||
e.HidePort = lvl > log.INFO // hide the port output if the log level is higher than INFO
|
||||
e.Validator = NewValidator()
|
||||
e.Renderer = &TemplateRegistry{
|
||||
templates: templates,
|
||||
|
|
17
util/util.go
17
util/util.go
|
@ -462,3 +462,20 @@ func LookupEnvOrStrings(key string, defaultVal []string) []string {
|
|||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue