Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #81 from getAlby/feat/custom-log-level
Browse files Browse the repository at this point in the history
Feat: custom log levels
  • Loading branch information
rolznz authored Mar 4, 2024
2 parents 2ef343c + 3a8289c commit 38ccc0b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Breez SDK requires gcc to build the Breez bindings. Run `choco install mingw` an
- `DATABASE_URI`: a sqlite filename. Default: .data/nwc.db
- `PORT`: the port on which the app should listen on (default: 8080)
- `WORK_DIR`: directory to store NWC data files. Default: .data
- `LOG_LEVEL`: log level for the application. Higher is more verbose. Default: 4 (info)

### LND Backend parameters

Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type AppConfig struct {
Port string `envconfig:"PORT" default:"8080"`
DatabaseUri string `envconfig:"DATABASE_URI" default:".data/nwc.db"`
CookieSecret string `envconfig:"COOKIE_SECRET"`
LogLevel string `envconfig:"LOG_LEVEL"`
LDKLogLevel string `envconfig:"LDK_LOG_LEVEL"`
}

type Config struct {
Expand Down
15 changes: 15 additions & 0 deletions ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func NewLDKService(svc *Service, mnemonic, workDir string) (result lnclient.LNCl
}
config.ListeningAddresses = &listeningAddresses
config.LogDirPath = &logDirPath
logLevel, err := strconv.Atoi(svc.cfg.Env.LDKLogLevel)
if err == nil {
config.LogLevel = ldk_node.LogLevel(logLevel)
}
builder := ldk_node.BuilderFromConfig(config)
builder.SetEntropyBip39Mnemonic(mnemonic, nil)
builder.SetNetwork("bitcoin")
Expand Down Expand Up @@ -73,14 +77,19 @@ func NewLDKService(svc *Service, mnemonic, workDir string) (result lnclient.LNCl

subscribeLdkEvents := func() chan ldk_node.Event {
ldkEventHandler := make(chan ldk_node.Event)
svc.Logger.Debugf("Locking event handler mutex")
ldkEventHandlersMutex.Lock()
svc.Logger.Debugf("Locked event handler mutex")
ldkEventHandlers = append(ldkEventHandlers, ldkEventHandler)
ldkEventHandlersMutex.Unlock()
svc.Logger.Debugf("Unlocked event handler mutex")
return ldkEventHandler
}

unsubscribeLdkEvents := func(eventHandler chan ldk_node.Event) {
svc.Logger.Debugf("Locking event handler mutex")
ldkEventHandlersMutex.Lock()
svc.Logger.Debugf("Locked event handler mutex")
for i := 0; i < len(ldkEventHandlers); i++ {
if eventHandler == ldkEventHandlers[i] {
// Replace the element to be removed with the last element of the slice
Expand All @@ -91,6 +100,7 @@ func NewLDKService(svc *Service, mnemonic, workDir string) (result lnclient.LNCl
}
}
ldkEventHandlersMutex.Unlock()
svc.Logger.Debugf("Unlocked event handler mutex")
}

go func() {
Expand All @@ -105,12 +115,15 @@ func NewLDKService(svc *Service, mnemonic, workDir string) (result lnclient.LNCl
time.Sleep(time.Duration(1) * time.Millisecond)
continue
}
svc.Logger.Debugf("Locking event handler mutex")
ldkEventHandlersMutex.Lock()
svc.Logger.Debugf("Locked event handler mutex")
svc.Logger.Infof("Received LDK event %+v (%d listeners)", *event, len(ldkEventHandlers))
for _, eventHandler := range ldkEventHandlers {
eventHandler <- *event
}
ldkEventHandlersMutex.Unlock()
svc.Logger.Debugf("Unlocked event handler mutex")

node.EventHandled()
}
Expand Down Expand Up @@ -147,6 +160,7 @@ func (gs *LDKService) Shutdown() error {
}

func (gs *LDKService) SendPaymentSync(ctx context.Context, payReq string) (preimage string, err error) {
paymentStart := time.Now()
eventListener := gs.subscribeLdkEvents()
defer gs.unsubscribeLdkEvents(eventListener)

Expand Down Expand Up @@ -208,6 +222,7 @@ func (gs *LDKService) SendPaymentSync(ctx context.Context, payReq string) (preim
return "", errors.New("Payment timed out")
}

gs.svc.Logger.Infof("Payment made in %d ms", time.Since(paymentStart).Milliseconds())
return preimage, nil
}

Expand Down
7 changes: 6 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"slices"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -50,7 +51,11 @@ func NewService(ctx context.Context) (*Service, error) {
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.InfoLevel)
logLevel, err := strconv.Atoi(appConfig.LogLevel)
if err != nil {
logLevel = int(logrus.InfoLevel)
}
logger.SetLevel(logrus.Level(logLevel))

// make sure workdir exists
os.MkdirAll(appConfig.Workdir, os.ModePerm)
Expand Down

0 comments on commit 38ccc0b

Please sign in to comment.