Skip to content

Commit

Permalink
loglevel-string (#12)
Browse files Browse the repository at this point in the history
* loglevel-string
  • Loading branch information
jmnote authored Sep 19, 2023
1 parent acd937f commit 601f318
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 59 deletions.
19 changes: 4 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ cover:
checks:
hack/checks.sh

misspell:
hack/misspell.sh

gocyclo:
hack/gocyclo.sh
3 changes: 3 additions & 0 deletions hack/checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 .

Expand Down
11 changes: 11 additions & 0 deletions hack/gocyclo.sh
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions hack/misspell.sh
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions hack/test-cover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 2 additions & 12 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,10 @@ import (
"github.com/sirupsen/logrus"
)

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)
)

var (
logger *logrus.Logger
AllLevels = []Level{FatalLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel}
callerSkip int = 10 // for prod(default), maybe 9 for test
AllLevels = []Level{PanicLevel, FatalLevel, ErrorLevel, WarnLevel, InfoLevel, DebugLevel, TraceLevel}
callerSkip int = 10 // 10 for prod(default), maybe 9 for goroutine or test code
)

func init() {
Expand Down
31 changes: 15 additions & 16 deletions logger/logger_inner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logger

import (
"bytes"
"fmt"
"os"
"runtime"
"testing"
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions logger/logger_outer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`},
}
Expand Down
26 changes: 26 additions & 0 deletions logger/types.go
Original file line number Diff line number Diff line change
@@ -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
}
66 changes: 66 additions & 0 deletions logger/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package logger

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestString(t *testing.T) {
testCases := []struct {
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) {
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)
})
}
}

0 comments on commit 601f318

Please sign in to comment.