Skip to content

Commit b41013b

Browse files
newm4nFerdinand Neman
andauthored
Noop and ZeroLog logger (#486)
* Initial logging, added Noop and initial Zero log * Added ZeroLog integration into the logging frame work, Also added NoOp for thos without logging --------- Co-authored-by: Ferdinand Neman <[email protected]>
1 parent 0756fc9 commit b41013b

File tree

10 files changed

+298
-7
lines changed

10 files changed

+298
-7
lines changed

antlr/GruleParserV3Listener.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package antlr
1616

1717
import (
1818
"fmt"
19+
"github.com/rs/zerolog"
1920
"github.com/sirupsen/logrus"
2021
"go.uber.org/zap"
2122
"strconv"
@@ -56,6 +57,12 @@ func SetLogger(log interface{}) {
5657
return
5758
}
5859
entry = logger.NewLogrus(log)
60+
case *zerolog.Logger:
61+
log, ok := log.(*zerolog.Logger)
62+
if !ok {
63+
return
64+
}
65+
entry = logger.NewZero(log)
5966
default:
6067
return
6168
}

ast/Ast.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package ast
1616

1717
import (
1818
"github.com/hyperjumptech/grule-rule-engine/logger"
19+
"github.com/rs/zerolog"
1920
"github.com/sirupsen/logrus"
2021
"go.uber.org/zap"
2122
)
@@ -78,6 +79,12 @@ func SetLogger(log interface{}) {
7879
return
7980
}
8081
entry = logger.NewLogrus(log)
82+
case *zerolog.Logger:
83+
log, ok := log.(*zerolog.Logger)
84+
if !ok {
85+
return
86+
}
87+
entry = logger.NewZero(log)
8188
default:
8289

8390
return

builder/RuleBuilder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"github.com/hyperjumptech/grule-rule-engine/ast"
2020
"github.com/hyperjumptech/grule-rule-engine/logger"
21+
"github.com/rs/zerolog"
2122
"github.com/sirupsen/logrus"
2223
"go.uber.org/zap"
2324
"time"
@@ -57,6 +58,12 @@ func SetLogger(log interface{}) {
5758
return
5859
}
5960
entry = logger.NewLogrus(log)
61+
case *zerolog.Logger:
62+
log, ok := log.(*zerolog.Logger)
63+
if !ok {
64+
return
65+
}
66+
entry = logger.NewZero(log)
6067
default:
6168

6269
return

engine/GruleEngine.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package engine
1717
import (
1818
"context"
1919
"fmt"
20+
"github.com/rs/zerolog"
2021
"github.com/sirupsen/logrus"
2122
"go.uber.org/zap"
2223
"sort"
@@ -59,6 +60,12 @@ func SetLogger(externalLog interface{}) {
5960
return
6061
}
6162
entry = logger.NewLogrus(log)
63+
case *zerolog.Logger:
64+
log, ok := externalLog.(*zerolog.Logger)
65+
if !ok {
66+
return
67+
}
68+
entry = logger.NewZero(log)
6269
default:
6370

6471
return
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright DataWiseHQ/grule-rule-engine Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package examples
16+
17+
import (
18+
"bytes"
19+
"github.com/hyperjumptech/grule-rule-engine/ast"
20+
"github.com/hyperjumptech/grule-rule-engine/builder"
21+
"github.com/hyperjumptech/grule-rule-engine/engine"
22+
"github.com/hyperjumptech/grule-rule-engine/pkg"
23+
"github.com/rs/zerolog"
24+
"github.com/stretchr/testify/assert"
25+
"testing"
26+
)
27+
28+
const zerologRule = `
29+
rule HelloWorld "Prints hello" {
30+
when
31+
true
32+
then
33+
Log("zerolog hello from grule");
34+
Retract("HelloWorld");
35+
}
36+
`
37+
38+
func TestZerologIntegration(t *testing.T) {
39+
var buf bytes.Buffer
40+
41+
zl := zerolog.New(&buf).Level(zerolog.InfoLevel).With().Timestamp().Logger()
42+
43+
engine.SetLogger(&zl)
44+
45+
dataContext := ast.NewDataContext()
46+
lib := ast.NewKnowledgeLibrary()
47+
ruleBuilder := builder.NewRuleBuilder(lib)
48+
49+
err := ruleBuilder.BuildRuleFromResource("HelloWorld", "0.0.1", pkg.NewBytesResource([]byte(zerologRule)))
50+
assert.NoError(t, err)
51+
52+
kb, err := lib.NewKnowledgeBaseInstance("HelloWorld", "0.0.1")
53+
assert.NoError(t, err)
54+
55+
grule := engine.NewGruleEngine()
56+
err = grule.Execute(dataContext, kb)
57+
assert.NoError(t, err)
58+
59+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ require (
2727
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
2828
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
2929
github.com/kevinburke/ssh_config v1.2.0 // indirect
30+
github.com/mattn/go-colorable v0.1.14 // indirect
31+
github.com/mattn/go-isatty v0.0.20 // indirect
3032
github.com/pjbgf/sha1cd v0.3.2 // indirect
3133
github.com/pmezard/go-difflib v1.0.0 // indirect
3234
github.com/rs/cors v1.11.1 // indirect
35+
github.com/rs/zerolog v1.34.0 // indirect
3336
github.com/sergi/go-diff v1.4.0 // indirect
3437
github.com/skeema/knownhosts v1.3.1 // indirect
3538
github.com/xanzy/ssh-agent v0.3.3 // indirect

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQ
1717
github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE=
1818
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
1919
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
20+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
2021
github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
2122
github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
2223
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -36,6 +37,7 @@ github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMj
3637
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
3738
github.com/go-git/go-git/v5 v5.16.2 h1:fT6ZIOjE5iEnkzKyxTHK1W4HGAsPhqEqiSAssSO77hM=
3839
github.com/go-git/go-git/v5 v5.16.2/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
40+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
3941
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
4042
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
4143
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
@@ -55,6 +57,13 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
5557
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
5658
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
5759
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
60+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
61+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
62+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
63+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
64+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
65+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
66+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
5867
github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k=
5968
github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY=
6069
github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
@@ -67,6 +76,9 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
6776
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
6877
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
6978
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
79+
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
80+
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
81+
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
7082
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
7183
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
7284
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
@@ -103,6 +115,9 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
103115
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
104116
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
105117
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
118+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
119+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
120+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
106121
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
107122
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
108123
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=

logger/Logger.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package logger
1616

1717
import (
18+
"github.com/rs/zerolog"
1819
"github.com/sirupsen/logrus"
1920
"go.uber.org/zap"
2021
)
@@ -80,16 +81,13 @@ var (
8081
)
8182

8283
func init() {
83-
logger := logrus.New()
84-
logger.Level = logrus.InfoLevel
85-
86-
Log = LogEntry{
87-
Logger: NewLogrus(logger).WithFields(Fields{"lib": "grule-rule-engine"}),
88-
Level: DebugLevel,
89-
}
84+
Log = NewNoopLogger()
9085
}
9186

9287
// SetLogger changes default logger on external
88+
//
89+
// logrusLogger := logrus.New()
90+
// SetLogger(logrusLogger)
9391
func SetLogger(externalLog interface{}) {
9492
switch externalLog.(type) {
9593
case *zap.Logger:
@@ -106,6 +104,13 @@ func SetLogger(externalLog interface{}) {
106104
return
107105
}
108106
Log = NewLogrus(log)
107+
case *zerolog.Logger:
108+
log, ok := externalLog.(*zerolog.Logger)
109+
if !ok {
110+
111+
return
112+
}
113+
Log = NewZero(log)
109114
default:
110115

111116
return

logger/Noop.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright hyperjumptech/grule-rule-engine Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package logger
16+
17+
type NoopLogger struct{}
18+
19+
func NewNoopLogger() LogEntry {
20+
noop := &NoopLogger{}
21+
return noop.WithFields(nil)
22+
}
23+
24+
func (logger *NoopLogger) Debug(args ...interface{}) {}
25+
func (logger *NoopLogger) Info(args ...interface{}) {}
26+
func (logger *NoopLogger) Warn(args ...interface{}) {}
27+
func (logger *NoopLogger) Error(args ...interface{}) {}
28+
func (logger *NoopLogger) Panic(args ...interface{}) {}
29+
func (logger *NoopLogger) Fatal(args ...interface{}) {}
30+
31+
func (logger *NoopLogger) Debugf(template string, args ...interface{}) {}
32+
func (logger *NoopLogger) Infof(template string, args ...interface{}) {}
33+
func (logger *NoopLogger) Warnf(template string, args ...interface{}) {}
34+
func (logger *NoopLogger) Errorf(template string, args ...interface{}) {}
35+
func (logger *NoopLogger) Panicf(template string, args ...interface{}) {}
36+
func (logger *NoopLogger) Fatalf(template string, args ...interface{}) {}
37+
38+
func (logger *NoopLogger) Trace(args ...interface{}) {}
39+
func (logger *NoopLogger) Tracef(format string, args ...interface{}) {}
40+
41+
func (logger *NoopLogger) Print(args ...interface{}) {}
42+
func (logger *NoopLogger) Println(args ...interface{}) {}
43+
func (logger *NoopLogger) Printf(format string, args ...interface{}) {}
44+
45+
func (logger *NoopLogger) WithFields(keyValues Fields) LogEntry {
46+
return LogEntry{
47+
Logger: logger,
48+
Level: TraceLevel,
49+
}
50+
}

0 commit comments

Comments
 (0)