feat(log): use github.com/qdm12/log library
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
16
internal/configuration/sources/env/log.go
vendored
16
internal/configuration/sources/env/log.go
vendored
@@ -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",
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user