From 9a3522b6c11a4cca4c5406c215dde05be1b26cde Mon Sep 17 00:00:00 2001 From: jmnote Date: Tue, 19 Sep 2023 02:50:44 +0000 Subject: [PATCH 1/5] loglevel-string --- logger/logger.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 3eb85f9..c115684 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -12,17 +12,21 @@ import ( type Level logrus.Level const ( - FatalLevel Level = Level(logrus.FatalLevel) - ErrorLevel Level = Level(logrus.ErrorLevel) - WarnLevel Level = Level(logrus.WarnLevel) - InfoLevel Level = Level(logrus.InfoLevel) - DebugLevel Level = Level(logrus.DebugLevel) + FatalLevel Level = Level(logrus.FatalLevel) // "fatal" + ErrorLevel Level = Level(logrus.ErrorLevel) // "error" + WarnLevel Level = Level(logrus.WarnLevel) // "warn" + InfoLevel Level = Level(logrus.InfoLevel) // "info" + DebugLevel Level = Level(logrus.DebugLevel) // "debug" ) +func (level Level) String() string { + return logrus.Level(level).String() +} + var ( logger *logrus.Logger AllLevels = []Level{FatalLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel} - callerSkip int = 10 // for prod(default), maybe 9 for test + callerSkip int = 10 // 10 for prod(default), maybe 9 for goroutine or test code ) func init() { From 6b2a8775c81fcb538ae7d5e392ed8958e7eb4d7d Mon Sep 17 00:00:00 2001 From: jmnote Date: Tue, 19 Sep 2023 03:27:49 +0000 Subject: [PATCH 2/5] coverage 100 --- .gitignore | 19 +++-------- Makefile | 5 +++ hack/checks.sh | 3 ++ hack/gocyclo.sh | 11 +++++++ hack/misspell.sh | 11 +++++++ hack/test-cover.sh | 4 +-- logger/logger.go | 16 +--------- logger/logger_inner_test.go | 31 +++++++++--------- logger/logger_outer_test.go | 28 ++++++++-------- logger/types.go | 26 +++++++++++++++ logger/types_test.go | 64 +++++++++++++++++++++++++++++++++++++ 11 files changed, 156 insertions(+), 62 deletions(-) create mode 100755 hack/gocyclo.sh create mode 100755 hack/misspell.sh create mode 100644 logger/types.go create mode 100644 logger/types_test.go diff --git a/.gitignore b/.gitignore index 66fd13c..880cf4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,4 @@ -# Binaries for programs and plugins -*.exe -*.exe~ -*.dll -*.so -*.dylib - -# Test binary, built with `go test -c` -*.test - -# Output of the go coverage tool, specifically when used with LiteIDE -*.out - -# Dependency directories (remove the comment below to include it) -# vendor/ +.vscode +.theia +tmp/ +bin/ diff --git a/Makefile b/Makefile index af5ed8d..35a12d0 100644 --- a/Makefile +++ b/Makefile @@ -8,3 +8,8 @@ cover: checks: hack/checks.sh +misspell: + hack/misspell.sh + +gocyclo: + hack/gocyclo.sh \ No newline at end of file diff --git a/hack/checks.sh b/hack/checks.sh index 5e14e66..8206382 100755 --- a/hack/checks.sh +++ b/hack/checks.sh @@ -6,6 +6,9 @@ go mod tidy go fmt ./... go vet ./... +./hack/misspell.sh +./hack/gocyclo.sh + which goimports || go install golang.org/x/tools/cmd/goimports@latest goimports -local -v -w . diff --git a/hack/gocyclo.sh b/hack/gocyclo.sh new file mode 100755 index 0000000..f101228 --- /dev/null +++ b/hack/gocyclo.sh @@ -0,0 +1,11 @@ +#!/bin/bash +cd $(dirname $0)/../ + +which gocyclo || go install github.com/fzipp/gocyclo/cmd/gocyclo@latest + +gocyclo -over 15 . +if [[ $? != 0 ]]; then + echo "❌ FAIL" + exit 1 +fi +echo "✔️ OK" \ No newline at end of file diff --git a/hack/misspell.sh b/hack/misspell.sh new file mode 100755 index 0000000..9b0f5f9 --- /dev/null +++ b/hack/misspell.sh @@ -0,0 +1,11 @@ +#!/bin/bash +cd $(dirname $0)/.. + +[ -f ./bin/misspell ] || curl -L https://git.io/misspell | bash + +find ./ -type f -name '*.*' | xargs ./bin/misspell -error +if [[ $? != 0 ]]; then + echo "❌ FAIL - misspell found" + exit 1 +fi +echo "✔️ OK - misspell not found" diff --git a/hack/test-cover.sh b/hack/test-cover.sh index abc11da..156043f 100755 --- a/hack/test-cover.sh +++ b/hack/test-cover.sh @@ -4,8 +4,8 @@ MIN_COVER=80 cd $(dirname $0)/.. export PS4='[$(basename $0):$LINENO] ' -echo + go test ./... -v -failfast -race -covermode=atomic -coverprofile /tmp/cover.out - go test ./... -v -failfast -race -covermode=atomic -coverprofile /tmp/cover.out +echo + go test ./... -v -failfast -race -coverprofile /tmp/cover.out + go test ./... -v -failfast -race -coverprofile /tmp/cover.out if [[ $? != 0 ]]; then echo "❌ FAIL - test failed" exit 1 diff --git a/logger/logger.go b/logger/logger.go index c115684..2f0a664 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -9,23 +9,9 @@ import ( "github.com/sirupsen/logrus" ) -type Level logrus.Level - -const ( - FatalLevel Level = Level(logrus.FatalLevel) // "fatal" - ErrorLevel Level = Level(logrus.ErrorLevel) // "error" - WarnLevel Level = Level(logrus.WarnLevel) // "warn" - InfoLevel Level = Level(logrus.InfoLevel) // "info" - DebugLevel Level = Level(logrus.DebugLevel) // "debug" -) - -func (level Level) String() string { - return logrus.Level(level).String() -} - var ( logger *logrus.Logger - AllLevels = []Level{FatalLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel} + AllLevels = []Level{PanicLevel, FatalLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel, TraceLevel} callerSkip int = 10 // 10 for prod(default), maybe 9 for goroutine or test code ) diff --git a/logger/logger_inner_test.go b/logger/logger_inner_test.go index 05e6f21..a42e00d 100644 --- a/logger/logger_inner_test.go +++ b/logger/logger_inner_test.go @@ -2,7 +2,6 @@ package logger import ( "bytes" - "fmt" "os" "runtime" "testing" @@ -86,25 +85,25 @@ func TestSetCallerSkip(t *testing.T) { skip int wantContains string }{ - {0, `level=warning msg="hello=world number=42" file="logger.go:74"`}, - {1, `level=warning msg="hello=world number=42" file="text_formatter.go:159"`}, - {2, `level=warning msg="hello=world number=42" file="entry.go:289"`}, - {3, `level=warning msg="hello=world number=42" file="entry.go:252"`}, - {4, `level=warning msg="hello=world number=42" file="entry.go:304"`}, - {5, `level=warning msg="hello=world number=42" file="entry.go:349"`}, - {6, `level=warning msg="hello=world number=42" file="logger.go:154"`}, - {7, `level=warning msg="hello=world number=42" file="logger.go:178"`}, - {8, `level=warning msg="hello=world number=42" file="logger.go:98"`}, - {9, `level=warning msg="hello=world number=42" file="logger_inner_test.go:110"`}, // good for go test - {10, `level=warning msg="hello=world number=42" file="logger_inner_test.go:121"`}, - {11, `level=warning msg="hello=world number=42" file="logger_inner_test.go:109"`}, - {12, `level=warning msg="hello=world number=42" file="testing.go:1576"`}, - {13, `level=warning msg="hello=world number=42" file="asm_amd64.s:1598"`}, + {0, `level=warning msg="hello=world number=42" file="logger.go:`}, + {1, `level=warning msg="hello=world number=42" file="text_formatter.go:`}, + {2, `level=warning msg="hello=world number=42" file="entry.go:`}, + {3, `level=warning msg="hello=world number=42" file="entry.go:`}, + {4, `level=warning msg="hello=world number=42" file="entry.go:`}, + {5, `level=warning msg="hello=world number=42" file="entry.go:`}, + {6, `level=warning msg="hello=world number=42" file="logger.go:`}, + {7, `level=warning msg="hello=world number=42" file="logger.go:`}, + {8, `level=warning msg="hello=world number=42" file="logger.go:`}, + {9, `level=warning msg="hello=world number=42" file="logger_inner_test.go:`}, // good for go test + {10, `level=warning msg="hello=world number=42" file="logger_inner_test.go:`}, + {11, `level=warning msg="hello=world number=42" file="logger_inner_test.go:`}, + {12, `level=warning msg="hello=world number=42" file="testing.go:`}, + {13, `level=warning msg="hello=world number=42" file="asm_amd64.s:`}, {14, `level=warning msg="hello=world number=42" file="???:1"`}, {15, `level=warning msg="hello=world number=42" file="???:1"`}, } for _, tc := range testCases { - t.Run(fmt.Sprintf("skip=%d", tc.skip), func(t *testing.T) { + t.Run("", func(t *testing.T) { SetCallerSkip(tc.skip) got := captureOutput(func() { Warnf("hello=%s number=%d", "world", 42) diff --git a/logger/logger_outer_test.go b/logger/logger_outer_test.go index a9d047f..51aec50 100644 --- a/logger/logger_outer_test.go +++ b/logger/logger_outer_test.go @@ -56,20 +56,20 @@ func TestSetCallerSkip_outer(t *testing.T) { skip int wantContains string }{ - {0, `level=warning msg="hello=world number=42" file="logger.go:74"`}, - {1, `level=warning msg="hello=world number=42" file="text_formatter.go:159"`}, - {2, `level=warning msg="hello=world number=42" file="entry.go:289"`}, - {3, `level=warning msg="hello=world number=42" file="entry.go:252"`}, - {4, `level=warning msg="hello=world number=42" file="entry.go:304"`}, - {5, `level=warning msg="hello=world number=42" file="entry.go:349"`}, - {6, `level=warning msg="hello=world number=42" file="logger.go:154"`}, - {7, `level=warning msg="hello=world number=42" file="logger.go:178"`}, - {8, `level=warning msg="hello=world number=42" file="logger.go:98"`}, - {9, `level=warning msg="hello=world number=42" file="logger_outer_test.go:80"`}, // good for go test - {10, `level=warning msg="hello=world number=42" file="logger_outer_test.go:91"`}, - {11, `level=warning msg="hello=world number=42" file="logger_outer_test.go:79"`}, - {12, `level=warning msg="hello=world number=42" file="testing.go:1576"`}, - {13, `level=warning msg="hello=world number=42" file="asm_amd64.s:1598"`}, + {0, `level=warning msg="hello=world number=42" file="logger.go:`}, + {1, `level=warning msg="hello=world number=42" file="text_formatter.go:`}, + {2, `level=warning msg="hello=world number=42" file="entry.go:`}, + {3, `level=warning msg="hello=world number=42" file="entry.go:`}, + {4, `level=warning msg="hello=world number=42" file="entry.go:`}, + {5, `level=warning msg="hello=world number=42" file="entry.go:`}, + {6, `level=warning msg="hello=world number=42" file="logger.go:`}, + {7, `level=warning msg="hello=world number=42" file="logger.go:`}, + {8, `level=warning msg="hello=world number=42" file="logger.go:`}, + {9, `level=warning msg="hello=world number=42" file="logger_outer_test.go:`}, // good for go test + {10, `level=warning msg="hello=world number=42" file="logger_outer_test.go:`}, + {11, `level=warning msg="hello=world number=42" file="logger_outer_test.go:`}, + {12, `level=warning msg="hello=world number=42" file="testing.go:`}, + {13, `level=warning msg="hello=world number=42" file="asm_amd64.s:`}, {14, `level=warning msg="hello=world number=42" file="???:1"`}, {15, `level=warning msg="hello=world number=42" file="???:1"`}, } diff --git a/logger/types.go b/logger/types.go new file mode 100644 index 0000000..25588ad --- /dev/null +++ b/logger/types.go @@ -0,0 +1,26 @@ +package logger + +import ( + "github.com/sirupsen/logrus" +) + +type Level logrus.Level + +const ( + PanicLevel Level = Level(logrus.PanicLevel) + FatalLevel Level = Level(logrus.FatalLevel) + ErrorLevel Level = Level(logrus.ErrorLevel) + WarnLevel Level = Level(logrus.WarnLevel) + InfoLevel Level = Level(logrus.InfoLevel) + DebugLevel Level = Level(logrus.DebugLevel) + TraceLevel Level = Level(logrus.TraceLevel) +) + +func (level Level) String() string { + return logrus.Level(level).String() +} + +func ParseLevel(lvl string) (Level, error) { + level, err := logrus.ParseLevel(lvl) + return Level(level), err +} diff --git a/logger/types_test.go b/logger/types_test.go new file mode 100644 index 0000000..a04e205 --- /dev/null +++ b/logger/types_test.go @@ -0,0 +1,64 @@ +package logger + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestString(t *testing.T) { + testCases := []struct { + logLevel Level + want string + }{ + {FatalLevel, "fatal"}, + {ErrorLevel, "error"}, + {WarnLevel, "warning"}, + {InfoLevel, "info"}, + {DebugLevel, "debug"}, + } + for _, tc := range testCases { + t.Run("", func(t *testing.T) { + got := tc.logLevel.String() + require.Equal(t, tc.want, got) + }) + } +} + +func TestParseLevel(t *testing.T) { + testCases := []struct { + levelString string + want Level + wantError string + }{ + {"panic", PanicLevel, ``}, + {"PANIC", PanicLevel, ``}, + {"fatal", FatalLevel, ``}, + {"FATAL", FatalLevel, ``}, + {"error", ErrorLevel, ``}, + {"ERROR", ErrorLevel, ``}, + {"warn", WarnLevel, ``}, + {"WARN", WarnLevel, ``}, + {"warning", WarnLevel, ``}, + {"WARNING", WarnLevel, ``}, + {"info", InfoLevel, ``}, + {"INFO", InfoLevel, ``}, + {"debug", DebugLevel, ``}, + {"DEBUG", DebugLevel, ``}, + {"trace", TraceLevel, ``}, + {"TRACE", TraceLevel, ``}, + {"invalid", PanicLevel, `not a valid logrus Level: "invalid"`}, + {"foo", PanicLevel, `not a valid logrus Level: "foo"`}, + } + for _, tc := range testCases { + t.Run("", func(t *testing.T) { + got, err := ParseLevel(tc.levelString) + if tc.wantError == "" { + require.NoError(t, err) + } else { + require.EqualError(t, err, tc.wantError) + } + require.Equal(t, tc.want, got) + }) + } +} From 877e5a0eba19befeaf52fff54fcd068b56b847c0 Mon Sep 17 00:00:00 2001 From: jmnote Date: Tue, 19 Sep 2023 03:28:47 +0000 Subject: [PATCH 3/5] add newline --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 35a12d0..7146899 100644 --- a/Makefile +++ b/Makefile @@ -12,4 +12,4 @@ misspell: hack/misspell.sh gocyclo: - hack/gocyclo.sh \ No newline at end of file + hack/gocyclo.sh From 4a035f661992cbe0cc1953837817258e7e26cf6c Mon Sep 17 00:00:00 2001 From: jmnote Date: Tue, 19 Sep 2023 03:29:14 +0000 Subject: [PATCH 4/5] add newline --- hack/gocyclo.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hack/gocyclo.sh b/hack/gocyclo.sh index f101228..c5aabec 100755 --- a/hack/gocyclo.sh +++ b/hack/gocyclo.sh @@ -8,4 +8,4 @@ if [[ $? != 0 ]]; then echo "❌ FAIL" exit 1 fi -echo "✔️ OK" \ No newline at end of file +echo "✔️ OK" From 26b664ccd0ae1badb2e5c24c5b414d8aeb8d0359 Mon Sep 17 00:00:00 2001 From: jmnote Date: Tue, 19 Sep 2023 03:30:23 +0000 Subject: [PATCH 5/5] TestString cases --- logger/types_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/logger/types_test.go b/logger/types_test.go index a04e205..6bd8614 100644 --- a/logger/types_test.go +++ b/logger/types_test.go @@ -11,11 +11,13 @@ func TestString(t *testing.T) { logLevel Level want string }{ + {PanicLevel, "panic"}, {FatalLevel, "fatal"}, {ErrorLevel, "error"}, {WarnLevel, "warning"}, {InfoLevel, "info"}, {DebugLevel, "debug"}, + {TraceLevel, "trace"}, } for _, tc := range testCases { t.Run("", func(t *testing.T) {