Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: log events by default #389

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# only enable event logging in production
LOG_EVENTS=false

#WORK_DIR=.data
#DATABASE_URI=nwc.db
#NOSTR_PRIVKEY=
Expand All @@ -8,9 +11,6 @@
#FRONTEND_URL=http://localhost:5173
#AUTO_LINK_ALBY_ACCOUNT=false

# set LDK_GOSSIP_SOURCE as empty to not use RGS
#LDK_GOSSIP_SOURCE=

# Alby OAuth configuration
#ALBY_OAUTH_CLIENT_SECRET=
#ALBY_OAUTH_CLIENT_ID=
Expand Down
2 changes: 1 addition & 1 deletion config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type AppConfig struct {
AlbyOAuthAuthUrl string `envconfig:"ALBY_OAUTH_AUTH_URL" default:"https://getalby.com/oauth"`
BaseUrl string `envconfig:"BASE_URL" default:"http://localhost:8080"`
FrontendUrl string `envconfig:"FRONTEND_URL"`
LogEvents bool `envconfig:"LOG_EVENTS" default:"false"`
LogEvents bool `envconfig:"LOG_EVENTS" default:"true"`
AutoLinkAlbyAccount bool `envconfig:"AUTO_LINK_ALBY_ACCOUNT" default:"true"`
PhoenixdAddress string `envconfig:"PHOENIXD_ADDRESS"`
PhoenixdAuthorization string `envconfig:"PHOENIXD_AUTHORIZATION"`
Expand Down
26 changes: 13 additions & 13 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ func NewEventPublisher() *eventPublisher {
return eventPublisher
}

func (el *eventPublisher) RegisterSubscriber(listener EventSubscriber) {
el.subscriberMtx.Lock()
defer el.subscriberMtx.Unlock()
el.listeners = append(el.listeners, listener)
func (ep *eventPublisher) RegisterSubscriber(listener EventSubscriber) {
ep.subscriberMtx.Lock()
defer ep.subscriberMtx.Unlock()
ep.listeners = append(ep.listeners, listener)
}

func (el *eventPublisher) RemoveSubscriber(listenerToRemove EventSubscriber) {
el.subscriberMtx.Lock()
defer el.subscriberMtx.Unlock()
func (ep *eventPublisher) RemoveSubscriber(listenerToRemove EventSubscriber) {
ep.subscriberMtx.Lock()
defer ep.subscriberMtx.Unlock()

for i, listener := range el.listeners {
for i, listener := range ep.listeners {
// delete the listener from the listeners array
if listener == listenerToRemove {
el.listeners[i] = el.listeners[len(el.listeners)-1]
el.listeners = slices.Delete(el.listeners, len(el.listeners)-1, len(el.listeners))
ep.listeners[i] = ep.listeners[len(ep.listeners)-1]
ep.listeners = slices.Delete(ep.listeners, len(ep.listeners)-1, len(ep.listeners))
break
}
}
Expand All @@ -46,14 +46,14 @@ func (el *eventPublisher) RemoveSubscriber(listenerToRemove EventSubscriber) {
func (ep *eventPublisher) Publish(event *Event) {
ep.subscriberMtx.Lock()
defer ep.subscriberMtx.Unlock()
logger.Logger.WithFields(logrus.Fields{"event": event}).Info("Publishing event")
logger.Logger.WithFields(logrus.Fields{"event": event, "global": ep.globalProperties}).Info("Publishing event")
for _, listener := range ep.listeners {
// events are consumed in sequence as some listeners depend on earlier consumers
// (e.g. NIP-47 notifier depends on transactions service updating transactions)
listener.ConsumeEvent(context.Background(), event, ep.globalProperties)
}
}

func (el *eventPublisher) SetGlobalProperty(key string, value interface{}) {
el.globalProperties[key] = value
func (ep *eventPublisher) SetGlobalProperty(key string, value interface{}) {
ep.globalProperties[key] = value
}
3 changes: 2 additions & 1 deletion service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ func (svc *service) launchLNBackend(ctx context.Context, encryptionKey string) e
if err != nil {
logger.Logger.WithError(err).Error("Failed to fetch node info")
}
if info != nil && info.Pubkey != "" {
if info != nil {
svc.eventPublisher.SetGlobalProperty("node_id", info.Pubkey)
svc.eventPublisher.SetGlobalProperty("network", info.Network)
}

svc.eventPublisher.Publish(&events.Event{
Expand Down
Loading