Maint: package local log levels

This commit is contained in:
Quentin McGaw (desktop)
2021-09-23 17:05:48 +00:00
parent cf95692b93
commit 79f243e98d
5 changed files with 58 additions and 49 deletions

View File

@@ -6,7 +6,15 @@ import (
"strings" "strings"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/golibs/logging" )
type logLevel uint8
const (
levelDebug logLevel = iota
levelInfo
levelWarn
levelError
) )
func (l *Loop) collectLines(ctx context.Context, done chan<- struct{}, func (l *Loop) collectLines(ctx context.Context, done chan<- struct{},
@@ -29,13 +37,13 @@ func (l *Loop) collectLines(ctx context.Context, done chan<- struct{},
line, level := processLogLine(line) line, level := processLogLine(line)
switch level { switch level {
case logging.LevelDebug: case levelDebug:
l.logger.Debug(line) l.logger.Debug(line)
case logging.LevelInfo: case levelInfo:
l.logger.Info(line) l.logger.Info(line)
case logging.LevelWarn: case levelWarn:
l.logger.Warn(line) l.logger.Warn(line)
case logging.LevelError: case levelError:
l.logger.Error(line) l.logger.Error(line)
} }
} }
@@ -43,24 +51,24 @@ func (l *Loop) collectLines(ctx context.Context, done chan<- struct{},
var unboundPrefix = regexp.MustCompile(`\[[0-9]{10}\] unbound\[[0-9]+:[0|1]\] `) var unboundPrefix = regexp.MustCompile(`\[[0-9]{10}\] unbound\[[0-9]+:[0|1]\] `)
func processLogLine(s string) (filtered string, level logging.Level) { func processLogLine(s string) (filtered string, level logLevel) {
prefix := unboundPrefix.FindString(s) prefix := unboundPrefix.FindString(s)
filtered = s[len(prefix):] filtered = s[len(prefix):]
switch { switch {
case strings.HasPrefix(filtered, "notice: "): case strings.HasPrefix(filtered, "notice: "):
filtered = strings.TrimPrefix(filtered, "notice: ") filtered = strings.TrimPrefix(filtered, "notice: ")
level = logging.LevelInfo level = levelInfo
case strings.HasPrefix(filtered, "info: "): case strings.HasPrefix(filtered, "info: "):
filtered = strings.TrimPrefix(filtered, "info: ") filtered = strings.TrimPrefix(filtered, "info: ")
level = logging.LevelInfo level = levelInfo
case strings.HasPrefix(filtered, "warn: "): case strings.HasPrefix(filtered, "warn: "):
filtered = strings.TrimPrefix(filtered, "warn: ") filtered = strings.TrimPrefix(filtered, "warn: ")
level = logging.LevelWarn level = levelWarn
case strings.HasPrefix(filtered, "error: "): case strings.HasPrefix(filtered, "error: "):
filtered = strings.TrimPrefix(filtered, "error: ") filtered = strings.TrimPrefix(filtered, "error: ")
level = logging.LevelError level = levelError
default: default:
level = logging.LevelInfo level = levelInfo
} }
filtered = constants.ColorUnbound().Sprintf(filtered) filtered = constants.ColorUnbound().Sprintf(filtered)
return filtered, level return filtered, level

View File

@@ -3,7 +3,6 @@ package dns
import ( import (
"testing" "testing"
"github.com/qdm12/golibs/logging"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@@ -12,30 +11,30 @@ func Test_processLogLine(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
s string s string
filtered string filtered string
level logging.Level level logLevel
}{ }{
"empty string": {"", "", logging.LevelInfo}, "empty string": {"", "", levelInfo},
"random string": {"asdasqdb", "asdasqdb", logging.LevelInfo}, "random string": {"asdasqdb", "asdasqdb", levelInfo},
"unbound notice": { "unbound notice": {
"[1594595249] unbound[75:0] notice: init module 0: validator", "[1594595249] unbound[75:0] notice: init module 0: validator",
"init module 0: validator", "init module 0: validator",
logging.LevelInfo}, levelInfo},
"unbound info": { "unbound info": {
"[1594595249] unbound[75:0] info: init module 0: validator", "[1594595249] unbound[75:0] info: init module 0: validator",
"init module 0: validator", "init module 0: validator",
logging.LevelInfo}, levelInfo},
"unbound warn": { "unbound warn": {
"[1594595249] unbound[75:0] warn: init module 0: validator", "[1594595249] unbound[75:0] warn: init module 0: validator",
"init module 0: validator", "init module 0: validator",
logging.LevelWarn}, levelWarn},
"unbound error": { "unbound error": {
"[1594595249] unbound[75:0] error: init module 0: validator", "[1594595249] unbound[75:0] error: init module 0: validator",
"init module 0: validator", "init module 0: validator",
logging.LevelError}, levelError},
"unbound unknown": { "unbound unknown": {
"[1594595249] unbound[75:0] BLA: init module 0: validator", "[1594595249] unbound[75:0] BLA: init module 0: validator",
"BLA: init module 0: validator", "BLA: init module 0: validator",
logging.LevelInfo}, levelInfo},
} }
for name, tc := range tests { for name, tc := range tests {
tc := tc tc := tc

View File

@@ -5,37 +5,44 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/golibs/logging"
) )
func processLogLine(s string) (filtered string, level logging.Level) { type logLevel uint8
const (
levelInfo logLevel = iota
levelWarn
levelError
)
func processLogLine(s string) (filtered string, level logLevel) {
for _, ignored := range []string{ for _, ignored := range []string{
"WARNING: you are using user/group/chroot/setcon without persist-tun -- this may cause restarts to fail", "WARNING: you are using user/group/chroot/setcon without persist-tun -- this may cause restarts to fail",
"NOTE: UID/GID downgrade will be delayed because of --client, --pull, or --up-delay", "NOTE: UID/GID downgrade will be delayed because of --client, --pull, or --up-delay",
} { } {
if s == ignored { if s == ignored {
return "", logging.LevelDebug return "", levelInfo
} }
} }
switch { switch {
case strings.HasPrefix(s, "NOTE: "): case strings.HasPrefix(s, "NOTE: "):
filtered = strings.TrimPrefix(s, "NOTE: ") filtered = strings.TrimPrefix(s, "NOTE: ")
level = logging.LevelInfo level = levelInfo
case strings.HasPrefix(s, "WARNING: "): case strings.HasPrefix(s, "WARNING: "):
filtered = strings.TrimPrefix(s, "WARNING: ") filtered = strings.TrimPrefix(s, "WARNING: ")
level = logging.LevelWarn level = levelWarn
case strings.HasPrefix(s, "Options error: "): case strings.HasPrefix(s, "Options error: "):
filtered = strings.TrimPrefix(s, "Options error: ") filtered = strings.TrimPrefix(s, "Options error: ")
level = logging.LevelError level = levelError
case s == "Initialization Sequence Completed": case s == "Initialization Sequence Completed":
return color.HiGreenString(s), logging.LevelInfo return color.HiGreenString(s), levelInfo
case s == "AUTH: Received control message: AUTH_FAILED": case s == "AUTH: Received control message: AUTH_FAILED":
filtered = s + ` filtered = s + `
Your credentials might be wrong 🤨 Your credentials might be wrong 🤨
` `
level = logging.LevelError level = levelError
case strings.Contains(s, "TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)"): //nolint:lll case strings.Contains(s, "TLS Error: TLS key negotiation failed to occur within 60 seconds (check your network connectivity)"): //nolint:lll
filtered = s + ` filtered = s + `
🚒🚒🚒🚒🚒🚨🚨🚨🚨🚨🚨🚒🚒🚒🚒🚒 🚒🚒🚒🚒🚒🚨🚨🚨🚨🚨🚨🚒🚒🚒🚒🚒
@@ -50,10 +57,10 @@ That error usually happens because either:
4. Something else ➡️ https://github.com/qdm12/gluetun/issues/new/choose 4. Something else ➡️ https://github.com/qdm12/gluetun/issues/new/choose
` `
level = logging.LevelWarn level = levelWarn
default: default:
filtered = s filtered = s
level = logging.LevelInfo level = levelInfo
} }
filtered = constants.ColorOpenvpn().Sprintf(filtered) filtered = constants.ColorOpenvpn().Sprintf(filtered)
return filtered, level return filtered, level

View File

@@ -3,7 +3,6 @@ package openvpn
import ( import (
"testing" "testing"
"github.com/qdm12/golibs/logging"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@@ -12,38 +11,38 @@ func Test_processLogLine(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
s string s string
filtered string filtered string
level logging.Level level logLevel
}{ }{
"empty string": {"", "", logging.LevelInfo}, "empty string": {"", "", levelInfo},
"random string": {"asdasqdb", "asdasqdb", logging.LevelInfo}, "random string": {"asdasqdb", "asdasqdb", levelInfo},
"openvpn unknown": { "openvpn unknown": {
"message", "message",
"message", "message",
logging.LevelInfo}, levelInfo},
"openvpn note": { "openvpn note": {
"NOTE: message", "NOTE: message",
"message", "message",
logging.LevelInfo}, levelInfo},
"openvpn warning": { "openvpn warning": {
"WARNING: message", "WARNING: message",
"message", "message",
logging.LevelWarn}, levelWarn},
"openvpn options error": { "openvpn options error": {
"Options error: message", "Options error: message",
"message", "message",
logging.LevelError}, levelError},
"openvpn ignored message": { "openvpn ignored message": {
"NOTE: UID/GID downgrade will be delayed because of --client, --pull, or --up-delay", "NOTE: UID/GID downgrade will be delayed because of --client, --pull, or --up-delay",
"", "",
logging.LevelDebug}, levelInfo},
"openvpn success": { "openvpn success": {
"Initialization Sequence Completed", "Initialization Sequence Completed",
"Initialization Sequence Completed", "Initialization Sequence Completed",
logging.LevelInfo}, levelInfo},
"openvpn auth failed": { "openvpn auth failed": {
"AUTH: Received control message: AUTH_FAILED", "AUTH: Received control message: AUTH_FAILED",
"AUTH: Received control message: AUTH_FAILED\n\nYour credentials might be wrong 🤨\n\n", "AUTH: Received control message: AUTH_FAILED\n\nYour credentials might be wrong 🤨\n\n",
logging.LevelError}, levelError},
} }
for name, tc := range tests { for name, tc := range tests {
tc := tc tc := tc

View File

@@ -3,8 +3,6 @@ package openvpn
import ( import (
"context" "context"
"strings" "strings"
"github.com/qdm12/golibs/logging"
) )
func streamLines(ctx context.Context, done chan<- struct{}, func streamLines(ctx context.Context, done chan<- struct{},
@@ -32,16 +30,14 @@ func streamLines(ctx context.Context, done chan<- struct{},
continue // filtered out continue // filtered out
} }
if errLine { if errLine {
level = logging.LevelError level = levelError
} }
switch level { switch level {
case logging.LevelDebug: case levelInfo:
logger.Debug(line)
case logging.LevelInfo:
logger.Info(line) logger.Info(line)
case logging.LevelWarn: case levelWarn:
logger.Warn(line) logger.Warn(line)
case logging.LevelError: case levelError:
logger.Error(line) logger.Error(line)
} }
if strings.Contains(line, "Initialization Sequence Completed") { if strings.Contains(line, "Initialization Sequence Completed") {