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