diff --git a/cmd/sowerd/main.go b/cmd/sowerd/main.go index 6cebaa1..1d32030 100644 --- a/cmd/sowerd/main.go +++ b/cmd/sowerd/main.go @@ -10,7 +10,7 @@ import ( "github.com/cristalhq/aconfig" "github.com/rs/zerolog/log" - "github.com/wweir/sower/pkg/deferlog" + "github.com/wweir/deferlog" "github.com/wweir/sower/pkg/teeconn" "github.com/wweir/sower/transport/sower" "github.com/wweir/sower/transport/trojan" diff --git a/go.mod b/go.mod index 5807651..8569526 100644 --- a/go.mod +++ b/go.mod @@ -14,5 +14,6 @@ require ( github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.23.0 github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6 + github.com/wweir/deferlog v0.0.1 golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 ) diff --git a/go.sum b/go.sum index d6f65aa..1600c73 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6 h1:TtyC78WMafNW8QFfv3TeP3yWNDG+uxNkk9vOrnDu6JA= github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6/go.mod h1:h8272+G2omSmi30fBXiZDMkmHuOgonplfKIKjQWzlfs= +github.com/wweir/deferlog v0.0.1 h1:KKWFRsS46KTqkAR4F79VtZ4h7Q6PvnbrPkBdUxEUujc= +github.com/wweir/deferlog v0.0.1/go.mod h1:mIpuA0PTdQ28oas0iR1uZYlFiDyOxMOXQgmx6gNOg3I= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= diff --git a/pkg/deferlog/deferlog.go b/pkg/deferlog/deferlog.go deleted file mode 100644 index abcaa0d..0000000 --- a/pkg/deferlog/deferlog.go +++ /dev/null @@ -1,131 +0,0 @@ -// Package deferlog is a copy of zerolog/log/log.go for log in defer -package deferlog - -import ( - "context" - "fmt" - "io" - "os" - - "github.com/rs/zerolog" -) - -// Logger is the global logger. -var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger() - -// Output duplicates the global logger and sets w as its output. -func Output(w io.Writer) zerolog.Logger { - return Logger.Output(w) -} - -// With creates a child logger with the field added to its context. -func With() zerolog.Context { - return Logger.With() -} - -// Level creates a child logger with the minimum accepted level set to level. -func Level(level zerolog.Level) zerolog.Logger { - return Logger.Level(level) -} - -// Sample returns a logger with the s sampler. -func Sample(s zerolog.Sampler) zerolog.Logger { - return Logger.Sample(s) -} - -// Hook returns a logger with the h Hook. -func Hook(h zerolog.Hook) zerolog.Logger { - return Logger.Hook(h) -} - -// Err starts a new message with error level with err as a field if not nil or -// with info level if err is nil. -// -// You must call Msg on the returned event in order to send the event. -func Err(err error) *zerolog.Event { - return Logger.Err(err) -} - -// Trace starts a new message with trace level. -// -// You must call Msg on the returned event in order to send the event. -func Trace() *zerolog.Event { - return Logger.Trace() -} - -// Debug starts a new message with debug level. -// -// You must call Msg on the returned event in order to send the event. -func Debug() *zerolog.Event { - return Logger.Debug() -} - -// Info starts a new message with info level. -// -// You must call Msg on the returned event in order to send the event. -func Info() *zerolog.Event { - return Logger.Info() -} - -// Warn starts a new message with warn level. -// -// You must call Msg on the returned event in order to send the event. -func Warn() *zerolog.Event { - return Logger.Warn() -} - -// Error starts a new message with error level. -// -// You must call Msg on the returned event in order to send the event. -func Error() *zerolog.Event { - return Logger.Error() -} - -// Fatal starts a new message with fatal level. The os.Exit(1) function -// is called by the Msg method. -// -// You must call Msg on the returned event in order to send the event. -func Fatal() *zerolog.Event { - return Logger.Fatal() -} - -// Panic starts a new message with panic level. The message is also sent -// to the panic function. -// -// You must call Msg on the returned event in order to send the event. -func Panic() *zerolog.Event { - return Logger.Panic() -} - -// WithLevel starts a new message with level. -// -// You must call Msg on the returned event in order to send the event. -func WithLevel(level zerolog.Level) *zerolog.Event { - return Logger.WithLevel(level) -} - -// Log starts a new message with no level. Setting zerolog.GlobalLevel to -// zerolog.Disabled will still disable events produced by this method. -// -// You must call Msg on the returned event in order to send the event. -func Log() *zerolog.Event { - return Logger.Log() -} - -// Print sends a log event using debug level and no extra field. -// Arguments are handled in the manner of fmt.Print. -func Print(v ...interface{}) { - Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...)) -} - -// Printf sends a log event using debug level and no extra field. -// Arguments are handled in the manner of fmt.Printf. -func Printf(format string, v ...interface{}) { - Logger.Debug().CallerSkipFrame(1).Msgf(format, v...) -} - -// Ctx returns the Logger associated with the ctx. If no logger -// is associated, a disabled logger is returned. -func Ctx(ctx context.Context) *zerolog.Logger { - return zerolog.Ctx(ctx) -} diff --git a/pkg/deferlog/enhance.go b/pkg/deferlog/enhance.go deleted file mode 100644 index 49d1602..0000000 --- a/pkg/deferlog/enhance.go +++ /dev/null @@ -1,88 +0,0 @@ -package deferlog - -import ( - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" -) - -func InfoWarn(err error) *zerolog.Event { - if err != nil { - return Logger.Warn().Err(err) - } - - return Logger.Info() -} -func InfoFatal(err error) *zerolog.Event { - if err != nil { - return Logger.Fatal().Err(err) - } - - return Logger.Info() -} - -func DebugWarn(err error) *zerolog.Event { - if err != nil { - return Logger.Warn().Err(err) - } - - return Logger.Debug() -} -func DebugError(err error) *zerolog.Event { - if err != nil { - return Logger.Error().Err(err) - } - - return Logger.Debug() -} -func DebugFatal(err error) *zerolog.Event { - if err != nil { - return Logger.Fatal().Err(err) - } - - return Logger.Debug() -} - -type StdLogger struct { - *zerolog.Logger -} - -var Std = StdLogger{ - Logger: &log.Logger, -} - -func (std *StdLogger) InfoWarn(err error) *zerolog.Event { - if err != nil { - return Std.Logger.Warn().Err(err) - } - - return Std.Logger.Info() -} -func (std *StdLogger) InfoFatal(err error) *zerolog.Event { - if err != nil { - return Std.Logger.Fatal().Err(err) - } - - return Std.Logger.Info() -} - -func (std *StdLogger) DebugWarn(err error) *zerolog.Event { - if err != nil { - return Std.Logger.Warn().Err(err) - } - - return Std.Logger.Debug() -} -func (std *StdLogger) DebugError(err error) *zerolog.Event { - if err != nil { - return Std.Logger.Error().Err(err) - } - - return Std.Logger.Debug() -} -func (std *StdLogger) DebugFatal(err error) *zerolog.Event { - if err != nil { - return Std.Logger.Fatal().Err(err) - } - - return Std.Logger.Debug() -} diff --git a/pkg/deferlog/init.go b/pkg/deferlog/init.go deleted file mode 100644 index 4195003..0000000 --- a/pkg/deferlog/init.go +++ /dev/null @@ -1,51 +0,0 @@ -package deferlog - -import ( - "os" - "strconv" - "strings" - "time" - - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - "github.com/rs/zerolog/pkgerrors" -) - -var StructLogger = zerolog.New(os.Stdout). - With().Timestamp().Logger() - -var ConsoleLogger = zerolog.New(zerolog.ConsoleWriter{ - Out: os.Stdout, - TimeFormat: time.StampMilli, - FormatCaller: func(i interface{}) string { - caller := i.(string) - if idx := strings.Index(caller, "/pkg/mod/"); idx > 0 { - return caller[idx+9:] - } - if idx := strings.LastIndexByte(caller, '/'); idx > 0 { - return caller[idx+1:] - } - return caller - }, -}).With().Timestamp().Logger() - -func init() { - zerolog.ErrorStackMarshaler = func(err error) interface{} { - return pkgerrors.MarshalStack(err) - } - - if ok, _ := strconv.ParseBool(os.Getenv("DEBUG")); ok { - SetDefaultLogger(ConsoleLogger, 1, zerolog.DebugLevel) - - } else if fi, _ := os.Stdout.Stat(); (fi.Mode() & os.ModeCharDevice) == 0 { - SetDefaultLogger(StructLogger, 1, zerolog.InfoLevel) - - } else { - SetDefaultLogger(ConsoleLogger, 1, zerolog.InfoLevel) - } -} - -func SetDefaultLogger(logger zerolog.Logger, deferSkip int, logLevel zerolog.Level) { - log.Logger = logger.With().Caller().Logger().Level(logLevel) - Logger = logger.With().CallerWithSkipFrameCount(deferSkip + 2).Logger().Level(logLevel) -} diff --git a/router/dns.go b/router/dns.go index 0087412..5b3a041 100644 --- a/router/dns.go +++ b/router/dns.go @@ -5,7 +5,7 @@ import ( "time" "github.com/miekg/dns" - "github.com/wweir/sower/pkg/deferlog" + "github.com/wweir/deferlog" ) func (r *Router) ServeDNS(w dns.ResponseWriter, req *dns.Msg) { diff --git a/router/ping.go b/router/ping.go index 8ef6680..65662c5 100644 --- a/router/ping.go +++ b/router/ping.go @@ -5,7 +5,7 @@ import ( "net/http" "time" - "github.com/wweir/sower/pkg/deferlog" + "github.com/wweir/deferlog" ) var pingClient = http.Client{ diff --git a/router/router.go b/router/router.go index 11761fb..006ec60 100644 --- a/router/router.go +++ b/router/router.go @@ -9,7 +9,7 @@ import ( geoip2 "github.com/oschwald/geoip2-golang" "github.com/pkg/errors" "github.com/rs/zerolog/log" - "github.com/wweir/sower/pkg/deferlog" + "github.com/wweir/deferlog" "github.com/wweir/sower/pkg/dhcp" "github.com/wweir/sower/pkg/mem" "github.com/wweir/sower/util"