Skip to content

Commit

Permalink
Introducing redactor (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
liranbg authored Jan 16, 2022
1 parent ce76476 commit 928f541
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 106 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: "1.14"
go-version: "1.17"

- uses: actions/cache@v2
with:
Expand All @@ -31,12 +31,17 @@ jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:

# 1.14 for BC
go: [ "1.14", "1.17" ]
steps:
- uses: actions/checkout@v2

- uses: actions/setup-go@v2
with:
go-version: "1.14"
go-version: ${{ matrix.go }}

- uses: actions/cache@v2
with:
Expand Down
23 changes: 17 additions & 6 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"time"

"github.com/pkg/errors"
"github.com/nuclio/errors"
)

var ErrBufferPoolAllocationTimeout = errors.New("Timed out waiting for buffer logger")
Expand All @@ -34,26 +35,36 @@ type BufferLogger struct {
Buffer *bytes.Buffer
}

// NewBufferLogger creates a logger that is able to capture the output into a buffer. if a request arrives
// and the user wishes to capture the log, this will be used as the logger instead of the default
// logger
func NewBufferLogger(name string, encoding string, level Level) (*BufferLogger, error) {
writer := &bytes.Buffer{}
return newBufferLogger(name, encoding, level, writer, writer)
}

func NewBufferLoggerWithRedactor(name string, encoding string, level Level, redactor *Redactor) (*BufferLogger, error) {
return newBufferLogger(name, encoding, level, redactor, redactor.GetOutput().(*bytes.Buffer))
}

// create a logger that is able to capture the output into a buffer. if a request arrives
// and the user wishes to capture the log, this will be used as the logger instead of the default
// logger
func newBufferLogger(name string,
encoding string,
level Level,
writer io.Writer,
buffer *bytes.Buffer) (*BufferLogger, error) {
newLogger, err := NewNuclioZap(name,
encoding,
nil,
writer,
writer,
level)

if err != nil {
return nil, errors.Wrap(err, "Failed to create buffer logger")
}

return &BufferLogger{
Logger: newLogger,
Buffer: writer,
Buffer: buffer,
encoding: encoding,
}, nil
}
Expand Down
85 changes: 85 additions & 0 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package nucliozap

