From 61ec4a4d87eeea73b9c60ef10c1c93a3b1e836e5 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Sun, 4 Jul 2021 15:43:34 +0200 Subject: [PATCH 1/2] Stop defaulting to color output on non-TTY This is arguably a breaking change, but there really is no legitimate case for output like this: https://circleci.com/gh/filecoin-project/lotus/189136 The change as implemented respects any explicit setting the user might have provided, and also keeps coloration in the URL case, in order to minimize the potential fallout --- go.mod | 1 + go.sum | 4 ++++ setup.go | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/go.mod b/go.mod index f9d6441..87a9891 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module github.com/ipfs/go-log/v2 require ( + github.com/mattn/go-isatty v0.0.13 go.uber.org/multierr v1.6.0 go.uber.org/zap v1.16.0 ) diff --git a/go.sum b/go.sum index 5062fc1..e8bf85f 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA= +github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -39,6 +41,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= diff --git a/setup.go b/setup.go index 75fed3f..8af0e51 100644 --- a/setup.go +++ b/setup.go @@ -8,6 +8,7 @@ import ( "strings" "sync" + "github.com/mattn/go-isatty" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -285,11 +286,18 @@ func configFromEnv() Config { format = os.Getenv(envIPFSLoggingFmt) } + var noExplicitFormat bool + switch format { case "nocolor": cfg.Format = PlaintextOutput case "json": cfg.Format = JSONOutput + default: + if format != "" { + fmt.Fprintf(os.Stderr, "ignoring unrecognized log format '%s'\n", format) + } + noExplicitFormat = true } lvl := os.Getenv(envLogging) @@ -340,6 +348,12 @@ func configFromEnv() Config { } } + if noExplicitFormat && + (!cfg.Stdout || !isTerm(os.Stdout)) && + (!cfg.Stderr || !isTerm(os.Stderr)) { + cfg.Format = PlaintextOutput + } + labels := os.Getenv(envLoggingLabels) if labels != "" { labelKVs := strings.Split(labels, ",") @@ -355,3 +369,7 @@ func configFromEnv() Config { return cfg } + +func isTerm(f *os.File) bool { + return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd()) +} From bd5b0c3bca7f1cfb9f51a3a17ab003ee9841bd9c Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Mon, 5 Jul 2021 06:38:13 +0200 Subject: [PATCH 2/2] Allow explicit usage of 'color' in GOLOG_LOG_FMT --- setup.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.go b/setup.go index 8af0e51..25940db 100644 --- a/setup.go +++ b/setup.go @@ -289,6 +289,8 @@ func configFromEnv() Config { var noExplicitFormat bool switch format { + case "color": + cfg.Format = ColorizedOutput case "nocolor": cfg.Format = PlaintextOutput case "json":