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"
"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{},
@ -29,13 +37,13 @@ func (l *Loop) collectLines(ctx context.Context, done chan<- struct{},
line, level := processLogLine(line)
switch level {
case logging.LevelDebug:
case levelDebug:
l.logger.Debug(line)
case logging.LevelInfo:
case levelInfo:
l.logger.Info(line)
case logging.LevelWarn:
case levelWarn:
l.logger.Warn(line)
case logging.LevelError:
case levelError:
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]\] `)
func processLogLine(s string) (filtered string, level logging.Level) {
func processLogLine(s string) (filtered string, level logLevel) {
prefix := unboundPrefix.FindString(s)
filtered = s[len(prefix):]
switch {
case strings.HasPrefix(filtered, "notice: "):
filtered = strings.TrimPrefix(filtered, "notice: ")
level = logging.LevelInfo
level = levelInfo
case strings.HasPrefix(filtered, "info: "):
filtered = strings.TrimPrefix(filtered, "info: ")
level = logging.LevelInfo
level = levelInfo
case strings.HasPrefix(filtered, "warn: "):
filtered = strings.TrimPrefix(filtered, "warn: ")
level = logging.LevelWarn
level = levelWarn
case strings.HasPrefix(filtered, "error: "):
filtered = strings.TrimPrefix(filtered, "error: ")
level = logging.LevelError
level = levelError
default:
level = logging.LevelInfo
level = levelInfo
}
filtered = constants.ColorUnbound().Sprintf(filtered)
return filtered, level

View File

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

View File

@ -5,37 +5,44 @@ import (
"github.com/fatih/color"
"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{
"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",
} {
if s == ignored {
return "", logging.LevelDebug
return "", levelInfo
}
}
switch {
case strings.HasPrefix(s, "NOTE: "):
filtered = strings.TrimPrefix(s, "NOTE: ")
level = logging.LevelInfo
level = levelInfo
case strings.HasPrefix(s, "WARNING: "):
filtered = strings.TrimPrefix(s, "WARNING: ")
level = logging.LevelWarn
level = levelWarn
case strings.HasPrefix(s, "Options error: "):
filtered = strings.TrimPrefix(s, "Options error: ")
level = logging.LevelError
level = levelError
case s == "Initialization Sequence Completed":
return color.HiGreenString(s), logging.LevelInfo
return color.HiGreenString(s), levelInfo
case s == "AUTH: Received control message: AUTH_FAILED":
filtered = s + `
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
filtered = s + `
🚒🚒🚒🚒🚒🚨🚨🚨🚨🚨🚨🚒🚒🚒🚒🚒
@ -50,10 +57,10 @@ That error usually happens because either:
4. Something else https://github.com/qdm12/gluetun/issues/new/choose
`
level = logging.LevelWarn
level = levelWarn
default:
filtered = s
level = logging.LevelInfo
level = levelInfo
}
filtered = constants.ColorOpenvpn().Sprintf(filtered)
return filtered, level

View File

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

View File

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