mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-08 00:56: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.WgConfTemplate = flagWgConfTemplate
|
||||||
util.BasePath = util.ParseBasePath(flagBasePath)
|
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
|
// print app information
|
||||||
fmt.Println("Wireguard UI")
|
fmt.Println("Wireguard UI")
|
||||||
fmt.Println("App Version\t:", appVersion)
|
fmt.Println("App Version\t:", appVersion)
|
||||||
|
@ -104,6 +106,7 @@ func init() {
|
||||||
fmt.Println("Custom wg.conf\t:", util.WgConfTemplate)
|
fmt.Println("Custom wg.conf\t:", util.WgConfTemplate)
|
||||||
fmt.Println("Base path\t:", util.BasePath+"/")
|
fmt.Println("Base path\t:", util.BasePath+"/")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db, err := jsondb.New("./db")
|
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["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,
|
||||||
|
|
17
util/util.go
17
util/util.go
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue