From 07141acce92fb73b1fa8fe8b011d886c759c36d2 Mon Sep 17 00:00:00 2001 From: Krzysztof Grodzicki Date: Sun, 12 Mar 2023 23:31:43 +0100 Subject: [PATCH] add: support for text formatter, deps --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- main.go | 36 +++++++++++++++++++++++++----------- main_test.go | 27 ++++++++++++++++++++++++--- 5 files changed, 91 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 65554da..45637a2 100644 --- a/README.md +++ b/README.md @@ -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 ( ... @@ -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 diff --git a/go.mod b/go.mod index 366ea34..24a7271 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4a26994..db12142 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 2df6183..33ec1e4 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,6 @@ func init() { } logrus.SetFormatter(f) logrus.SetOutput(os.Stdout) - logrus.AddHook(&hook{}) } @@ -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 @@ -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, diff --git a/main_test.go b/main_test.go index b348eee..57a6aed 100644 --- a/main_test.go +++ b/main_test.go @@ -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" @@ -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") }) }