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