mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-04-19 19:59:13 +03:00
Add log levels (#332)
This commit is contained in:
parent
d1cf0ca7eb
commit
3d59c7d0de
5 changed files with 56 additions and 17 deletions
|
@ -60,6 +60,7 @@ Note:
|
|||
| `WGUI_PERSISTENT_KEEPALIVE` | The default persistent keepalive for WireGuard in global settings | `15` |
|
||||
| `WGUI_FORWARD_MARK` | The default WireGuard forward mark | `0xca6c` |
|
||||
| `WGUI_CONFIG_FILE_PATH` | The default WireGuard config file path used in global settings | `/etc/wireguard/wg0.conf` |
|
||||
| `WGUI_LOG_LEVEL` | The default log level. Possible values: `DEBUG`, `INFO`, `WARN`, `ERROR`, `OFF` | `INFO` | |
|
||||
| `WG_CONF_TEMPLATE` | The custom `wg.conf` config file template. Please refer to our [default template](https://github.com/ngoduykhanh/wireguard-ui/blob/master/templates/wg.conf) | N/A |
|
||||
| `EMAIL_FROM_ADDRESS` | The sender email address | N/A |
|
||||
| `EMAIL_FROM_NAME` | The sender name | `WireGuard UI` |
|
||||
|
|
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)
|
||||
|
@ -103,6 +105,7 @@ func init() {
|
|||
//fmt.Println("Session secret\t:", util.SessionSecret)
|
||||
fmt.Println("Custom wg.conf\t:", util.WgConfTemplate)
|
||||
fmt.Println("Base path\t:", util.BasePath+"/")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -118,10 +118,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))
|
||||
|
||||
e.Logger.SetLevel(log.DEBUG)
|
||||
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(middleware.Logger())
|
||||
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,
|
||||
|
|
|
@ -42,6 +42,7 @@ const (
|
|||
PersistentKeepaliveEnvVar = "WGUI_PERSISTENT_KEEPALIVE"
|
||||
ForwardMarkEnvVar = "WGUI_FORWARD_MARK"
|
||||
ConfigFilePathEnvVar = "WGUI_CONFIG_FILE_PATH"
|
||||
LogLevel = "WGUI_LOG_LEVEL"
|
||||
ServerAddressesEnvVar = "WGUI_SERVER_INTERFACE_ADDRESSES"
|
||||
ServerListenPortEnvVar = "WGUI_SERVER_LISTEN_PORT"
|
||||
ServerPostUpScriptEnvVar = "WGUI_SERVER_POST_UP_SCRIPT"
|
||||
|
|
16
util/util.go
16
util/util.go
|
@ -469,6 +469,22 @@ 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)
|
||||
}
|
||||
|
||||
// GetCurrentHash returns current hashes
|
||||
func GetCurrentHash(db store.IStore) (string, string) {
|
||||
hashClients, _ := dirhash.HashDir(path.Join(db.GetPath(), "clients"), "prefix", dirhash.Hash1)
|
||||
|
|
Loading…
Add table
Reference in a new issue