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_PERSISTENT_KEEPALIVE` | The default persistent keepalive for WireGuard in global settings | `15` |
|
||||||
| `WGUI_FORWARD_MARK` | The default WireGuard forward mark | `0xca6c` |
|
| `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_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 |
|
| `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_ADDRESS` | The sender email address | N/A |
|
||||||
| `EMAIL_FROM_NAME` | The sender name | `WireGuard UI` |
|
| `EMAIL_FROM_NAME` | The sender name | `WireGuard UI` |
|
||||||
|
|
33
main.go
33
main.go
|
@ -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() {
|
||||||
|
|
|
@ -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["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))
|
||||||
|
|
||||||
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.Pre(middleware.RemoveTrailingSlash())
|
||||||
e.Use(middleware.Logger())
|
e.Use(middleware.LoggerWithConfig(logConfig))
|
||||||
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,
|
||||||
|
|
|
@ -42,6 +42,7 @@ const (
|
||||||
PersistentKeepaliveEnvVar = "WGUI_PERSISTENT_KEEPALIVE"
|
PersistentKeepaliveEnvVar = "WGUI_PERSISTENT_KEEPALIVE"
|
||||||
ForwardMarkEnvVar = "WGUI_FORWARD_MARK"
|
ForwardMarkEnvVar = "WGUI_FORWARD_MARK"
|
||||||
ConfigFilePathEnvVar = "WGUI_CONFIG_FILE_PATH"
|
ConfigFilePathEnvVar = "WGUI_CONFIG_FILE_PATH"
|
||||||
|
LogLevel = "WGUI_LOG_LEVEL"
|
||||||
ServerAddressesEnvVar = "WGUI_SERVER_INTERFACE_ADDRESSES"
|
ServerAddressesEnvVar = "WGUI_SERVER_INTERFACE_ADDRESSES"
|
||||||
ServerListenPortEnvVar = "WGUI_SERVER_LISTEN_PORT"
|
ServerListenPortEnvVar = "WGUI_SERVER_LISTEN_PORT"
|
||||||
ServerPostUpScriptEnvVar = "WGUI_SERVER_POST_UP_SCRIPT"
|
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
|
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
|
// GetCurrentHash returns current hashes
|
||||||
func GetCurrentHash(db store.IStore) (string, string) {
|
func GetCurrentHash(db store.IStore) (string, string) {
|
||||||
hashClients, _ := dirhash.HashDir(path.Join(db.GetPath(), "clients"), "prefix", dirhash.Hash1)
|
hashClients, _ := dirhash.HashDir(path.Join(db.GetPath(), "clients"), "prefix", dirhash.Hash1)
|
||||||
|
|
Loading…
Add table
Reference in a new issue