Logging improvements (#195)
- Fix (and test) filtering of lines - Filter out shadowsocks cannot resolve error - Change tinyproxy color - Deduct logging level according to message content
This commit is contained in:
93
internal/logging/line.go
Normal file
93
internal/logging/line.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/qdm12/golibs/logging"
|
||||
"github.com/qdm12/private-internet-access-docker/internal/constants"
|
||||
)
|
||||
|
||||
var regularExpressions = struct { //nolint:gochecknoglobals
|
||||
unboundPrefix *regexp.Regexp
|
||||
shadowsocksPrefix *regexp.Regexp
|
||||
shadowsocksErrorPrefix *regexp.Regexp
|
||||
tinyproxyLoglevel *regexp.Regexp
|
||||
tinyproxyPrefix *regexp.Regexp
|
||||
}{
|
||||
unboundPrefix: regexp.MustCompile(`unbound: \[[0-9]{10}\] unbound\[[0-9]+:0\] `),
|
||||
shadowsocksPrefix: regexp.MustCompile(`shadowsocks:[ ]+2[0-9]{3}\-[0-1][0-9]\-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] `),
|
||||
shadowsocksErrorPrefix: regexp.MustCompile(`shadowsocks error:[ ]+2[0-9]{3}\-[0-1][0-9]\-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] `),
|
||||
tinyproxyLoglevel: regexp.MustCompile(`INFO|CONNECT|NOTICE|WARNING|ERROR|CRITICAL`),
|
||||
tinyproxyPrefix: regexp.MustCompile(`tinyproxy: .+[ ]+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9] \[[0-9]+\]: `),
|
||||
}
|
||||
|
||||
func PostProcessLine(s string) (filtered string, level logging.Level) {
|
||||
switch {
|
||||
case strings.HasPrefix(s, "openvpn: "):
|
||||
filtered = constants.ColorOpenvpn().Sprintf(s)
|
||||
return filtered, logging.InfoLevel
|
||||
case strings.HasPrefix(s, "unbound: "):
|
||||
prefix := regularExpressions.unboundPrefix.FindString(s)
|
||||
filtered = s[len(prefix):]
|
||||
switch {
|
||||
case strings.HasPrefix(filtered, "notice: "):
|
||||
filtered = strings.TrimPrefix(filtered, "notice: ")
|
||||
level = logging.InfoLevel
|
||||
case strings.HasPrefix(filtered, "info: "):
|
||||
filtered = strings.TrimPrefix(filtered, "info: ")
|
||||
level = logging.InfoLevel
|
||||
case strings.HasPrefix(filtered, "warn: "):
|
||||
filtered = strings.TrimPrefix(filtered, "warn: ")
|
||||
level = logging.WarnLevel
|
||||
case strings.HasPrefix(filtered, "error: "):
|
||||
filtered = strings.TrimPrefix(filtered, "error: ")
|
||||
level = logging.ErrorLevel
|
||||
default:
|
||||
level = logging.ErrorLevel
|
||||
}
|
||||
filtered = fmt.Sprintf("unbound: %s", filtered)
|
||||
filtered = constants.ColorUnbound().Sprintf(filtered)
|
||||
return filtered, level
|
||||
case strings.HasPrefix(s, "shadowsocks: "):
|
||||
prefix := regularExpressions.shadowsocksPrefix.FindString(s)
|
||||
filtered = s[len(prefix):]
|
||||
switch {
|
||||
case strings.HasPrefix(filtered, "INFO: "):
|
||||
level = logging.InfoLevel
|
||||
filtered = strings.TrimPrefix(filtered, "INFO: ")
|
||||
default:
|
||||
level = logging.WarnLevel
|
||||
}
|
||||
filtered = fmt.Sprintf("shadowsocks: %s", filtered)
|
||||
filtered = constants.ColorShadowsocks().Sprintf(filtered)
|
||||
return filtered, level
|
||||
case strings.HasPrefix(s, "shadowsocks error: "):
|
||||
if strings.Contains(s, "ERROR: unable to resolve") { // caused by DNS blocking
|
||||
return "", logging.ErrorLevel
|
||||
}
|
||||
prefix := regularExpressions.shadowsocksErrorPrefix.FindString(s)
|
||||
filtered = s[len(prefix):]
|
||||
filtered = strings.TrimPrefix(filtered, "ERROR: ")
|
||||
filtered = fmt.Sprintf("shadowsocks: %s", filtered)
|
||||
filtered = constants.ColorShadowsocksError().Sprintf(filtered)
|
||||
return filtered, logging.ErrorLevel
|
||||
case strings.HasPrefix(s, "tinyproxy: "):
|
||||
logLevel := regularExpressions.tinyproxyLoglevel.FindString(s)
|
||||
prefix := regularExpressions.tinyproxyPrefix.FindString(s)
|
||||
filtered = fmt.Sprintf("tinyproxy: %s", s[len(prefix):])
|
||||
filtered = constants.ColorTinyproxy().Sprintf(filtered)
|
||||
switch logLevel {
|
||||
case "INFO", "CONNECT", "NOTICE":
|
||||
return filtered, logging.InfoLevel
|
||||
case "WARNING":
|
||||
return filtered, logging.WarnLevel
|
||||
case "ERROR", "CRITICAL":
|
||||
return filtered, logging.ErrorLevel
|
||||
default:
|
||||
return filtered, logging.ErrorLevel
|
||||
}
|
||||
}
|
||||
return s, logging.InfoLevel
|
||||
}
|
||||
93
internal/logging/line_test.go
Normal file
93
internal/logging/line_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/qdm12/golibs/logging"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_PostProcessLine(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := map[string]struct {
|
||||
s string
|
||||
filtered string
|
||||
level logging.Level
|
||||
}{
|
||||
"empty string": {"", "", logging.InfoLevel},
|
||||
"random string": {"asdasqdb", "asdasqdb", logging.InfoLevel},
|
||||
"unbound notice": {
|
||||
"unbound: [1594595249] unbound[75:0] notice: init module 0: validator",
|
||||
"unbound: init module 0: validator",
|
||||
logging.InfoLevel},
|
||||
"unbound info": {
|
||||
"unbound: [1594595249] unbound[75:0] info: init module 0: validator",
|
||||
"unbound: init module 0: validator",
|
||||
logging.InfoLevel},
|
||||
"unbound warn": {
|
||||
"unbound: [1594595249] unbound[75:0] warn: init module 0: validator",
|
||||
"unbound: init module 0: validator",
|
||||
logging.WarnLevel},
|
||||
"unbound error": {
|
||||
"unbound: [1594595249] unbound[75:0] error: init module 0: validator",
|
||||
"unbound: init module 0: validator",
|
||||
logging.ErrorLevel},
|
||||
"unbound unknown": {
|
||||
"unbound: [1594595249] unbound[75:0] BLA: init module 0: validator",
|
||||
"unbound: BLA: init module 0: validator",
|
||||
logging.ErrorLevel},
|
||||
"shadowsocks stdout info": {
|
||||
"shadowsocks: 2020-07-12 23:07:25 INFO: UDP relay enabled",
|
||||
"shadowsocks: UDP relay enabled",
|
||||
logging.InfoLevel},
|
||||
"shadowsocks stdout other": {
|
||||
"shadowsocks: 2020-07-12 23:07:25 BLABLA: UDP relay enabled",
|
||||
"shadowsocks: BLABLA: UDP relay enabled",
|
||||
logging.WarnLevel},
|
||||
"shadowsocks stderr": {
|
||||
"shadowsocks error: 2020-07-12 23:07:25 Some error",
|
||||
"shadowsocks: Some error",
|
||||
logging.ErrorLevel},
|
||||
"shadowsocks stderr unable to resolve muted": {
|
||||
"shadowsocks error: 2020-07-12 23:07:25 ERROR: unable to resolve",
|
||||
"",
|
||||
logging.ErrorLevel},
|
||||
"tinyproxy info": {
|
||||
"tinyproxy: INFO Jul 12 23:07:25 [32]: Reloading config file",
|
||||
"tinyproxy: Reloading config file",
|
||||
logging.InfoLevel},
|
||||
"tinyproxy connect": {
|
||||
"tinyproxy: CONNECT Jul 12 23:07:25 [32]: Reloading config file",
|
||||
"tinyproxy: Reloading config file",
|
||||
logging.InfoLevel},
|
||||
"tinyproxy notice": {
|
||||
"tinyproxy: NOTICE Jul 12 23:07:25 [32]: Reloading config file",
|
||||
"tinyproxy: Reloading config file",
|
||||
logging.InfoLevel},
|
||||
"tinyproxy warning": {
|
||||
"tinyproxy: WARNING Jul 12 23:07:25 [32]: Reloading config file",
|
||||
"tinyproxy: Reloading config file",
|
||||
logging.WarnLevel},
|
||||
"tinyproxy error": {
|
||||
"tinyproxy: ERROR Jul 12 23:07:25 [32]: Reloading config file",
|
||||
"tinyproxy: Reloading config file",
|
||||
logging.ErrorLevel},
|
||||
"tinyproxy critical": {
|
||||
"tinyproxy: CRITICAL Jul 12 23:07:25 [32]: Reloading config file",
|
||||
"tinyproxy: Reloading config file",
|
||||
logging.ErrorLevel},
|
||||
"tinyproxy unknown": {
|
||||
"tinyproxy: BLABLA Jul 12 23:07:25 [32]: Reloading config file",
|
||||
"tinyproxy: Reloading config file",
|
||||
logging.ErrorLevel},
|
||||
}
|
||||
for name, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
filtered, level := PostProcessLine(tc.s)
|
||||
assert.Equal(t, tc.filtered, filtered)
|
||||
assert.Equal(t, tc.level, level)
|
||||
})
|
||||
}
|
||||
}
|
||||
59
internal/logging/splash.go
Normal file
59
internal/logging/splash.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kyokomi/emoji"
|
||||
"github.com/qdm12/private-internet-access-docker/internal/constants"
|
||||
)
|
||||
|
||||
// Splash returns the welcome spash message
|
||||
func Splash(version, vcsRef, buildDate string) string {
|
||||
lines := title()
|
||||
lines = append(lines, "")
|
||||
lines = append(lines, fmt.Sprintf("Running version %s built on %s (commit %s)", version, buildDate, vcsRef))
|
||||
lines = append(lines, "")
|
||||
lines = append(lines, announcement()...)
|
||||
lines = append(lines, "")
|
||||
lines = append(lines, links()...)
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func title() []string {
|
||||
return []string{
|
||||
"=========================================",
|
||||
"================ Gluetun ================",
|
||||
"=========================================",
|
||||
"==== A mix of OpenVPN, DNS over TLS, ====",
|
||||
"======= Shadowsocks and Tinyproxy =======",
|
||||
"========= all glued up with Go ==========",
|
||||
"=========================================",
|
||||
"=========== For tunneling to ============",
|
||||
"======== your favorite VPN server =======",
|
||||
"=========================================",
|
||||
"=== Made with " + emoji.Sprint(":heart:") + " by github.com/qdm12 ====",
|
||||
"=========================================",
|
||||
}
|
||||
}
|
||||
|
||||
func announcement() []string {
|
||||
if len(constants.Announcement) == 0 {
|
||||
return nil
|
||||
}
|
||||
expirationDate, _ := time.Parse("2006-01-02", constants.AnnouncementExpiration) // error covered by a unit test
|
||||
if time.Now().After(expirationDate) {
|
||||
return nil
|
||||
}
|
||||
return []string{emoji.Sprint(":mega: ") + constants.Announcement}
|
||||
}
|
||||
|
||||
func links() []string {
|
||||
return []string{
|
||||
emoji.Sprint(":wrench: ") + "Need help? " + constants.IssueLink,
|
||||
emoji.Sprint(":computer: ") + "Email? quentin.mcgaw@gmail.com",
|
||||
emoji.Sprint(":coffee: ") + "Slack? Join from the Slack button on Github",
|
||||
emoji.Sprint(":money_with_wings: ") + "Help me? https://github.com/sponsors/qdm12",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user