diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..3693f8e --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,25 @@ +name: Test + +on: push + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - name: Checkout the repository + uses: actions/checkout@v2 + + - name: Checkout actions repository + uses: actions/checkout@v2 + with: + repository: msales/github-actions + ref: master + token: ${{ secrets.GH_TOKEN }} + path: .github/actions/external + + - name: Run the tests + id: test + uses: ./.github/actions/external/go-test + with: + org_token: ${{ secrets.GH_TOKEN }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index bd85e94..0000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -sudo: false - -language: go -go: -- "1.11" -- "1.x" -env: -- GO111MODULE=on - -before_install: -- go get -u golang.org/x/lint/golint -- go get github.com/mattn/goveralls - -script: -- golint ./... -- go vet ./... -- go test -covermode=count -coverprofile=profile.cov ./... -- goveralls -coverprofile=profile.cov -service=travis-ci - -notifications: - email: false \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1d66308 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM msales/go-builder:1.15-base-1.0.0 as builder + +# This image only serves testing purposes. + +# Set token +ARG GITHUB_TOKEN +RUN git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/" + +WORKDIR /app + +COPY ./ . + +ENV GOPRIVATE github.com/msales +ENV GO111MODULE on + +# It's a library. Build will only check if the code can be compiled successfully. +RUN go build -v ./... \ No newline at end of file diff --git a/go.mod b/go.mod index 640212c..fe724ed 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,10 @@ module github.com/msales/logged +go 1.15 + require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.2 + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 ) diff --git a/go.sum b/go.sum index e03ee77..99900af 100644 --- a/go.sum +++ b/go.sum @@ -4,3 +4,5 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/logger.go b/logger.go index e654c93..ec6aec5 100644 --- a/logger.go +++ b/logger.go @@ -1,8 +1,9 @@ package logged import ( - "errors" "io" + + "golang.org/x/xerrors" ) const errorKey = "LOGGED_ERROR" @@ -33,7 +34,7 @@ func LevelFromString(lvl string) (Level, error) { case "crit": return Crit, nil default: - return 0, errors.New("unknown level " + lvl) + return 0, xerrors.Errorf("log: invalid log level: %s", lvl) } } @@ -55,6 +56,39 @@ func (l Level) String() string { } } +// List of predefined log Formats +const ( + JSON Format = iota + Logfmt +) + +// Format represents the predefined log format. +type Format int + +// FormatterFromString returns a formatter instance appropriate for the given format name. +func FormatterFromString(format string) (Formatter, error) { + switch format { + case "json": + return JSONFormat(), nil + case "logfmt": + return LogfmtFormat(), nil + default: + return nil, xerrors.Errorf("log: invalid log format: %s", format) + } +} + +// String returns the string representation of the level. +func (f Format) String() string { + switch f { + case JSON: + return "json" + case Logfmt: + return "logfmt" + default: + return "unkn" + } +} + // Logger represents a log writer. type Logger interface { // Debug logs a debug message. diff --git a/logger_test.go b/logger_test.go index a2a8ea4..5e634f0 100644 --- a/logger_test.go +++ b/logger_test.go @@ -65,7 +65,7 @@ func TestLevelFromString(t *testing.T) { } assert.NoError(t, err) - assert.Equal(t, tt.want, lvl, ) + assert.Equal(t, tt.want, lvl) }) } } @@ -106,6 +106,64 @@ func TestLevel_String(t *testing.T) { } } +func TestFormatterFromString(t *testing.T) { + tests := []struct { + format string + wantError bool + }{ + { + format: "json", + wantError: false, + }, + { + format: "logfmt", + wantError: false, + }, + { + format: "unkn", + wantError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.format, func(t *testing.T) { + format, err := logged.FormatterFromString(tt.format) + + if tt.wantError { + assert.Error(t, err) + return + } + + assert.NoError(t, err) + assert.IsType(t, logged.FormatterFunc(nil), format) + }) + } +} + +func TestFormat_String(t *testing.T) { + tests := []struct { + format logged.Format + want string + }{ + { + format: logged.JSON, + want: "json", + }, + { + format: logged.Logfmt, + want: "logfmt", + }, + { + format: logged.Format(123), + want: "unkn", + }, + } + + for _, tt := range tests { + assert.Equal(t, tt.want, tt.format.String()) + } +} + func TestNew(t *testing.T) { h := logged.HandlerFunc(func(msg string, lvl logged.Level, ctx []interface{}) {})