import (
"bytes"
"testing"
"time"

Expand All @@ -27,6 +28,34 @@ type BufferLoggerTestSuite struct {
suite.Suite
}

func (suite *BufferLoggerTestSuite) TestRedactor() {
redactor := NewRedactor(&bytes.Buffer{})
redactor.AddValueRedactions([]string{"password"})
redactor.AddRedactions([]string{"replaceme"})
bufferLogger, err := NewBufferLoggerWithRedactor("test", "json", InfoLevel, redactor)
suite.Require().NoError(err, "Failed creating buffer logger")

bufferLogger.Logger.customEncoderConfig = NewEncoderConfig()
bufferLogger.Logger.customEncoderConfig.JSON.VarGroupName = "testVars"
bufferLogger.Logger.customEncoderConfig.JSON.VarGroupMode = VarGroupModeStructured
bufferLogger.Logger.prepareVarsCallback = bufferLogger.Logger.prepareVarsStructured

// log
bufferLogger.Logger.InfoWith("Check", "password", "123456", "replaceme", "55")

// get log entries
logEntries, err := bufferLogger.GetLogEntries()
suite.Require().NoError(err, "Failed to get log entries")

// verify (debug should be filtered)
suite.Require().Equal("Check", logEntries[0]["message"])
suite.Require().Equal("info", logEntries[0]["level"])
suite.Require().Equal(map[string]interface{}{
"*****": "55",
"password": "[redacted]",
}, logEntries[0][bufferLogger.Logger.customEncoderConfig.JSON.VarGroupName])
}

func (suite *BufferLoggerTestSuite) TestJSONEncoding() {
bufferLogger, err := NewBufferLogger("test", "json", InfoLevel)
suite.Require().NoError(err, "Failed creating buffer logger")
Expand Down Expand Up @@ -171,6 +200,62 @@ func (suite *BufferLoggerPoolTestSuite) TestAllocation() {
suite.Require().Equal(0, bufferLogger.Buffer.Len())
}

// ============
// Benchmarking
// ============

func BenchmarkNewBufferLogger(b *testing.B) {
loggerInstance := createBufferLogger(b, nil)
executeBenchmark(b, loggerInstance)
}

func BenchmarkNewBufferLoggerWithRedactor(b *testing.B) {
loggerInstance := createBufferLogger(b, NewRedactor(&bytes.Buffer{}))
executeBenchmark(b, loggerInstance)
}

func BenchmarkNewBufferLoggerWithDisabledRedactor(b *testing.B) {
redactor := NewRedactor(&bytes.Buffer{})
redactor.SetDisabled(true)
loggerInstance := createBufferLogger(b, redactor)
executeBenchmark(b, loggerInstance)
}

func BenchmarkNewBufferLoggerWithRedactorWithRedactions(b *testing.B) {
redactor := NewRedactor(&bytes.Buffer{})
redactor.AddRedactions([]string{"replaceme"})
loggerInstance := createBufferLogger(b, redactor)
executeBenchmark(b, loggerInstance)
}

func BenchmarkNewBufferLoggerWithRedactorWithValueRedactions(b *testing.B) {
redactor := NewRedactor(&bytes.Buffer{})
redactor.AddValueRedactions([]string{"replaceme"})
loggerInstance := createBufferLogger(b, redactor)
executeBenchmark(b, loggerInstance)
}

func createBufferLogger(b *testing.B, redactor *Redactor) *BufferLogger {
var err error
var bufferLogger *BufferLogger
if redactor != nil {
bufferLogger, err = NewBufferLoggerWithRedactor("test", "json", InfoLevel, redactor)
} else {
bufferLogger, err = NewBufferLogger("test", "json", InfoLevel)
}
if err != nil {
b.FailNow()
return nil
}
return bufferLogger
}

func executeBenchmark(b *testing.B, bufferLogger *BufferLogger) {
for i := 0; i < b.N; i++ {
bufferLogger.Logger.InfoWith("Check", "password", "123456", "replaceme", "55")
}
}

func TestBufferLoggerTestSuite(t *testing.T) {
suite.Run(t, new(BufferLoggerPoolTestSuite))
suite.Run(t, new(BufferLoggerTestSuite))
Expand Down
18 changes: 12 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
module github.com/nuclio/zap

go 1.14
go 1.17

require (
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/liranbg/uberzap v1.20.0-nuclio.1
github.com/logrusorgru/aurora/v3 v3.0.0
github.com/nuclio/errors v0.0.3
github.com/nuclio/logger v0.0.1
github.com/pavius/zap v1.4.2-0.20180228181622-8d52692529b8
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
68 changes: 51 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,32 +1,66 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/liranbg/uberzap v1.20.0-nuclio.1 h1:KUh2DHnhhF8q9iuzqZGxbIMg2i/5izyF+a8Cy9C6WsE=
github.com/liranbg/uberzap v1.20.0-nuclio.1/go.mod h1:Tj8cuE+vk7B5p5zywDnPCRk1FZX5UjyVugTtsTTHjxQ=
github.com/logrusorgru/aurora/v3 v3.0.0 h1:R6zcoZZbvVcGMvDCKo45A9U/lzYyzl5NfYIvznmDfE4=
github.com/logrusorgru/aurora/v3 v3.0.0/go.mod h1:vsR12bk5grlLvLXAYrBsb5Oc/N+LxAlxggSjiwMnCUc=
github.com/nuclio/errors v0.0.3 h1:eXi8DjLj5kn5X7TrVlpmt3lPfS3rueUKf/cIUmxV6x8=
github.com/nuclio/errors v0.0.3/go.mod h1:it2rUqDarIL8PasLYZo0Q1Ebsx4NRPM+OyYYakgNyrQ=
github.com/nuclio/logger v0.0.1 h1:e+vT/Ug65RC+u0QX2J+lq3P57ZBwJ1ZA6Q2LCEcViwE=
github.com/nuclio/logger v0.0.1/go.mod h1:ttazNAqTxKjQ7XrGDZxecumGa9KCIuJh88gzFY1mRXo=
github.com/pavius/zap v1.4.2-0.20180228181622-8d52692529b8 h1:WqLgmr/wj9TO5Sc6oYPQRAJBxuHE0NTeuVeFnT+FZVo=
github.com/pavius/zap v1.4.2-0.20180228181622-8d52692529b8/go.mod h1:6FWOCx06uh50GClv8S2cfk3asqTJs3qq3ZNRtLZE77I=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 928f541

Please sign in to comment.