Skip to content

Commit

Permalink
Move ozap plugin under logging package
Browse files Browse the repository at this point in the history
Add a unit test
Add godoc comments
Add READMEs

Signed-off-by: Alexander Mckenzie-Kelly <alexmk@uk.ibm.com>
open-policy-agent#6499
  • Loading branch information
alxmk committed Dec 20, 2023
1 parent 98e70f9 commit 888af16
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/logging/README.md → logging/plugins/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Logging
Plug-ins
===

This directory contains plug-in support for external logging libraries
Expand Down
11 changes: 11 additions & 0 deletions logging/plugins/ozap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ozap
===

ozap is a logging plug-in to wrap [zap](https://github.com/uber-go/zap)
as a [`logging.Logger`](https://pkg.go.dev/github.com/open-policy-agent/opa/logging#Logger).

```golang
opa, err := sdk.New(context.Background(), sdk.Options{
Logger: ozap.Wrap(logger, level),
})
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/open-policy-agent/opa/plugins/logging/ozap
module github.com/open-policy-agent/opa/logging/plugins/ozap

go 1.21.5

Expand Down
File renamed without changes.
File renamed without changes.
56 changes: 56 additions & 0 deletions logging/plugins/ozap/zap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ozap

import (
"bytes"
"errors"
"log"
"strings"
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestWrapWithFields(t *testing.T) {
tests := []struct {
name string
fields map[string]interface{}
expect []string
}{
{
name: "Ex1",
fields: map[string]interface{}{
"bad_error": errors.New("something went wrong"),
"context": "everywhere",
"panic": true,
"problems": 99,
"luftballons": int64(99),
},
expect: []string{
`"bad_error":"something went wrong"`,
`"context":"everywhere"`,
`"panic":true`,
`"problems":99`,
`"luftballons":99`,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
level := zap.NewAtomicLevelAt(zap.InfoLevel)
zaplogger := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()), zapcore.AddSync(&buf), zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= level.Level()
})))
Wrap(zaplogger, &level).WithFields(tt.fields).Info("test")
out := buf.String()
for _, e := range tt.expect {
if !strings.Contains(out, e) {
t.Fail()
log.Printf("Missing %s in %s", e, out)
}
}
})
}
}

0 comments on commit 888af16

Please sign in to comment.