diff --git a/go.mod b/go.mod index ce36541..5f0fbd7 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/Laisky/go-chaining v0.0.0-20180507092046-43dcdc5a21be github.com/Laisky/golang-fifo v1.0.1-0.20240403091456-fc83d5e38c0b github.com/Laisky/graphql v1.0.6 - github.com/Laisky/zap v1.27.0 + github.com/Laisky/zap v1.27.1-0.20240628060440-a253d90172e3 github.com/brianvoe/gofakeit/v6 v6.23.2 github.com/cespare/xxhash v1.1.0 github.com/corvus-ch/shamir v1.0.1 diff --git a/go.sum b/go.sum index 9fcfab9..6fb3a1b 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,8 @@ github.com/Laisky/golang-fifo v1.0.1-0.20240403091456-fc83d5e38c0b h1:o2BuVyXFkD github.com/Laisky/golang-fifo v1.0.1-0.20240403091456-fc83d5e38c0b/go.mod h1:j90tUqwBaEncIzpAd6ZGPZHWjclgAyMY8fdOqsewitE= github.com/Laisky/graphql v1.0.6 h1:NEULGxfxo+wbsW2OmqBXOMNUGgqo8uFjWNabwuNK10g= github.com/Laisky/graphql v1.0.6/go.mod h1:zaKVqXmMQTnTkFJ2AA53oyBWMzlGCnzr3aodKTrtOxI= -github.com/Laisky/zap v1.27.0 h1:NEtFniRXOKUkEf9//FUiBkSIdgbB4V1H3khA/jmPZx4= -github.com/Laisky/zap v1.27.0/go.mod h1:HABqM5YDQlPq8w+Pmp9h/x9F6Vy+3oHBLP+2+pBoaJw= +github.com/Laisky/zap v1.27.1-0.20240628060440-a253d90172e3 h1:SD0siYXoInGc6MqVsmJrBJl4TNHYXfeGu92fRSUNnas= +github.com/Laisky/zap v1.27.1-0.20240628060440-a253d90172e3/go.mod h1:HABqM5YDQlPq8w+Pmp9h/x9F6Vy+3oHBLP+2+pBoaJw= github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/brianvoe/gofakeit/v6 v6.23.2 h1:lVde18uhad5wII/f5RMVFLtdQNE0HaGFuBUXmYKk8i8= diff --git a/log/filter_test.go b/log/filter_test.go new file mode 100644 index 0000000..3b1b8c6 --- /dev/null +++ b/log/filter_test.go @@ -0,0 +1,49 @@ +package log + +import ( + "os" + "path/filepath" + "regexp" + "testing" + + "github.com/Laisky/zap" + "github.com/Laisky/zap/zapcore" + "github.com/stretchr/testify/require" +) + +func TestFilter(t *testing.T) { + dir, err := os.MkdirTemp("", "TestWriteToFile*") + if err != nil { + t.Fatal(err) + } + t.Logf("create directory: %v", dir) + defer os.RemoveAll(dir) + + file := filepath.Join(dir, "test.log") + logger, err := New( + WithOutputPaths([]string{file}), + WithZapOptions( + zap.Filter(func(e zapcore.Entry, f []zapcore.Field) bool { + return e.Message != "world" + }), + ), + ) + require.NoError(t, err) + + logger.Info("hello") + logger.Info("beautiful") + logger.Info("world") + logger.Info("how") + logger.Info("are") + logger.Info("you") + _ = logger.Sync() + + cntBytes, err := os.ReadFile(file) + content := string(cntBytes) + t.Logf("content:\n%s", content) + require.NoError(t, err) + require.Contains(t, content, "log/filter_test.go") + require.Contains(t, content, "hello\n") + require.NotContains(t, content, "world\n") + require.Len(t, regexp.MustCompile(`hello`).FindAllString(content, -1), 1) +}