diff --git a/cmd/gluetun/main.go b/cmd/gluetun/main.go index b3d01b8f..58db17c6 100644 --- a/cmd/gluetun/main.go +++ b/cmd/gluetun/main.go @@ -40,12 +40,12 @@ import ( "github.com/qdm12/gluetun/internal/updater" "github.com/qdm12/gluetun/internal/vpn" "github.com/qdm12/golibs/command" - "github.com/qdm12/golibs/logging" "github.com/qdm12/goshutdown" "github.com/qdm12/goshutdown/goroutine" "github.com/qdm12/goshutdown/group" "github.com/qdm12/goshutdown/order" "github.com/qdm12/gosplash" + "github.com/qdm12/log" "github.com/qdm12/updated/pkg/dnscrypto" ) @@ -68,9 +68,7 @@ func main() { signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) ctx, cancel := context.WithCancel(background) - logger := logging.New(logging.Settings{ - Level: logging.LevelInfo, - }) + logger := log.New(log.SetLevel(log.LevelInfo)) args := os.Args tun := tun.New() @@ -125,7 +123,7 @@ var ( //nolint:gocognit,gocyclo,maintidx func _main(ctx context.Context, buildInfo models.BuildInformation, - args []string, logger logging.ParentLogger, source sources.Source, + args []string, logger log.LoggerInterface, source sources.Source, tun tun.Interface, netLinker netlink.NetLinker, cmder command.RunStarter, cli cli.CLIer) error { if len(args) > 1 { // cli operation @@ -175,13 +173,11 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, // - global log level is parsed from source // - firewall Debug and Enabled are booleans parsed from source - logger.PatchLevel(*allSettings.Log.Level) + logger.Patch(log.SetLevel(*allSettings.Log.Level)) - routingLogger := logger.NewChild(logging.Settings{ - Prefix: "routing: ", - }) + routingLogger := logger.New(log.SetComponent("routing")) if *allSettings.Firewall.Debug { // To remove in v4 - routingLogger.PatchLevel(logging.LevelDebug) + routingLogger.Patch(log.SetLevel(log.LevelDebug)) } routingConf := routing.New(netLinker, routingLogger) @@ -195,11 +191,9 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, return err } - firewallLogger := logger.NewChild(logging.Settings{ - Prefix: "firewall: ", - }) + firewallLogger := logger.New(log.SetComponent("firewall")) if *allSettings.Firewall.Debug { // To remove in v4 - firewallLogger.PatchLevel(logging.LevelDebug) + firewallLogger.Patch(log.SetLevel(log.LevelDebug)) } firewallConf, err := firewall.NewConfig(ctx, firewallLogger, cmder, defaultRoutes, localNetworks) @@ -215,7 +209,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, } // TODO run this in a loop or in openvpn to reload from file without restarting - storageLogger := logger.NewChild(logging.Settings{Prefix: "storage: "}) + storageLogger := logger.New(log.SetComponent("storage")) storage, err := storage.New(storageLogger, constants.ServersData) if err != nil { return err @@ -228,7 +222,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, return err } - allSettings.Pprof.HTTPServer.Logger = logger + allSettings.Pprof.HTTPServer.Logger = logger.New(log.SetComponent("pprof")) pprofServer, err := pprof.New(allSettings.Pprof) if err != nil { return fmt.Errorf("cannot create Pprof server: %w", err) @@ -241,7 +235,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, // Create configurators alpineConf := alpine.New() ovpnConf := openvpn.New( - logger.NewChild(logging.Settings{Prefix: "openvpn configurator: "}), + logger.New(log.SetComponent("openvpn configurator")), cmder, puid, pgid) dnsCrypto := dnscrypto.New(httpClient, "", "") const cacertsPath = "/etc/ssl/certs/ca-certificates.crt" @@ -294,9 +288,9 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, return fmt.Errorf("cannot setup routing: %w", err) } defer func() { - logger.Info("routing cleanup...") + routingLogger.Info("routing cleanup...") if err := routingConf.TearDown(); err != nil { - logger.Error("cannot teardown routing: " + err.Error()) + routingLogger.Error("cannot teardown routing: " + err.Error()) } }() @@ -348,14 +342,14 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, otherGroupHandler.Add(pprofHandler) <-pprofReady - portForwardLogger := logger.NewChild(logging.Settings{Prefix: "port forwarding: "}) + portForwardLogger := logger.New(log.SetComponent("port forwarding")) portForwardLooper := portforward.NewLoop(allSettings.VPN.Provider.PortForwarding, httpClient, firewallConf, portForwardLogger) portForwardHandler, portForwardCtx, portForwardDone := goshutdown.NewGoRoutineHandler( "port forwarding", goroutine.OptionTimeout(time.Second)) go portForwardLooper.Run(portForwardCtx, portForwardDone) - unboundLogger := logger.NewChild(logging.Settings{Prefix: "dns over tls: "}) + unboundLogger := logger.New(log.SetComponent("dns over tls")) unboundLooper := dns.NewLoop(dnsConf, allSettings.DNS, httpClient, unboundLogger) dnsHandler, dnsCtx, dnsDone := goshutdown.NewGoRoutineHandler( @@ -370,7 +364,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, controlGroupHandler.Add(dnsTickerHandler) publicIPLooper := publicip.NewLoop(httpClient, - logger.NewChild(logging.Settings{Prefix: "ip getter: "}), + logger.New(log.SetComponent("ip getter")), allSettings.PublicIP, puid, pgid) pubIPHandler, pubIPCtx, pubIPDone := goshutdown.NewGoRoutineHandler( "public IP", goroutine.OptionTimeout(defaultShutdownTimeout)) @@ -382,7 +376,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, go publicIPLooper.RunRestartTicker(pubIPTickerCtx, pubIPTickerDone) tickersGroupHandler.Add(pubIPTickerHandler) - vpnLogger := logger.NewChild(logging.Settings{Prefix: "vpn: "}) + vpnLogger := logger.New(log.SetComponent("vpn")) vpnLooper := vpn.NewLoop(allSettings.VPN, allSettings.Firewall.VPNInputPorts, allServers, ovpnConf, netLinker, firewallConf, routingConf, portForwardLooper, cmder, publicIPLooper, unboundLooper, vpnLogger, httpClient, @@ -393,7 +387,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, updaterLooper := updater.NewLooper(allSettings.Updater, allServers, storage, vpnLooper.SetServers, httpClient, - logger.NewChild(logging.Settings{Prefix: "updater: "})) + logger.New(log.SetComponent("updater"))) updaterHandler, updaterCtx, updaterDone := goshutdown.NewGoRoutineHandler( "updater", goroutine.OptionTimeout(defaultShutdownTimeout)) // wait for updaterLooper.Restart() or its ticket launched with RunRestartTicker @@ -406,7 +400,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, controlGroupHandler.Add(updaterTickerHandler) httpProxyLooper := httpproxy.NewLoop( - logger.NewChild(logging.Settings{Prefix: "http proxy: "}), + logger.New(log.SetComponent("http proxy")), allSettings.HTTPProxy) httpProxyHandler, httpProxyCtx, httpProxyDone := goshutdown.NewGoRoutineHandler( "http proxy", goroutine.OptionTimeout(defaultShutdownTimeout)) @@ -414,7 +408,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, otherGroupHandler.Add(httpProxyHandler) shadowsocksLooper := shadowsocks.NewLooper(allSettings.Shadowsocks, - logger.NewChild(logging.Settings{Prefix: "shadowsocks: "})) + logger.New(log.SetComponent("shadowsocks"))) shadowsocksHandler, shadowsocksCtx, shadowsocksDone := goshutdown.NewGoRoutineHandler( "shadowsocks proxy", goroutine.OptionTimeout(defaultShutdownTimeout)) go shadowsocksLooper.Run(shadowsocksCtx, shadowsocksDone) @@ -425,7 +419,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, httpServerHandler, httpServerCtx, httpServerDone := goshutdown.NewGoRoutineHandler( "http server", goroutine.OptionTimeout(defaultShutdownTimeout)) httpServer, err := server.New(httpServerCtx, controlServerAddress, controlServerLogging, - logger.NewChild(logging.Settings{Prefix: "http server: "}), + logger.New(log.SetComponent("http server")), buildInfo, vpnLooper, portForwardLooper, unboundLooper, updaterLooper, publicIPLooper) if err != nil { return fmt.Errorf("cannot setup control server: %w", err) @@ -435,7 +429,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, <-httpServerReady controlGroupHandler.Add(httpServerHandler) - healthLogger := logger.NewChild(logging.Settings{Prefix: "healthcheck: "}) + healthLogger := logger.New(log.SetComponent("healthcheck")) healthcheckServer := healthcheck.NewServer(allSettings.Health, healthLogger, vpnLooper) healthServerHandler, healthServerCtx, healthServerDone := goshutdown.NewGoRoutineHandler( "HTTP health server", goroutine.OptionTimeout(defaultShutdownTimeout)) diff --git a/go.mod b/go.mod index 68bbc383..e44ecda8 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/qdm12/gosplash v0.1.0 github.com/qdm12/gotree v0.2.0 github.com/qdm12/govalid v0.1.0 + github.com/qdm12/log v0.1.0 github.com/qdm12/ss-server v0.4.0 github.com/qdm12/updated v0.0.0-20210603204757-205acfe6937e github.com/stretchr/testify v1.7.1 diff --git a/go.sum b/go.sum index 7cbdcc56..b69467eb 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,8 @@ github.com/qdm12/gotree v0.2.0 h1:+58ltxkNLUyHtATFereAcOjBVfY6ETqRex8XK90Fb/c= github.com/qdm12/gotree v0.2.0/go.mod h1:1SdFaqKZuI46U1apbXIf25pDMNnrPuYLEqMF/qL4lY4= github.com/qdm12/govalid v0.1.0 h1:UIFVmuaAg0Q+h0GeyfcFEZ5sQ5KJPvRQwycC1/cqDN8= github.com/qdm12/govalid v0.1.0/go.mod h1:CyS/OEQdOvunBgrtIsW93fjd4jBkwZPBjGSpxq3NwA4= +github.com/qdm12/log v0.1.0 h1:jYBd/xscHYpblzZAd2kjZp2YmuYHjAAfbTViJWxoPTw= +github.com/qdm12/log v0.1.0/go.mod h1:Vchi5M8uBvHfPNIblN4mjXn/oSbiWguQIbsgF1zdQPI= github.com/qdm12/ss-server v0.4.0 h1:lMMYfDGc9P86Lyvd3+p8lK4hhgHUKDzjZC91FqJYkDU= github.com/qdm12/ss-server v0.4.0/go.mod h1:AY0p4huvPUPW+/CiWsJcDgT6sneDryk26VXSccPNCxY= github.com/qdm12/updated v0.0.0-20210603204757-205acfe6937e h1:4q+uFLawkaQRq3yARYLsjJPZd2wYwxn4g6G/5v0xW1g= diff --git a/internal/configuration/settings/helpers/copy.go b/internal/configuration/settings/helpers/copy.go index e2937b24..eb8de6c3 100644 --- a/internal/configuration/settings/helpers/copy.go +++ b/internal/configuration/settings/helpers/copy.go @@ -4,7 +4,7 @@ import ( "net" "time" - "github.com/qdm12/golibs/logging" + "github.com/qdm12/log" "inet.af/netaddr" ) @@ -62,11 +62,11 @@ func CopyDurationPtr(original *time.Duration) (copied *time.Duration) { return copied } -func CopyLogLevelPtr(original *logging.Level) (copied *logging.Level) { +func CopyLogLevelPtr(original *log.Level) (copied *log.Level) { if original == nil { return nil } - copied = new(logging.Level) + copied = new(log.Level) *copied = *original return copied } diff --git a/internal/configuration/settings/helpers/default.go b/internal/configuration/settings/helpers/default.go index 944377a5..1458fe70 100644 --- a/internal/configuration/settings/helpers/default.go +++ b/internal/configuration/settings/helpers/default.go @@ -4,7 +4,7 @@ import ( "net" "time" - "github.com/qdm12/golibs/logging" + "github.com/qdm12/log" ) func DefaultInt(existing *int, defaultValue int) ( @@ -74,12 +74,12 @@ func DefaultDuration(existing *time.Duration, return result } -func DefaultLogLevel(existing *logging.Level, - defaultValue logging.Level) (result *logging.Level) { +func DefaultLogLevel(existing *log.Level, + defaultValue log.Level) (result *log.Level) { if existing != nil { return existing } - result = new(logging.Level) + result = new(log.Level) *result = defaultValue return result } diff --git a/internal/configuration/settings/helpers/merge.go b/internal/configuration/settings/helpers/merge.go index 3a109d01..adb24ff2 100644 --- a/internal/configuration/settings/helpers/merge.go +++ b/internal/configuration/settings/helpers/merge.go @@ -5,7 +5,7 @@ import ( "net/http" "time" - "github.com/qdm12/golibs/logging" + "github.com/qdm12/log" "inet.af/netaddr" ) @@ -96,13 +96,13 @@ func MergeWithDuration(existing, other *time.Duration) (result *time.Duration) { return other } -func MergeWithLogLevel(existing, other *logging.Level) (result *logging.Level) { +func MergeWithLogLevel(existing, other *log.Level) (result *log.Level) { if existing != nil { return existing } else if other == nil { return nil } - result = new(logging.Level) + result = new(log.Level) *result = *other return result } diff --git a/internal/configuration/settings/helpers/override.go b/internal/configuration/settings/helpers/override.go index beec8e26..d60d5143 100644 --- a/internal/configuration/settings/helpers/override.go +++ b/internal/configuration/settings/helpers/override.go @@ -5,7 +5,7 @@ import ( "net/http" "time" - "github.com/qdm12/golibs/logging" + "github.com/qdm12/log" "inet.af/netaddr" ) @@ -86,11 +86,11 @@ func OverrideWithDuration(existing, other *time.Duration) (result *time.Duration return result } -func OverrideWithLogLevel(existing, other *logging.Level) (result *logging.Level) { +func OverrideWithLogLevel(existing, other *log.Level) (result *log.Level) { if other == nil { return existing } - result = new(logging.Level) + result = new(log.Level) *result = *other return result } diff --git a/internal/configuration/settings/log.go b/internal/configuration/settings/log.go index a2bf5f15..c4a65759 100644 --- a/internal/configuration/settings/log.go +++ b/internal/configuration/settings/log.go @@ -2,15 +2,15 @@ package settings import ( "github.com/qdm12/gluetun/internal/configuration/settings/helpers" - "github.com/qdm12/golibs/logging" "github.com/qdm12/gotree" + "github.com/qdm12/log" ) // Log contains settings to configure the logger. type Log struct { // Level is the log level of the logger. // It cannot be nil in the internal state. - Level *logging.Level + Level *log.Level } func (l Log) validate() (err error) { @@ -37,7 +37,7 @@ func (l *Log) overrideWith(other Log) { } func (l *Log) setDefaults() { - l.Level = helpers.DefaultLogLevel(l.Level, logging.LevelInfo) + l.Level = helpers.DefaultLogLevel(l.Level, log.LevelInfo) } func (l Log) String() string { diff --git a/internal/configuration/sources/env/log.go b/internal/configuration/sources/env/log.go index 1e45da4a..0ef65338 100644 --- a/internal/configuration/sources/env/log.go +++ b/internal/configuration/sources/env/log.go @@ -7,7 +7,7 @@ import ( "strings" "github.com/qdm12/gluetun/internal/configuration/settings" - "github.com/qdm12/golibs/logging" + "github.com/qdm12/log" ) func readLog() (log settings.Log, err error) { @@ -19,13 +19,13 @@ func readLog() (log settings.Log, err error) { return log, nil } -func readLogLevel() (level *logging.Level, err error) { +func readLogLevel() (level *log.Level, err error) { s := os.Getenv("LOG_LEVEL") if s == "" { return nil, nil //nolint:nilnil } - level = new(logging.Level) + level = new(log.Level) *level, err = parseLogLevel(s) if err != nil { return nil, fmt.Errorf("environment variable LOG_LEVEL: %w", err) @@ -36,16 +36,16 @@ func readLogLevel() (level *logging.Level, err error) { var ErrLogLevelUnknown = errors.New("log level is unknown") -func parseLogLevel(s string) (level logging.Level, err error) { +func parseLogLevel(s string) (level log.Level, err error) { switch strings.ToLower(s) { case "debug": - return logging.LevelDebug, nil + return log.LevelDebug, nil case "info": - return logging.LevelInfo, nil + return log.LevelInfo, nil case "warning": - return logging.LevelWarn, nil + return log.LevelWarn, nil case "error": - return logging.LevelError, nil + return log.LevelError, nil default: return level, fmt.Errorf( "%w: %q is not valid and can be one of debug, info, warning or error", diff --git a/internal/httpserver/run.go b/internal/httpserver/run.go index 0fe61da4..55608287 100644 --- a/internal/httpserver/run.go +++ b/internal/httpserver/run.go @@ -23,7 +23,6 @@ func (s *Server) Run(ctx context.Context, ready chan<- struct{}, done chan<- str return } - s.logger.Warn("http server shutting down: " + ctx.Err().Error()) shutdownCtx, cancel := context.WithTimeout( context.Background(), s.shutdownTimeout) defer cancel() diff --git a/internal/httpserver/run_test.go b/internal/httpserver/run_test.go index 4da72eac..e546b112 100644 --- a/internal/httpserver/run_test.go +++ b/internal/httpserver/run_test.go @@ -17,7 +17,6 @@ func Test_Server_Run_success(t *testing.T) { logger := NewMockLogger(ctrl) logger.EXPECT().Info(newRegexMatcher("^http server listening on 127.0.0.1:[1-9][0-9]{0,4}$")) - logger.EXPECT().Warn("http server shutting down: context canceled") const shutdownTimeout = 10 * time.Second server := &Server{ diff --git a/internal/pprof/server_test.go b/internal/pprof/server_test.go index 0d3cd588..80fd4df9 100644 --- a/internal/pprof/server_test.go +++ b/internal/pprof/server_test.go @@ -23,7 +23,6 @@ func Test_Server(t *testing.T) { logger := NewMockLogger(ctrl) logger.EXPECT().Info(newRegexMatcher("^http server listening on 127.0.0.1:[1-9][0-9]{0,4}$")) - logger.EXPECT().Warn("http server shutting down: context canceled") const httpServerShutdownTimeout = 10 * time.Second // 10s in case test worker is slow settings := Settings{ diff --git a/internal/vpn/loop.go b/internal/vpn/loop.go index fd9276b6..2a43104c 100644 --- a/internal/vpn/loop.go +++ b/internal/vpn/loop.go @@ -17,7 +17,7 @@ import ( "github.com/qdm12/gluetun/internal/routing" "github.com/qdm12/gluetun/internal/vpn/state" "github.com/qdm12/golibs/command" - "github.com/qdm12/golibs/logging" + "github.com/qdm12/log" ) var _ Looper = (*Loop)(nil) @@ -47,7 +47,7 @@ type Loop struct { dnsLooper dns.Looper // Other objects starter command.Starter // for OpenVPN - logger logging.ParentLogger + logger log.LoggerInterface client *http.Client // Internal channels and values stop <-chan struct{} @@ -73,7 +73,7 @@ func NewLoop(vpnSettings settings.VPN, vpnInputPorts []uint16, netLinker netlink.NetLinker, fw firewallConfigurer, routing routing.VPNGetter, portForward portforward.StartStopper, starter command.Starter, publicip publicip.Looper, dnsLooper dns.Looper, - logger logging.ParentLogger, client *http.Client, + logger log.LoggerInterface, client *http.Client, buildInfo models.BuildInformation, versionInfo bool) *Loop { start := make(chan struct{}) running := make(chan models.LoopStatus) diff --git a/internal/vpn/run.go b/internal/vpn/run.go index 161d4855..29e0bec4 100644 --- a/internal/vpn/run.go +++ b/internal/vpn/run.go @@ -6,7 +6,7 @@ import ( "github.com/qdm12/gluetun/internal/constants" "github.com/qdm12/gluetun/internal/provider" - "github.com/qdm12/golibs/logging" + "github.com/qdm12/log" ) type Runner interface { @@ -35,7 +35,7 @@ func (l *Loop) Run(ctx context.Context, done chan<- struct{}) { var vpnRunner vpnRunner var serverName, vpnInterface string var err error - subLogger := l.logger.NewChild(logging.Settings{Prefix: settings.Type + ": "}) + subLogger := l.logger.New(log.SetComponent(settings.Type)) if settings.Type == constants.OpenVPN { vpnInterface = settings.OpenVPN.Interface vpnRunner, serverName, err = setupOpenVPN(ctx, l.fw,