Skip to content

Commit

Permalink
add: support for text formatter, deps
Browse files Browse the repository at this point in the history
  • Loading branch information
kgrodzicki committed Mar 12, 2023
1 parent 1d279cf commit 07141ac
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ go get github.com/flow-lab/dlog

## Usage

### Basic Usage with JSON formatter

This format is used by default. It is optimised for use with Datadog, Google Stackdriver and AWS CloudWatch.

```go
import (
...
Expand All @@ -47,6 +51,41 @@ logger.Info("Hello world")
{"appname":"myservice","component":"myprocessor","build":"2020-01-01T00:00:00Z","commit":"1234567","file":"/Users/test/dlog/main_test.go:82","func":"github.com/flow-lab/dlog.TestContextLogger.func2","level":"info","message":"Hello World","timestamp":"2023-01-09T16:17:36+01:00","version":"0.1.0"}
```


## Basic Usage with Text formatter

This format is used for local development.

```go
import (
...
log "github.com/sirupsen/logrus"
"github.com/flow-lab/dlog"
)

...

logger := dlog.NewLogger(&dlog.Config{
AppName: "myservice",
Level: "debug",
Version: "0.1.0",
Commit: "1234567",
Build: "2020-01-01T00:00:00Z",
ReportCaller: false,
Formatter: "text", // for local development
})

logger.Info("Hello World")
logger.Debug("Hello World")
logger.Warn("Hello World")
logger.Error("Hello World")

INFO [2023-03-12 23:04:11.377Z] Hello World
DEBUG [2023-03-12 23:04:11.378Z] Hello World
WARNING[2023-03-12 23:04:11.378Z] Hello World
ERROR [2023-03-12 23:04:11.378Z] Hello World
```

## Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you want to
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
36 changes: 25 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func init() {
}
logrus.SetFormatter(f)
logrus.SetOutput(os.Stdout)

logrus.AddHook(&hook{})
}

Expand Down Expand Up @@ -71,16 +70,17 @@ const (

// Config parameters for creating a logger
type Config struct {
CorrelationID string
AppName string
Parent string
Trace string
Span string
Version string
Commit string
Build string
Level string
ReportCaller bool
CorrelationID string // correlation id
AppName string // name of the application, e.g. diatom
Parent string // parent id
Trace string // trace id
Span string // span id
Version string // version of the application, e.g. 0.1.0
Commit string // short SHA
Build string // build number, e.g. 123
Level string // debug, info, warn, error, fatal, panic
ReportCaller bool // default is false
Formatter string // json or text, default is json
}

// NewLogger creates logger based on the config
Expand All @@ -97,6 +97,20 @@ func NewLogger(c *Config) *logrus.Entry {
}
}

if c.Formatter == "text" {
// this should be used for local development
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: false,
DisableLevelTruncation: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000Z",
PadLevelText: true,
ForceColors: true,
})
logrus.SetOutput(os.Stdout)
return logrus.NewEntry(logrus.StandardLogger())
}

fields := logrus.WithFields(logrus.Fields{
CorrelationID: &c.CorrelationID,
AppName: &c.AppName,
Expand Down
27 changes: 24 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package dlog
import (
"bytes"
"encoding/json"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)

const correlationID = "1-581cf771-a006649127e371903a2de979"
Expand Down Expand Up @@ -99,6 +98,28 @@ func TestContextLogger(t *testing.T) {
assert.Equal(t, "build", fields[Build])
assert.NotNil(t, fields["timestamp"])
assert.Contains(t, fields[Func], "TestContextLogger")
assert.Contains(t, fields[File], "main_test.go:85")
assert.Contains(t, fields[File], "main_test.go:84")
})

t.Run("should use text formatter", func(t *testing.T) {
var buffer bytes.Buffer
logger := NewLogger(&Config{
AppName: "myservice",
CorrelationID: "",
Span: "",
Trace: "",
Parent: "",
Version: "",
Commit: "",
Build: "",
ReportCaller: false,
Level: "debug",
Formatter: "text", // default is json
})
logger.Logger.Out = &buffer
logger.Info("Hello World")

assert.Contains(t, buffer.String(), "INFO ")
assert.Contains(t, buffer.String(), " Hello World")
})
}

0 comments on commit 07141ac

Please sign in to comment.