From caad60da45b9e89232dc9fd724a1902d3b90225f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 13 Mar 2023 11:23:00 +0800 Subject: [PATCH] Apply `--disable-color` to global logger --- cmd/sing-box/main.go | 4 ++++ log/export.go | 4 ++++ log/factory.go | 29 ++++++----------------------- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/cmd/sing-box/main.go b/cmd/sing-box/main.go index aee754a6..322fb720 100644 --- a/cmd/sing-box/main.go +++ b/cmd/sing-box/main.go @@ -2,6 +2,7 @@ package main import ( "os" + "time" _ "github.com/sagernet/sing-box/include" "github.com/sagernet/sing-box/log" @@ -33,6 +34,9 @@ func main() { } func preRun(cmd *cobra.Command, args []string) { + if disableColor { + log.SetStdLogger(log.NewFactory(log.Formatter{BaseTime: time.Now(), DisableColors: true}, os.Stderr, nil).Logger()) + } if workingDir != "" { if err := os.Chdir(workingDir); err != nil { log.Fatal(err) diff --git a/log/export.go b/log/export.go index 690a8a1a..743fce93 100644 --- a/log/export.go +++ b/log/export.go @@ -16,6 +16,10 @@ func StdLogger() ContextLogger { return std } +func SetStdLogger(logger ContextLogger) { + std = logger +} + func Trace(args ...any) { std.Trace(args...) } diff --git a/log/factory.go b/log/factory.go index 6aab0401..cdc184c5 100644 --- a/log/factory.go +++ b/log/factory.go @@ -1,11 +1,15 @@ package log import ( - "context" - + "github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/observable" ) +type ( + Logger logger.Logger + ContextLogger logger.ContextLogger +) + type Factory interface { Level() Level SetLevel(level Level) @@ -22,24 +26,3 @@ type Entry struct { Level Level Message string } - -type Logger interface { - Trace(args ...any) - Debug(args ...any) - Info(args ...any) - Warn(args ...any) - Error(args ...any) - Fatal(args ...any) - Panic(args ...any) -} - -type ContextLogger interface { - Logger - TraceContext(ctx context.Context, args ...any) - DebugContext(ctx context.Context, args ...any) - InfoContext(ctx context.Context, args ...any) - WarnContext(ctx context.Context, args ...any) - ErrorContext(ctx context.Context, args ...any) - FatalContext(ctx context.Context, args ...any) - PanicContext(ctx context.Context, args ...any) -}