Skip to content

Commit d879c4a

Browse files
authored
Merge pull request #120 from blinklabs-io/feat/plugin-logging
feat: plugin logging framework
2 parents 24df779 + dd3a638 commit d879c4a

File tree

22 files changed

+148
-17
lines changed

22 files changed

+148
-17
lines changed

filter/chainsync/chainsync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import (
2222
"github.com/blinklabs-io/gouroboros/ledger"
2323
"github.com/blinklabs-io/snek/event"
2424
"github.com/blinklabs-io/snek/input/chainsync"
25+
"github.com/blinklabs-io/snek/plugin"
2526
)
2627

2728
type ChainSync struct {
2829
errorChan chan error
2930
inputChan chan event.Event
3031
outputChan chan event.Event
32+
logger plugin.Logger
3133
filterAddresses []string
3234
filterAssetFingerprints []string
3335
filterPolicyIds []string

filter/chainsync/option.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414

1515
package chainsync
1616

17+
import "github.com/blinklabs-io/snek/plugin"
18+
1719
type ChainSyncOptionFunc func(*ChainSync)
1820

21+
// WithLogger specifies the logger object to use for logging messages
22+
func WithLogger(logger plugin.Logger) ChainSyncOptionFunc {
23+
return func(c *ChainSync) {
24+
c.logger = logger
25+
}
26+
}
27+
1928
// WithAddresses specfies the address to filter on
2029
func WithAddresses(addresses []string) ChainSyncOptionFunc {
2130
return func(c *ChainSync) {

filter/chainsync/plugin.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package chainsync
1717
import (
1818
"strings"
1919

20+
"github.com/blinklabs-io/snek/internal/logging"
2021
"github.com/blinklabs-io/snek/plugin"
2122
)
2223

@@ -73,7 +74,11 @@ func init() {
7374
}
7475

7576
func NewFromCmdlineOptions() plugin.Plugin {
76-
pluginOptions := []ChainSyncOptionFunc{}
77+
pluginOptions := []ChainSyncOptionFunc{
78+
WithLogger(
79+
logging.GetLogger().With("plugin", "filter.chainsync"),
80+
),
81+
}
7782
if cmdlineOptions.address != "" {
7883
pluginOptions = append(
7984
pluginOptions,

filter/event/event.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ package event
1616

1717
import (
1818
"github.com/blinklabs-io/snek/event"
19+
"github.com/blinklabs-io/snek/plugin"
1920
)
2021

2122
type Event struct {
2223
errorChan chan error
2324
inputChan chan event.Event
2425
outputChan chan event.Event
26+
logger plugin.Logger
2527
filterTypes []string
2628
}
2729

filter/event/option.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414

1515
package event
1616

17+
import "github.com/blinklabs-io/snek/plugin"
18+
1719
type EventOptionFunc func(*Event)
1820

21+
// WithLogger specifies the logger object to use for logging messages
22+
func WithLogger(logger plugin.Logger) EventOptionFunc {
23+
return func(e *Event) {
24+
e.logger = logger
25+
}
26+
}
27+
1928
// WithTypes specfies the event types to filter on
2029
func WithTypes(eventTypes []string) EventOptionFunc {
2130
return func(e *Event) {

filter/event/plugin.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package event
1717
import (
1818
"strings"
1919

20+
"github.com/blinklabs-io/snek/internal/logging"
2021
"github.com/blinklabs-io/snek/plugin"
2122
)
2223

@@ -46,7 +47,11 @@ func init() {
4647
}
4748

4849
func NewFromCmdlineOptions() plugin.Plugin {
49-
pluginOptions := []EventOptionFunc{}
50+
pluginOptions := []EventOptionFunc{
51+
WithLogger(
52+
logging.GetLogger().With("plugin", "filter.event"),
53+
),
54+
}
5055
if cmdlineOptions.eventType != "" {
5156
pluginOptions = append(
5257
pluginOptions,

input/chainsync/chainsync.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"time"
2121

2222
"github.com/blinklabs-io/snek/event"
23+
"github.com/blinklabs-io/snek/plugin"
2324

2425
ouroboros "github.com/blinklabs-io/gouroboros"
2526
"github.com/blinklabs-io/gouroboros/ledger"
@@ -30,6 +31,7 @@ import (
3031

3132
type ChainSync struct {
3233
oConn *ouroboros.Connection
34+
logger plugin.Logger
3335
network string
3436
networkMagic uint32
3537
address string
@@ -192,6 +194,9 @@ func (c *ChainSync) setupConnection() error {
192194
if err := c.oConn.Dial(dialFamily, dialAddress); err != nil {
193195
return err
194196
}
197+
if c.logger != nil {
198+
c.logger.Infof("connected to node at %s", dialAddress)
199+
}
195200
// Start async error handler
196201
go func() {
197202
err, ok := <-c.oConn.ErrorChan()

input/chainsync/options.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,36 @@ package chainsync
1616

1717
import (
1818
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
19+
"github.com/blinklabs-io/snek/plugin"
1920
)
2021

2122
type ChainSyncOptionFunc func(*ChainSync)
2223

24+
// WithLogger specifies the logger object to use for logging messages
25+
func WithLogger(logger plugin.Logger) ChainSyncOptionFunc {
26+
return func(c *ChainSync) {
27+
c.logger = logger
28+
}
29+
}
30+
2331
// WithNetwork specifies the network
2432
func WithNetwork(network string) ChainSyncOptionFunc {
25-
return func(o *ChainSync) {
26-
o.network = network
33+
return func(c *ChainSync) {
34+
c.network = network
2735
}
2836
}
2937

3038
// WithNetworkMagic specifies the network magic value
3139
func WithNetworkMagic(networkMagic uint32) ChainSyncOptionFunc {
32-
return func(o *ChainSync) {
33-
o.networkMagic = networkMagic
40+
return func(c *ChainSync) {
41+
c.networkMagic = networkMagic
3442
}
3543
}
3644

3745
// WithNtcTcp specifies whether to use the NtC (node-to-client) protocol over TCP. This is useful when exposing a node's UNIX socket via socat or similar. The default is to use the NtN (node-to-node) protocol over TCP
3846
func WithNtcTcp(ntcTcp bool) ChainSyncOptionFunc {
39-
return func(o *ChainSync) {
40-
o.ntcTcp = ntcTcp
47+
return func(c *ChainSync) {
48+
c.ntcTcp = ntcTcp
4149
}
4250
}
4351

input/chainsync/plugin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strconv"
2020
"strings"
2121

22+
"github.com/blinklabs-io/snek/internal/logging"
2223
"github.com/blinklabs-io/snek/plugin"
2324

2425
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
@@ -116,6 +117,9 @@ func init() {
116117

117118
func NewFromCmdlineOptions() plugin.Plugin {
118119
opts := []ChainSyncOptionFunc{
120+
WithLogger(
121+
logging.GetLogger().With("plugin", "input.chainsync"),
122+
),
119123
WithNetwork(cmdlineOptions.network),
120124
WithNetworkMagic(uint32(cmdlineOptions.networkMagic)),
121125
WithAddress(cmdlineOptions.address),

output/log/log.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,39 @@ package log
1717
import (
1818
"github.com/blinklabs-io/snek/event"
1919
"github.com/blinklabs-io/snek/internal/logging"
20+
"github.com/blinklabs-io/snek/plugin"
2021
)
2122

2223
type LogOutput struct {
23-
errorChan chan error
24-
eventChan chan event.Event
25-
logger *logging.Logger
26-
level string
24+
errorChan chan error
25+
eventChan chan event.Event
26+
logger plugin.Logger
27+
outputLogger *logging.Logger
28+
level string
2729
}
2830

2931
func New(options ...LogOptionFunc) *LogOutput {
3032
l := &LogOutput{
3133
errorChan: make(chan error),
3234
eventChan: make(chan event.Event, 10),
33-
logger: logging.GetLogger().With("type", "event"),
3435
level: "info",
3536
}
3637
for _, option := range options {
3738
option(l)
3839
}
40+
if l.logger == nil {
41+
l.logger = logging.GetLogger()
42+
}
43+
// Determine if we can use the provided logger or need our own
44+
// This is necessary because this plugin uses logger functions that aren't part
45+
// of the plugin.Logger interface
46+
switch v := l.logger.(type) {
47+
case *logging.Logger:
48+
l.outputLogger = v
49+
default:
50+
l.outputLogger = logging.GetLogger()
51+
}
52+
l.outputLogger = l.outputLogger.With("type", "event")
3953
return l
4054
}
4155

@@ -50,14 +64,14 @@ func (l *LogOutput) Start() error {
5064
}
5165
switch l.level {
5266
case "info":
53-
l.logger.Infow("", "event", evt)
67+
l.outputLogger.Infow("", "event", evt)
5468
case "warn":
55-
l.logger.Warnw("", "event", evt)
69+
l.outputLogger.Warnw("", "event", evt)
5670
case "error":
57-
l.logger.Errorw("", "event", evt)
71+
l.outputLogger.Errorw("", "event", evt)
5872
default:
5973
// Use INFO level if log level isn't recognized
60-
l.logger.Infow("", "event", evt)
74+
l.outputLogger.Infow("", "event", evt)
6175
}
6276
}
6377
}()

0 commit comments

Comments
 (0)