From c0765944301f7eb1a3476c419ed415154625b370 Mon Sep 17 00:00:00 2001 From: Emil Hessman Date: Sun, 24 Mar 2019 16:01:10 +0100 Subject: [PATCH 01/34] Add Go 1.12 to Travis CI build matrix --- .travis.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.travis.yml b/.travis.yml index a8f1545..ff85ac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,21 @@ matrix: - go get golang.org/x/sys/windows script: - go test -race -v ./... + - go: 1.12.x + env: GO111MODULE=on + install: + - go mod download + script: + - go test -race -v ./... + - go: 1.12.x + env: GO111MODULE=off + install: + - go get github.com/stretchr/testify/assert + - go get golang.org/x/crypto/ssh/terminal + - go get golang.org/x/sys/unix + - go get golang.org/x/sys/windows + script: + - go test -race -v ./... - go: 1.10.x install: - go get github.com/stretchr/testify/assert @@ -50,3 +65,18 @@ matrix: - go get golang.org/x/sys/windows script: - go test -race -v -tags appengine ./... + - go: 1.12.x + env: GO111MODULE=on + install: + - go mod download + script: + - go test -race -v -tags appengine ./... + - go: 1.12.x + env: GO111MODULE=off + install: + - go get github.com/stretchr/testify/assert + - go get golang.org/x/crypto/ssh/terminal + - go get golang.org/x/sys/unix + - go get golang.org/x/sys/windows + script: + - go test -race -v -tags appengine ./... From 10ff0d07c31daf527d014cdf950c69b49b081a51 Mon Sep 17 00:00:00 2001 From: Andrey Tcherepanov Date: Tue, 26 Mar 2019 10:19:59 -0600 Subject: [PATCH 02/34] Got rid of IsTerminal call to reduce external dependencies --- terminal_check_aix.go | 10 +++++----- terminal_check_bsd.go | 7 +++++++ terminal_check_linux.go | 7 +++++++ terminal_check_notappengine.go | 8 +++++--- 4 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 terminal_check_bsd.go create mode 100644 terminal_check_linux.go diff --git a/terminal_check_aix.go b/terminal_check_aix.go index 04fdb7b..948c385 100644 --- a/terminal_check_aix.go +++ b/terminal_check_aix.go @@ -1,9 +1,9 @@ -// +build !appengine,!js,!windows,aix +// +build aix package logrus -import "io" +import ( + "golang.org/x/sys/unix" +) -func checkIfTerminal(w io.Writer) bool { - return false -} +const ioctlReadTermios = unix.TCGETS diff --git a/terminal_check_bsd.go b/terminal_check_bsd.go new file mode 100644 index 0000000..8f9c330 --- /dev/null +++ b/terminal_check_bsd.go @@ -0,0 +1,7 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package logrus + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TIOCGETA diff --git a/terminal_check_linux.go b/terminal_check_linux.go new file mode 100644 index 0000000..1690197 --- /dev/null +++ b/terminal_check_linux.go @@ -0,0 +1,7 @@ +// +build linux + +package logrus + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TCGETS diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index d465565..156a93b 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -1,4 +1,4 @@ -// +build !appengine,!js,!windows,!aix +// +build !appengine,!js,!windows package logrus @@ -6,13 +6,15 @@ import ( "io" "os" - "golang.org/x/crypto/ssh/terminal" + "golang.org/x/sys/unix" ) func checkIfTerminal(w io.Writer) bool { switch v := w.(type) { case *os.File: - return terminal.IsTerminal(int(v.Fd())) + _, err := unix.IoctlGetTermios(int(v.Fd()), ioctlReadTermios) + + return err == nil default: return false } From 7de3dd8c8bcd66620fd36cc2f77c8f3d1508c612 Mon Sep 17 00:00:00 2001 From: Andrey Tcherepanov Date: Tue, 26 Mar 2019 11:01:50 -0600 Subject: [PATCH 03/34] Removed golang.org/x/crypto refs --- .travis.yml | 4 ---- go.mod | 1 - go.sum | 2 -- 3 files changed, 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index ff85ac7..21c75b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ matrix: - go: 1.10.x install: - go get github.com/stretchr/testify/assert - - go get golang.org/x/crypto/ssh/terminal - go get golang.org/x/sys/unix - go get golang.org/x/sys/windows script: @@ -22,7 +21,6 @@ matrix: env: GO111MODULE=off install: - go get github.com/stretchr/testify/assert - - go get golang.org/x/crypto/ssh/terminal - go get golang.org/x/sys/unix - go get golang.org/x/sys/windows script: @@ -37,7 +35,6 @@ matrix: env: GO111MODULE=off install: - go get github.com/stretchr/testify/assert - - go get golang.org/x/crypto/ssh/terminal - go get golang.org/x/sys/unix - go get golang.org/x/sys/windows script: @@ -45,7 +42,6 @@ matrix: - go: 1.10.x install: - go get github.com/stretchr/testify/assert - - go get golang.org/x/crypto/ssh/terminal - go get golang.org/x/sys/unix - go get golang.org/x/sys/windows script: diff --git a/go.mod b/go.mod index 94574cc..8261a2b 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,5 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.1 // indirect github.com/stretchr/testify v1.2.2 - golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 ) diff --git a/go.sum b/go.sum index 133d34a..2d787be 100644 --- a/go.sum +++ b/go.sum @@ -9,7 +9,5 @@ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= From 41ee4dd36547ea07a3ffc951db6c341988569d5d Mon Sep 17 00:00:00 2001 From: Andrey Tcherepanov Date: Tue, 26 Mar 2019 14:53:49 -0600 Subject: [PATCH 04/34] Moved moved unix-related parts into terminal --- internal/terminal/terminal_check_bsd.go | 13 +++++++++++++ internal/terminal/terminal_check_unix.go | 13 +++++++++++++ terminal_check_aix.go | 9 --------- terminal_check_bsd.go | 7 ------- terminal_check_linux.go | 7 ------- terminal_check_notappengine.go | 6 ++---- 6 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 internal/terminal/terminal_check_bsd.go create mode 100644 internal/terminal/terminal_check_unix.go delete mode 100644 terminal_check_aix.go delete mode 100644 terminal_check_bsd.go delete mode 100644 terminal_check_linux.go diff --git a/internal/terminal/terminal_check_bsd.go b/internal/terminal/terminal_check_bsd.go new file mode 100644 index 0000000..6a47df6 --- /dev/null +++ b/internal/terminal/terminal_check_bsd.go @@ -0,0 +1,13 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package terminal + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TIOCGETA + +func IsTerminal(fd int) bool { + _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) + return err == nil +} + diff --git a/internal/terminal/terminal_check_unix.go b/internal/terminal/terminal_check_unix.go new file mode 100644 index 0000000..f30ea87 --- /dev/null +++ b/internal/terminal/terminal_check_unix.go @@ -0,0 +1,13 @@ +// +build linux aix + +package terminal + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TCGETS + +func IsTerminal(fd int) bool { + _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) + return err == nil +} + diff --git a/terminal_check_aix.go b/terminal_check_aix.go deleted file mode 100644 index 948c385..0000000 --- a/terminal_check_aix.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build aix - -package logrus - -import ( - "golang.org/x/sys/unix" -) - -const ioctlReadTermios = unix.TCGETS diff --git a/terminal_check_bsd.go b/terminal_check_bsd.go deleted file mode 100644 index 8f9c330..0000000 --- a/terminal_check_bsd.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build darwin dragonfly freebsd netbsd openbsd - -package logrus - -import "golang.org/x/sys/unix" - -const ioctlReadTermios = unix.TIOCGETA diff --git a/terminal_check_linux.go b/terminal_check_linux.go deleted file mode 100644 index 1690197..0000000 --- a/terminal_check_linux.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build linux - -package logrus - -import "golang.org/x/sys/unix" - -const ioctlReadTermios = unix.TCGETS diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index 156a93b..6f28f89 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -6,15 +6,13 @@ import ( "io" "os" - "golang.org/x/sys/unix" + "github.com/sirupsen/logrus/internal/terminal" ) func checkIfTerminal(w io.Writer) bool { switch v := w.(type) { case *os.File: - _, err := unix.IoctlGetTermios(int(v.Fd()), ioctlReadTermios) - - return err == nil + return terminal.IsTerminal(int(v.Fd())) default: return false } From 5d8c3bffc9ce192a8c4f61e46f3f174969149cc6 Mon Sep 17 00:00:00 2001 From: Andrey Tcherepanov Date: Wed, 27 Mar 2019 10:59:38 -0600 Subject: [PATCH 05/34] Updated travis.yml --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 21c75b7..2442f0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -56,7 +56,6 @@ matrix: env: GO111MODULE=off install: - go get github.com/stretchr/testify/assert - - go get golang.org/x/crypto/ssh/terminal - go get golang.org/x/sys/unix - go get golang.org/x/sys/windows script: @@ -71,7 +70,6 @@ matrix: env: GO111MODULE=off install: - go get github.com/stretchr/testify/assert - - go get golang.org/x/crypto/ssh/terminal - go get golang.org/x/sys/unix - go get golang.org/x/sys/windows script: From c49ef1d4bf8ba0994a478d210d58446327524490 Mon Sep 17 00:00:00 2001 From: Jessica Paczuski Date: Thu, 28 Mar 2019 11:32:49 +0100 Subject: [PATCH 06/34] Move terminal package fixes issue where terminal_check_notappengine.go can't access terminal package since terminal package is in an internal package --- {internal/terminal => terminal}/terminal_check_bsd.go | 0 {internal/terminal => terminal}/terminal_check_unix.go | 0 terminal_check_notappengine.go | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename {internal/terminal => terminal}/terminal_check_bsd.go (100%) rename {internal/terminal => terminal}/terminal_check_unix.go (100%) diff --git a/internal/terminal/terminal_check_bsd.go b/terminal/terminal_check_bsd.go similarity index 100% rename from internal/terminal/terminal_check_bsd.go rename to terminal/terminal_check_bsd.go diff --git a/internal/terminal/terminal_check_unix.go b/terminal/terminal_check_unix.go similarity index 100% rename from internal/terminal/terminal_check_unix.go rename to terminal/terminal_check_unix.go diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index 6f28f89..b61be8d 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -6,7 +6,7 @@ import ( "io" "os" - "github.com/sirupsen/logrus/internal/terminal" + "github.com/sirupsen/logrus/terminal" ) func checkIfTerminal(w io.Writer) bool { From 7d700cdff022e336606a06a82f6289ef694a13ab Mon Sep 17 00:00:00 2001 From: Andrey Tcherepanov Date: Thu, 28 Mar 2019 13:14:38 -0600 Subject: [PATCH 07/34] Test more platforms It would be 5 * 3 = 15 runs --- .travis.yml | 89 ++++++++++------------------------------------------- 1 file changed, 17 insertions(+), 72 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2442f0e..7e54dc6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,76 +1,21 @@ language: go go_import_path: github.com/sirupsen/logrus +git: + depth: 1 env: - - GOMAXPROCS=4 GORACE=halt_on_error=1 + - GO111MODULE=on + - GO111MODULE=off +go: [ 1.10.x, 1.11.x, 1.12.x ] +os: [ linux, osx, windows ] matrix: - include: - - go: 1.10.x - install: - - go get github.com/stretchr/testify/assert - - go get golang.org/x/sys/unix - - go get golang.org/x/sys/windows - script: - - go test -race -v ./... - - go: 1.11.x - env: GO111MODULE=on - install: - - go mod download - script: - - go test -race -v ./... - - go: 1.11.x - env: GO111MODULE=off - install: - - go get github.com/stretchr/testify/assert - - go get golang.org/x/sys/unix - - go get golang.org/x/sys/windows - script: - - go test -race -v ./... - - go: 1.12.x - env: GO111MODULE=on - install: - - go mod download - script: - - go test -race -v ./... - - go: 1.12.x - env: GO111MODULE=off - install: - - go get github.com/stretchr/testify/assert - - go get golang.org/x/sys/unix - - go get golang.org/x/sys/windows - script: - - go test -race -v ./... - - go: 1.10.x - install: - - go get github.com/stretchr/testify/assert - - go get golang.org/x/sys/unix - - go get golang.org/x/sys/windows - script: - - go test -race -v -tags appengine ./... - - go: 1.11.x - env: GO111MODULE=on - install: - - go mod download - script: - - go test -race -v -tags appengine ./... - - go: 1.11.x - env: GO111MODULE=off - install: - - go get github.com/stretchr/testify/assert - - go get golang.org/x/sys/unix - - go get golang.org/x/sys/windows - script: - - go test -race -v -tags appengine ./... - - go: 1.12.x - env: GO111MODULE=on - install: - - go mod download - script: - - go test -race -v -tags appengine ./... - - go: 1.12.x - env: GO111MODULE=off - install: - - go get github.com/stretchr/testify/assert - - go get golang.org/x/sys/unix - - go get golang.org/x/sys/windows - script: - - go test -race -v -tags appengine ./... + exclude: + - env: GO111MODULE=on + go: 1.10.x +install: + - if [[ "$GO111MODULE" == "on" ]]; then go mod download; fi + - if [[ "$GO111MODULE" == "off" ]]; then go get github.com/stretchr/testify/assert golang.org/x/sys/unix github.com/konsorten/go-windows-terminal-sequences; fi +script: + - export GOMAXPROCS=4 + - export GORACE=halt_on_error=1 + - go test -race -v ./... + - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then go test -race -v -tags appengine ./... ; fi From 38bc297a3db027003d165efe3483872065b4e00c Mon Sep 17 00:00:00 2001 From: Haoran Xu Date: Fri, 29 Mar 2019 14:04:26 +0800 Subject: [PATCH 08/34] return new entry for Entry.WithContext --- entry.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entry.go b/entry.go index 3d2c9e0..63e2558 100644 --- a/entry.go +++ b/entry.go @@ -103,8 +103,7 @@ func (entry *Entry) WithError(err error) *Entry { // Add a context to the Entry. func (entry *Entry) WithContext(ctx context.Context) *Entry { - entry.Context = ctx - return entry + return &Entry{Logger: entry.Logger, Data: entry.Data, Time: entry.Time, err: entry.err, Context: ctx} } // Add a single field to the Entry. From 3e06420226535b37c2ad9f55e41815c518a3f687 Mon Sep 17 00:00:00 2001 From: Andrey Tcherepanov Date: Mon, 1 Apr 2019 10:15:09 -0600 Subject: [PATCH 09/34] Move files to main directory --- terminal/terminal_check_bsd.go => terminal_check_bsd.go | 0 terminal/terminal_check_unix.go => terminal_check_unix.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename terminal/terminal_check_bsd.go => terminal_check_bsd.go (100%) rename terminal/terminal_check_unix.go => terminal_check_unix.go (100%) diff --git a/terminal/terminal_check_bsd.go b/terminal_check_bsd.go similarity index 100% rename from terminal/terminal_check_bsd.go rename to terminal_check_bsd.go diff --git a/terminal/terminal_check_unix.go b/terminal_check_unix.go similarity index 100% rename from terminal/terminal_check_unix.go rename to terminal_check_unix.go From ede5b639cd53235df193ab3c501d122be5195b17 Mon Sep 17 00:00:00 2001 From: Andrey Tcherepanov Date: Mon, 1 Apr 2019 10:16:11 -0600 Subject: [PATCH 10/34] Make isTerminal un-exported --- terminal_check_bsd.go | 4 ++-- terminal_check_notappengine.go | 4 +--- terminal_check_unix.go | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/terminal_check_bsd.go b/terminal_check_bsd.go index 6a47df6..3c4f43f 100644 --- a/terminal_check_bsd.go +++ b/terminal_check_bsd.go @@ -1,12 +1,12 @@ // +build darwin dragonfly freebsd netbsd openbsd -package terminal +package logrus import "golang.org/x/sys/unix" const ioctlReadTermios = unix.TIOCGETA -func IsTerminal(fd int) bool { +func isTerminal(fd int) bool { _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) return err == nil } diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index b61be8d..7be2d87 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -5,14 +5,12 @@ package logrus import ( "io" "os" - - "github.com/sirupsen/logrus/terminal" ) func checkIfTerminal(w io.Writer) bool { switch v := w.(type) { case *os.File: - return terminal.IsTerminal(int(v.Fd())) + return isTerminal(int(v.Fd())) default: return false } diff --git a/terminal_check_unix.go b/terminal_check_unix.go index f30ea87..355dc96 100644 --- a/terminal_check_unix.go +++ b/terminal_check_unix.go @@ -1,12 +1,12 @@ // +build linux aix -package terminal +package logrus import "golang.org/x/sys/unix" const ioctlReadTermios = unix.TCGETS -func IsTerminal(fd int) bool { +func isTerminal(fd int) bool { _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) return err == nil } From 6c615e1abed4589fe553415ab09b419938f65152 Mon Sep 17 00:00:00 2001 From: Haoran Xu Date: Tue, 2 Apr 2019 11:43:56 +0800 Subject: [PATCH 11/34] remove field if val is empty string for func and file field in text formatter --- text_formatter.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index b3b1a30..1569161 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -73,9 +73,9 @@ type TextFormatter struct { FieldMap FieldMap // CallerPrettyfier can be set by the user to modify the content - // of the function and file keys in the json data when ReportCaller is + // of the function and file keys in the data when ReportCaller is // activated. If any of the returned value is the empty string the - // corresponding key will be removed from json fields. + // corresponding key will be removed from fields. CallerPrettyfier func(*runtime.Frame) (function string, file string) terminalInitOnce sync.Once @@ -133,14 +133,19 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError)) } if entry.HasCaller() { - fixedKeys = append(fixedKeys, - f.FieldMap.resolve(FieldKeyFunc), f.FieldMap.resolve(FieldKeyFile)) if f.CallerPrettyfier != nil { funcVal, fileVal = f.CallerPrettyfier(entry.Caller) } else { funcVal = entry.Caller.Function fileVal = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) } + + if funcVal != "" { + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFunc)) + } + if fileVal != "" { + fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyFile)) + } } if !f.DisableSorting { @@ -225,7 +230,6 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin entry.Message = strings.TrimSuffix(entry.Message, "\n") caller := "" - if entry.HasCaller() { funcVal := fmt.Sprintf("%s()", entry.Caller.Function) fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line) @@ -233,7 +237,14 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin if f.CallerPrettyfier != nil { funcVal, fileVal = f.CallerPrettyfier(entry.Caller) } - caller = fileVal + " " + funcVal + + if fileVal == "" { + caller = funcVal + } else if funcVal == "" { + caller = fileVal + } else { + caller = fileVal + " " + funcVal + } } if f.DisableTimestamp { From 8bdbc7bcc01dcbb8ec23dc8a28e332258d25251f Mon Sep 17 00:00:00 2001 From: David Bariod Date: Tue, 2 Apr 2019 18:14:07 +0200 Subject: [PATCH 12/34] Release 1.4.1 --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9978b41..f62cbd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# 1.4.1 +This new release introduces: + * Enhance TextFormatter to not print caller information when they are empty (#944) + * Remove dependency on golang.org/x/crypto (#932, #943) + +Fixes: + * Fix Entry.WithContext method to return a copy of the initial entry (#941) + # 1.4.0 This new release introduces: * Add `DeferExitHandler`, similar to `RegisterExitHandler` but prepending the handler to the list of handlers (semantically like `defer`) (#848). From c1b61542d78094ef1a6fee0746528a251730141f Mon Sep 17 00:00:00 2001 From: David Bariod Date: Wed, 3 Apr 2019 10:46:44 +0200 Subject: [PATCH 13/34] Fix solaris build --- terminal_check_solaris.go | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 terminal_check_solaris.go diff --git a/terminal_check_solaris.go b/terminal_check_solaris.go new file mode 100644 index 0000000..f6710b3 --- /dev/null +++ b/terminal_check_solaris.go @@ -0,0 +1,11 @@ +package logrus + +import ( + "golang.org/x/sys/unix" +) + +// IsTerminal returns true if the given file descriptor is a terminal. +func isTerminal(fd int) bool { + _, err := unix.IoctlGetTermio(fd, unix.TCGETA) + return err == nil +} From 5521996833c095f5ac75d99687cef8ad344ded12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Chigot?= Date: Tue, 23 Apr 2019 11:34:05 +0200 Subject: [PATCH 14/34] Update x/sys/unix to fix AIX support --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8261a2b..12fdf98 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,5 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.1 // indirect github.com/stretchr/testify v1.2.2 - golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 + golang.org/x/sys v0.0.0-20190422165155-953cdadca894 ) diff --git a/go.sum b/go.sum index 2d787be..7976428 100644 --- a/go.sum +++ b/go.sum @@ -11,3 +11,5 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= From 1a601d20598f07b6397f1c2b3c7d659e3d1b4894 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Fri, 10 May 2019 06:44:02 +0200 Subject: [PATCH 15/34] remove go 1.10 from ci build matrix --- .travis.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7e54dc6..4fac79a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,12 +5,8 @@ git: env: - GO111MODULE=on - GO111MODULE=off -go: [ 1.10.x, 1.11.x, 1.12.x ] +go: [ 1.11.x, 1.12.x ] os: [ linux, osx, windows ] -matrix: - exclude: - - env: GO111MODULE=on - go: 1.10.x install: - if [[ "$GO111MODULE" == "on" ]]; then go mod download; fi - if [[ "$GO111MODULE" == "off" ]]; then go get github.com/stretchr/testify/assert golang.org/x/sys/unix github.com/konsorten/go-windows-terminal-sequences; fi From 0006e8ce1a5bbf2be3461a014582db0c5581f7bb Mon Sep 17 00:00:00 2001 From: "A. F" Date: Sat, 11 May 2019 00:40:04 +0200 Subject: [PATCH 16/34] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a4796eb..2040b42 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ func main() { log.Out = os.Stdout // You could set this to any `io.Writer` such as a file - // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666) + // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) // if err == nil { // log.Out = file // } else { From 1bc909a4f82b24e40552305da4a6907cabc48e66 Mon Sep 17 00:00:00 2001 From: Nicolas Lepage <19571875+nlepage@users.noreply.github.com> Date: Tue, 14 May 2019 09:13:07 +0200 Subject: [PATCH 17/34] Add a checkTerminal for nacl to support running on play.golang.org --- terminal_check_nacl.go | 11 +++++++++++ terminal_check_notappengine.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 terminal_check_nacl.go diff --git a/terminal_check_nacl.go b/terminal_check_nacl.go new file mode 100644 index 0000000..59d1e2c --- /dev/null +++ b/terminal_check_nacl.go @@ -0,0 +1,11 @@ +// +build nacl + +package logrus + +import ( + "io" +) + +func checkIfTerminal(w io.Writer) bool { + return false +} diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index 7be2d87..a0b571a 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -1,4 +1,4 @@ -// +build !appengine,!js,!windows +// +build !appengine,!js,!windows,!nacl package logrus From f2849a8fb20867468d5da0ea8137484d49c12bd7 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Sat, 18 May 2019 10:27:12 +0200 Subject: [PATCH 18/34] add full cross compilation in travis (#963) * add full cross compilation in travis * reduce the travis build matrix * disable cross build for plan9 and nacl --- .travis.yml | 10 +++++++++- travis/cross_build.sh | 5 +++++ travis/install.sh | 11 +++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100755 travis/cross_build.sh create mode 100755 travis/install.sh diff --git a/.travis.yml b/.travis.yml index 4fac79a..848938a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,11 +6,19 @@ env: - GO111MODULE=on - GO111MODULE=off go: [ 1.11.x, 1.12.x ] -os: [ linux, osx, windows ] +os: [ linux, osx ] +matrix: + exclude: + - go: 1.12.x + env: GO111MODULE=off + - go: 1.11.x + os: osx install: + - ./travis/install.sh - if [[ "$GO111MODULE" == "on" ]]; then go mod download; fi - if [[ "$GO111MODULE" == "off" ]]; then go get github.com/stretchr/testify/assert golang.org/x/sys/unix github.com/konsorten/go-windows-terminal-sequences; fi script: + - ./travis/cross_build.sh - export GOMAXPROCS=4 - export GORACE=halt_on_error=1 - go test -race -v ./... diff --git a/travis/cross_build.sh b/travis/cross_build.sh new file mode 100755 index 0000000..7bb9449 --- /dev/null +++ b/travis/cross_build.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +if [[ "$TRAVIS_GO_VERSION" =~ ^1.\12\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + /tmp/gox/gox -build-lib -all -os '!plan9 !nacl' +fi diff --git a/travis/install.sh b/travis/install.sh new file mode 100755 index 0000000..07f4532 --- /dev/null +++ b/travis/install.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +if [[ "$TRAVIS_GO_VERSION" =~ ^1.\12\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]]; then + git clone https://github.com/dgsb/gox.git /tmp/gox + pushd /tmp/gox + git checkout new_master + go build ./ + popd +fi From 744fc4caad20588e394ddd7a1af4d7a9ecabe3ac Mon Sep 17 00:00:00 2001 From: David Bariod Date: Sat, 18 May 2019 11:44:18 +0200 Subject: [PATCH 19/34] fix build break for plan9 --- go.sum | 1 + terminal_check_nacl.go | 11 ----------- ...heck_js.go => terminal_check_no_terminal.go | 2 +- terminal_check_notappengine.go | 2 +- terminal_check_windows.go | 18 ++++++++++++++++-- terminal_notwindows.go | 8 -------- terminal_windows.go | 18 ------------------ text_formatter.go | 4 ---- travis/cross_build.sh | 2 +- 9 files changed, 20 insertions(+), 46 deletions(-) delete mode 100644 terminal_check_nacl.go rename terminal_check_js.go => terminal_check_no_terminal.go (79%) delete mode 100644 terminal_notwindows.go delete mode 100644 terminal_windows.go diff --git a/go.sum b/go.sum index 7976428..596c318 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe h1:CHRGQ8V7OlCYtwaKPJi3iA7J+YdNKdo8j7nG5IgDhjs= github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/terminal_check_nacl.go b/terminal_check_nacl.go deleted file mode 100644 index 59d1e2c..0000000 --- a/terminal_check_nacl.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build nacl - -package logrus - -import ( - "io" -) - -func checkIfTerminal(w io.Writer) bool { - return false -} diff --git a/terminal_check_js.go b/terminal_check_no_terminal.go similarity index 79% rename from terminal_check_js.go rename to terminal_check_no_terminal.go index 0c20975..97af92c 100644 --- a/terminal_check_js.go +++ b/terminal_check_no_terminal.go @@ -1,4 +1,4 @@ -// +build js +// +build js nacl plan9 package logrus diff --git a/terminal_check_notappengine.go b/terminal_check_notappengine.go index a0b571a..3293fb3 100644 --- a/terminal_check_notappengine.go +++ b/terminal_check_notappengine.go @@ -1,4 +1,4 @@ -// +build !appengine,!js,!windows,!nacl +// +build !appengine,!js,!windows,!nacl,!plan9 package logrus diff --git a/terminal_check_windows.go b/terminal_check_windows.go index 3b9d286..572889d 100644 --- a/terminal_check_windows.go +++ b/terminal_check_windows.go @@ -6,15 +6,29 @@ import ( "io" "os" "syscall" + + sequences "github.com/konsorten/go-windows-terminal-sequences" ) +func initTerminal(w io.Writer) { + switch v := w.(type) { + case *os.File: + sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true) + } +} + func checkIfTerminal(w io.Writer) bool { + var ret bool switch v := w.(type) { case *os.File: var mode uint32 err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode) - return err == nil + ret = (err == nil) default: - return false + ret = false } + if ret { + initTerminal(w) + } + return ret } diff --git a/terminal_notwindows.go b/terminal_notwindows.go deleted file mode 100644 index 3dbd237..0000000 --- a/terminal_notwindows.go +++ /dev/null @@ -1,8 +0,0 @@ -// +build !windows - -package logrus - -import "io" - -func initTerminal(w io.Writer) { -} diff --git a/terminal_windows.go b/terminal_windows.go deleted file mode 100644 index b4ef528..0000000 --- a/terminal_windows.go +++ /dev/null @@ -1,18 +0,0 @@ -// +build !appengine,!js,windows - -package logrus - -import ( - "io" - "os" - "syscall" - - sequences "github.com/konsorten/go-windows-terminal-sequences" -) - -func initTerminal(w io.Writer) { - switch v := w.(type) { - case *os.File: - sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true) - } -} diff --git a/text_formatter.go b/text_formatter.go index 1569161..e01587c 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -84,10 +84,6 @@ type TextFormatter struct { func (f *TextFormatter) init(entry *Entry) { if entry.Logger != nil { f.isTerminal = checkIfTerminal(entry.Logger.Out) - - if f.isTerminal { - initTerminal(entry.Logger.Out) - } } } diff --git a/travis/cross_build.sh b/travis/cross_build.sh index 7bb9449..545d8c3 100755 --- a/travis/cross_build.sh +++ b/travis/cross_build.sh @@ -1,5 +1,5 @@ #!/bin/bash if [[ "$TRAVIS_GO_VERSION" =~ ^1.\12\. ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]]; then - /tmp/gox/gox -build-lib -all -os '!plan9 !nacl' + /tmp/gox/gox -build-lib -all fi From 839c75faf7f98a33d445d181f3018b5c3409a45e Mon Sep 17 00:00:00 2001 From: David Bariod Date: Sat, 18 May 2019 12:38:48 +0200 Subject: [PATCH 20/34] Release 1.4.2 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f62cbd2..51a7ab0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +# 1.4.2 + * Fixes build break for plan9, nacl, solaris # 1.4.1 This new release introduces: * Enhance TextFormatter to not print caller information when they are empty (#944) From fa0d2a82ff4d29942688ebd241548a6daa971b09 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 5 Jun 2019 00:10:46 -0700 Subject: [PATCH 23/34] add implementation and tests --- text_formatter.go | 9 ++++- text_formatter_test.go | 88 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/text_formatter.go b/text_formatter.go index e01587c..fb13499 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -57,6 +57,10 @@ type TextFormatter struct { // Disables the truncation of the level text to 4 characters. DisableLevelTruncation bool + // PadLevelText Adds padding the level text so that all the levels output at the same length + // PadLevelText is a superset of the DisableLevelTruncation option + PadLevelText bool + // QuoteEmptyFields will wrap empty fields in quotes if true QuoteEmptyFields bool @@ -217,9 +221,12 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin } levelText := strings.ToUpper(entry.Level.String()) - if !f.DisableLevelTruncation { + if !f.DisableLevelTruncation && !f.PadLevelText { levelText = levelText[0:4] } + if f.PadLevelText { + levelText = fmt.Sprintf("%-7s", levelText) + } // Remove a single newline if it already exists in the message to keep // the behavior of logrus text_formatter the same as the stdlib log package diff --git a/text_formatter_test.go b/text_formatter_test.go index 9c5e6f0..e48e430 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -172,6 +172,94 @@ func TestDisableLevelTruncation(t *testing.T) { checkDisableTruncation(false, InfoLevel) } +func TestPadLevelText(t *testing.T) { + // A note for future maintainers / committers: + // + // This test denormalizes the level text as a part of its assertions. + // Because of that, its not really a "unit test" of the PadLevelText functionality. + // So! Many apologies to the potential future person who has to rewrite this test + // when they are changing some completely unrelated functionality. + params := []struct { + name string + level Level + paddedLevelText string + }{ + { + name: "PanicLevel", + level: PanicLevel, + paddedLevelText: "PANIC ", // 2 extra spaces + }, + { + name: "FatalLevel", + level: FatalLevel, + paddedLevelText: "FATAL ", // 2 extra spaces + }, + { + name: "ErrorLevel", + level: ErrorLevel, + paddedLevelText: "ERROR ", // 2 extra spaces + }, + { + name: "WarnLevel", + level: WarnLevel, + // WARNING is already the max length, so we don't need to assert a paddedLevelText + }, + { + name: "DebugLevel", + level: DebugLevel, + paddedLevelText: "DEBUG ", // 2 extra spaces + }, + { + name: "TraceLevel", + level: TraceLevel, + paddedLevelText: "TRACE ", // 2 extra spaces + }, + { + name: "InfoLevel", + level: InfoLevel, + paddedLevelText: "INFO ", // 3 extra spaces + }, + } + + // We create a "default" TextFormatter to do a control case test + // and a TextFormatter with PadLevelText, which is the flag we are testing here + tfDefault := TextFormatter{} + tfWithPadding := TextFormatter{PadLevelText: true} + + for _, val := range params { + t.Run(val.name, func(t *testing.T) { + // TextFormatter writes into these bytes.Buffers, and we make assertions about their contents later + var bytesDefault bytes.Buffer + var bytesWithPadding bytes.Buffer + + // The TextFormatter instance and the bytes.Buffer instance are different here + // all the other arguments are the same + tfDefault.printColored(&bytesDefault, &Entry{Level: val.level}, []string{}, nil, "") + tfWithPadding.printColored(&bytesWithPadding, &Entry{Level: val.level}, []string{}, nil, "") + + // turn the bytes back into a string so that we can actually work with the data + logLineDefault := (&bytesDefault).String() + logLineWithPadding := (&bytesWithPadding).String() + + // Control: the level text should not be padded by default + if val.paddedLevelText != "" && strings.Contains(logLineDefault, val.paddedLevelText) { + t.Errorf("log line \"%s\" should not contain the padded level text \"%s\" by default", logLineDefault, val.paddedLevelText) + } + + // Assertion: the level text should still contain the string representation of the level + if !strings.Contains(strings.ToLower(logLineWithPadding), val.level.String()) { + t.Errorf("log line \"%s\" should contain the level text \"%s\" when padding is enabled", logLineWithPadding, val.level.String()) + } + + // Assertion: the level text should be in its padded form now + if val.paddedLevelText != "" && !strings.Contains(logLineWithPadding, val.paddedLevelText) { + t.Errorf("log line \"%s\" should contain the padded level text \"%s\" when padding is enabled", logLineWithPadding, val.paddedLevelText) + } + + }) + } +} + func TestDisableTimestampWithColoredOutput(t *testing.T) { tf := &TextFormatter{DisableTimestamp: true, ForceColors: true} From 58f7e00129989b7799aa4ed9097f98383bb579ac Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 5 Jun 2019 00:27:10 -0700 Subject: [PATCH 24/34] update comments --- text_formatter_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/text_formatter_test.go b/text_formatter_test.go index e48e430..775f4aa 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -221,8 +221,8 @@ func TestPadLevelText(t *testing.T) { }, } - // We create a "default" TextFormatter to do a control case test - // and a TextFormatter with PadLevelText, which is the flag we are testing here + // We create a "default" TextFormatter to do a control test. + // We also create a TextFormatter with PadLevelText, which is the parameter we want to do our most relevant assertions against. tfDefault := TextFormatter{} tfWithPadding := TextFormatter{PadLevelText: true} From 2d641d1668b752bae18eff57cbaa4f04f14236b2 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 5 Jun 2019 00:33:48 -0700 Subject: [PATCH 25/34] update the readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2040b42..48d64a0 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcr ``` Note that this does add measurable overhead - the cost will depend on the version of Go, but is between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your -environment via benchmarks: +environment via benchmarks: ``` go test -bench=.*CallerTracing ``` @@ -354,6 +354,7 @@ The built-in logging formatters are: [github.com/mattn/go-colorable](https://github.com/mattn/go-colorable). * When colors are enabled, levels are truncated to 4 characters by default. To disable truncation set the `DisableLevelTruncation` field to `true`. + * When outputting to a TTY, its often helpful to visually scan down a column where all the levels are the same width. Setting the `PadLevelText` field to `true` enables this functionality, by adding padding to the level text. * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter). * `logrus.JSONFormatter`. Logs fields as JSON. * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter). From 691f1b6074511f5bcf95dda280c8da89b6a7b3da Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 5 Jun 2019 00:36:14 -0700 Subject: [PATCH 26/34] add a space back --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 48d64a0..c5cd0e4 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ time="2015-03-26T01:27:38-04:00" level=fatal method=github.com/sirupsen/arcticcr ``` Note that this does add measurable overhead - the cost will depend on the version of Go, but is between 20 and 40% in recent tests with 1.6 and 1.7. You can validate this in your -environment via benchmarks: +environment via benchmarks: ``` go test -bench=.*CallerTracing ``` From bef31a5df9d1b50314d7611e392c706d6e6d695b Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 5 Jun 2019 00:41:05 -0700 Subject: [PATCH 27/34] wording shift --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c5cd0e4..08c3269 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ The built-in logging formatters are: [github.com/mattn/go-colorable](https://github.com/mattn/go-colorable). * When colors are enabled, levels are truncated to 4 characters by default. To disable truncation set the `DisableLevelTruncation` field to `true`. - * When outputting to a TTY, its often helpful to visually scan down a column where all the levels are the same width. Setting the `PadLevelText` field to `true` enables this functionality, by adding padding to the level text. + * When outputting to a TTY, its often helpful to visually scan down a column where all the levels are the same width. Setting the `PadLevelText` field to `true` enables this behavior, by adding padding to the level text. * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter). * `logrus.JSONFormatter`. Logs fields as JSON. * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter). From 693469de8f5d1e17ff6f3d8b479f36e5f97c5b7d Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Mon, 24 Jun 2019 20:42:20 -0700 Subject: [PATCH 29/34] dynamically space the level text --- text_formatter.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/text_formatter.go b/text_formatter.go index fb13499..a4b65e8 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -6,6 +6,7 @@ import ( "os" "runtime" "sort" + "strconv" "strings" "sync" "time" @@ -83,12 +84,22 @@ type TextFormatter struct { CallerPrettyfier func(*runtime.Frame) (function string, file string) terminalInitOnce sync.Once + + // The max length of the level text, generated dynamically on init + levelTextMaxLength int } func (f *TextFormatter) init(entry *Entry) { if entry.Logger != nil { f.isTerminal = checkIfTerminal(entry.Logger.Out) } + // Get the max length of the level text + for _, level := range AllLevels { + levelTextLength := len(level.String()) + if levelTextLength > f.levelTextMaxLength { + f.levelTextMaxLength = levelTextLength + } + } } func (f *TextFormatter) isColored() bool { @@ -225,7 +236,13 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin levelText = levelText[0:4] } if f.PadLevelText { - levelText = fmt.Sprintf("%-7s", levelText) + // Generates the format string used in the next line, for example "%-6s" or "%-7s". + // Based on the max level text length. + formatString := "%-" + strconv.Itoa(f.levelTextMaxLength) + "s" + // Formats the level text by appending spaces up to the max length, for example: + // - "INFO " + // - "WARNING" + levelText = fmt.Sprintf(formatString, levelText) } // Remove a single newline if it already exists in the message to keep From 8ba442aca65c8333a2c78c566e064cc0e8ad1f88 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Mon, 24 Jun 2019 20:50:37 -0700 Subject: [PATCH 30/34] init the loggers in tests --- text_formatter_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/text_formatter_test.go b/text_formatter_test.go index 775f4aa..d04fbcd 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -233,8 +233,11 @@ func TestPadLevelText(t *testing.T) { var bytesWithPadding bytes.Buffer // The TextFormatter instance and the bytes.Buffer instance are different here - // all the other arguments are the same + // all the other arguments are the same. We also initialize them so that they + // fill in the value of levelTextMaxLength. + tfDefault.init(&Entry{}) tfDefault.printColored(&bytesDefault, &Entry{Level: val.level}, []string{}, nil, "") + tfWithPadding.init(&Entry{}) tfWithPadding.printColored(&bytesWithPadding, &Entry{Level: val.level}, []string{}, nil, "") // turn the bytes back into a string so that we can actually work with the data From dcce32597dee9bd34880bf1e7a42a328eaec13d1 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 26 Jun 2019 20:37:17 -0700 Subject: [PATCH 31/34] avoid escapes! h/t @therealplato --- text_formatter_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/text_formatter_test.go b/text_formatter_test.go index d04fbcd..99b5d8c 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -246,17 +246,17 @@ func TestPadLevelText(t *testing.T) { // Control: the level text should not be padded by default if val.paddedLevelText != "" && strings.Contains(logLineDefault, val.paddedLevelText) { - t.Errorf("log line \"%s\" should not contain the padded level text \"%s\" by default", logLineDefault, val.paddedLevelText) + t.Errorf("log line %q should not contain the padded level text %q by default", logLineDefault, val.paddedLevelText) } // Assertion: the level text should still contain the string representation of the level if !strings.Contains(strings.ToLower(logLineWithPadding), val.level.String()) { - t.Errorf("log line \"%s\" should contain the level text \"%s\" when padding is enabled", logLineWithPadding, val.level.String()) + t.Errorf("log line %q should contain the level text %q when padding is enabled", logLineWithPadding, val.level.String()) } // Assertion: the level text should be in its padded form now if val.paddedLevelText != "" && !strings.Contains(logLineWithPadding, val.paddedLevelText) { - t.Errorf("log line \"%s\" should contain the padded level text \"%s\" when padding is enabled", logLineWithPadding, val.paddedLevelText) + t.Errorf("log line %q should contain the padded level text %q when padding is enabled", logLineWithPadding, val.paddedLevelText) } }) From af6ed964ef540511b4982e50cee85e6fc9715763 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Wed, 26 Jun 2019 20:48:04 -0700 Subject: [PATCH 32/34] len => RuneCount note: this is not currently easily testable without a larger diff that refactors the levels --- text_formatter.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/text_formatter.go b/text_formatter.go index a4b65e8..f08563e 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -10,6 +10,7 @@ import ( "strings" "sync" "time" + "unicode/utf8" ) const ( @@ -95,7 +96,7 @@ func (f *TextFormatter) init(entry *Entry) { } // Get the max length of the level text for _, level := range AllLevels { - levelTextLength := len(level.String()) + levelTextLength := utf8.RuneCount([]byte(level.String())) if levelTextLength > f.levelTextMaxLength { f.levelTextMaxLength = levelTextLength } From 539b8af839f189b659e6c7f62b4524a460fbae20 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Thu, 27 Jun 2019 18:35:00 -0700 Subject: [PATCH 33/34] its => it's --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08c3269..e7450a8 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ The built-in logging formatters are: [github.com/mattn/go-colorable](https://github.com/mattn/go-colorable). * When colors are enabled, levels are truncated to 4 characters by default. To disable truncation set the `DisableLevelTruncation` field to `true`. - * When outputting to a TTY, its often helpful to visually scan down a column where all the levels are the same width. Setting the `PadLevelText` field to `true` enables this behavior, by adding padding to the level text. + * When outputting to a TTY, it's often helpful to visually scan down a column where all the levels are the same width. Setting the `PadLevelText` field to `true` enables this behavior, by adding padding to the level text. * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter). * `logrus.JSONFormatter`. Logs fields as JSON. * All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter). From de736cf91b921d56253b4010270681d33fdf7cb5 Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Wed, 7 Aug 2019 06:34:36 -0400 Subject: [PATCH 34/34] readme: we have great maintainers now --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index e7450a8..7aa7bab 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,6 @@ comments](https://github.com/sirupsen/logrus/issues/553#issuecomment-306591437). For an in-depth explanation of the casing issue, see [this comment](https://github.com/sirupsen/logrus/issues/570#issuecomment-313933276). -**Are you interested in assisting in maintaining Logrus?** Currently I have a -lot of obligations, and I am unable to provide Logrus with the maintainership it -needs. If you'd like to help, please reach out to me at `simon at author's -username dot com`. - Nicely color-coded in development (when a TTY is attached, otherwise just plain text):