Skip to content

Commit

Permalink
Added log formats and support methods. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skandalik authored Sep 9, 2020
1 parent 9c1702d commit 9a6eb24
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 24 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 ./...
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
38 changes: 36 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package logged

import (
"errors"
"io"

"golang.org/x/xerrors"
)

const errorKey = "LOGGED_ERROR"
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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.
Expand Down
60 changes: 59 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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{}) {})

Expand Down

0 comments on commit 9a6eb24

Please sign in to comment.