Skip to content

Commit

Permalink
feat: plugin logging framework
Browse files Browse the repository at this point in the history
Fixes #119
  • Loading branch information
agaffney committed Nov 5, 2023
1 parent 24df779 commit dd3a638
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 17 deletions.
2 changes: 2 additions & 0 deletions filter/chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"github.com/blinklabs-io/gouroboros/ledger"
"github.com/blinklabs-io/snek/event"
"github.com/blinklabs-io/snek/input/chainsync"
"github.com/blinklabs-io/snek/plugin"
)

type ChainSync struct {
errorChan chan error
inputChan chan event.Event
outputChan chan event.Event
logger plugin.Logger
filterAddresses []string
filterAssetFingerprints []string
filterPolicyIds []string
Expand Down
9 changes: 9 additions & 0 deletions filter/chainsync/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@

package chainsync

import "github.com/blinklabs-io/snek/plugin"

type ChainSyncOptionFunc func(*ChainSync)

// WithLogger specifies the logger object to use for logging messages
func WithLogger(logger plugin.Logger) ChainSyncOptionFunc {
return func(c *ChainSync) {
c.logger = logger
}
}

// WithAddresses specfies the address to filter on
func WithAddresses(addresses []string) ChainSyncOptionFunc {
return func(c *ChainSync) {
Expand Down
7 changes: 6 additions & 1 deletion filter/chainsync/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package chainsync
import (
"strings"

"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"
)

Expand Down Expand Up @@ -73,7 +74,11 @@ func init() {
}

func NewFromCmdlineOptions() plugin.Plugin {
pluginOptions := []ChainSyncOptionFunc{}
pluginOptions := []ChainSyncOptionFunc{
WithLogger(
logging.GetLogger().With("plugin", "filter.chainsync"),
),
}
if cmdlineOptions.address != "" {
pluginOptions = append(
pluginOptions,
Expand Down
2 changes: 2 additions & 0 deletions filter/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ package event

import (
"github.com/blinklabs-io/snek/event"
"github.com/blinklabs-io/snek/plugin"
)

type Event struct {
errorChan chan error
inputChan chan event.Event
outputChan chan event.Event
logger plugin.Logger
filterTypes []string
}

Expand Down
9 changes: 9 additions & 0 deletions filter/event/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@

package event

import "github.com/blinklabs-io/snek/plugin"

type EventOptionFunc func(*Event)

// WithLogger specifies the logger object to use for logging messages
func WithLogger(logger plugin.Logger) EventOptionFunc {
return func(e *Event) {
e.logger = logger
}
}

// WithTypes specfies the event types to filter on
func WithTypes(eventTypes []string) EventOptionFunc {
return func(e *Event) {
Expand Down
7 changes: 6 additions & 1 deletion filter/event/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package event
import (
"strings"

"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"
)

Expand Down Expand Up @@ -46,7 +47,11 @@ func init() {
}

func NewFromCmdlineOptions() plugin.Plugin {
pluginOptions := []EventOptionFunc{}
pluginOptions := []EventOptionFunc{
WithLogger(
logging.GetLogger().With("plugin", "filter.event"),
),
}
if cmdlineOptions.eventType != "" {
pluginOptions = append(
pluginOptions,
Expand Down
5 changes: 5 additions & 0 deletions input/chainsync/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/blinklabs-io/snek/event"
"github.com/blinklabs-io/snek/plugin"

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

type ChainSync struct {
oConn *ouroboros.Connection
logger plugin.Logger
network string
networkMagic uint32
address string
Expand Down Expand Up @@ -192,6 +194,9 @@ func (c *ChainSync) setupConnection() error {
if err := c.oConn.Dial(dialFamily, dialAddress); err != nil {
return err
}
if c.logger != nil {
c.logger.Infof("connected to node at %s", dialAddress)
}
// Start async error handler
go func() {
err, ok := <-c.oConn.ErrorChan()
Expand Down
20 changes: 14 additions & 6 deletions input/chainsync/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,36 @@ package chainsync

import (
ocommon "github.com/blinklabs-io/gouroboros/protocol/common"
"github.com/blinklabs-io/snek/plugin"
)

type ChainSyncOptionFunc func(*ChainSync)

// WithLogger specifies the logger object to use for logging messages
func WithLogger(logger plugin.Logger) ChainSyncOptionFunc {
return func(c *ChainSync) {
c.logger = logger
}
}

// WithNetwork specifies the network
func WithNetwork(network string) ChainSyncOptionFunc {
return func(o *ChainSync) {
o.network = network
return func(c *ChainSync) {
c.network = network
}
}

// WithNetworkMagic specifies the network magic value
func WithNetworkMagic(networkMagic uint32) ChainSyncOptionFunc {
return func(o *ChainSync) {
o.networkMagic = networkMagic
return func(c *ChainSync) {
c.networkMagic = networkMagic
}
}

// 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
func WithNtcTcp(ntcTcp bool) ChainSyncOptionFunc {
return func(o *ChainSync) {
o.ntcTcp = ntcTcp
return func(c *ChainSync) {
c.ntcTcp = ntcTcp
}
}

Expand Down
4 changes: 4 additions & 0 deletions input/chainsync/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strconv"
"strings"

"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"

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

func NewFromCmdlineOptions() plugin.Plugin {
opts := []ChainSyncOptionFunc{
WithLogger(
logging.GetLogger().With("plugin", "input.chainsync"),
),
WithNetwork(cmdlineOptions.network),
WithNetworkMagic(uint32(cmdlineOptions.networkMagic)),
WithAddress(cmdlineOptions.address),
Expand Down
32 changes: 23 additions & 9 deletions output/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,39 @@ package log
import (
"github.com/blinklabs-io/snek/event"
"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"
)

type LogOutput struct {
errorChan chan error
eventChan chan event.Event
logger *logging.Logger
level string
errorChan chan error
eventChan chan event.Event
logger plugin.Logger
outputLogger *logging.Logger
level string
}

func New(options ...LogOptionFunc) *LogOutput {
l := &LogOutput{
errorChan: make(chan error),
eventChan: make(chan event.Event, 10),
logger: logging.GetLogger().With("type", "event"),
level: "info",
}
for _, option := range options {
option(l)
}
if l.logger == nil {
l.logger = logging.GetLogger()
}
// Determine if we can use the provided logger or need our own
// This is necessary because this plugin uses logger functions that aren't part
// of the plugin.Logger interface
switch v := l.logger.(type) {
case *logging.Logger:
l.outputLogger = v
default:
l.outputLogger = logging.GetLogger()
}
l.outputLogger = l.outputLogger.With("type", "event")
return l
}

Expand All @@ -50,14 +64,14 @@ func (l *LogOutput) Start() error {
}
switch l.level {
case "info":
l.logger.Infow("", "event", evt)
l.outputLogger.Infow("", "event", evt)
case "warn":
l.logger.Warnw("", "event", evt)
l.outputLogger.Warnw("", "event", evt)
case "error":
l.logger.Errorw("", "event", evt)
l.outputLogger.Errorw("", "event", evt)
default:
// Use INFO level if log level isn't recognized
l.logger.Infow("", "event", evt)
l.outputLogger.Infow("", "event", evt)
}
}
}()
Expand Down
9 changes: 9 additions & 0 deletions output/log/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@

package log

import "github.com/blinklabs-io/snek/plugin"

type LogOptionFunc func(*LogOutput)

// WithLogger specifies the logger object to use for logging messages
func WithLogger(logger plugin.Logger) LogOptionFunc {
return func(o *LogOutput) {
o.logger = logger
}
}

// WithLevel specifies the logging level
func WithLevel(level string) LogOptionFunc {
return func(o *LogOutput) {
Expand Down
4 changes: 4 additions & 0 deletions output/log/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package log

import (
"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"
)

Expand Down Expand Up @@ -44,6 +45,9 @@ func init() {

func NewFromCmdlineOptions() plugin.Plugin {
p := New(
WithLogger(
logging.GetLogger().With("plugin", "output.log"),
),
WithLevel(cmdlineOptions.level),
)
return p
Expand Down
2 changes: 2 additions & 0 deletions output/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import (

"github.com/blinklabs-io/snek/event"
"github.com/blinklabs-io/snek/input/chainsync"
"github.com/blinklabs-io/snek/plugin"
"github.com/gen2brain/beeep"
)

type NotifyOutput struct {
errorChan chan error
eventChan chan event.Event
logger plugin.Logger
title string
}

Expand Down
9 changes: 9 additions & 0 deletions output/notify/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@

package notify

import "github.com/blinklabs-io/snek/plugin"

// import "github.com/blinklabs-io/snek/event"

type NotifyOptionFunc func(*NotifyOutput)

// WithLogger specifies the logger object to use for logging messages
func WithLogger(logger plugin.Logger) NotifyOptionFunc {
return func(o *NotifyOutput) {
o.logger = logger
}
}

// WithTitle specifies the notification title
func WithTitle(title string) NotifyOptionFunc {
return func(o *NotifyOutput) {
Expand Down
4 changes: 4 additions & 0 deletions output/notify/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package notify

import (
"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"
)

Expand Down Expand Up @@ -44,6 +45,9 @@ func init() {

func NewFromCmdlineOptions() plugin.Plugin {
p := New(
WithLogger(
logging.GetLogger().With("plugin", "output.notify"),
),
WithTitle(cmdlineOptions.title),
)
return p
Expand Down
9 changes: 9 additions & 0 deletions output/push/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@

package push

import "github.com/blinklabs-io/snek/plugin"

type PushOptionFunc func(*PushOutput)

// WithLogger specifies the logger object to use for logging messages
func WithLogger(logger plugin.Logger) PushOptionFunc {
return func(o *PushOutput) {
o.logger = logger
}
}

func WithServiceAccountFilePath(serviceAccountFilePath string) PushOptionFunc {
return func(o *PushOutput) {
o.serviceAccountFilePath = serviceAccountFilePath
Expand Down
4 changes: 4 additions & 0 deletions output/push/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package push

import (
"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"
)

Expand Down Expand Up @@ -52,6 +53,9 @@ func init() {

func NewFromCmdlineOptions() plugin.Plugin {
p := New(
WithLogger(
logging.GetLogger().With("plugin", "output.push"),
),
WithAccessTokenUrl(cmdlineOptions.accessTokenUrl),
WithServiceAccountFilePath(cmdlineOptions.serviceAccountFilePath),
)
Expand Down
2 changes: 2 additions & 0 deletions output/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"github.com/blinklabs-io/snek/fcm"
"github.com/blinklabs-io/snek/input/chainsync"
"github.com/blinklabs-io/snek/internal/logging"
"github.com/blinklabs-io/snek/plugin"
"golang.org/x/oauth2/google"
)

type PushOutput struct {
errorChan chan error
eventChan chan event.Event
logger plugin.Logger
accessToken string
accessTokenUrl string
projectID string
Expand Down
Loading

0 comments on commit dd3a638

Please sign in to comment.