From b45956b84fce96e3ad7cc3bb3eb4042bf7005e43 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 22 Mar 2019 17:21:26 -0700 Subject: [PATCH] loop: unify top level logger, use lnd's build pacakge for logging --- client.go | 18 +++++++++--------- executor.go | 4 ++-- go.mod | 1 + log.go | 35 ++++++++++++++++++++--------------- loopout.go | 16 ++++++++-------- swap.go | 2 +- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/client.go b/client.go index 8489d18..8a9e93a 100644 --- a/client.go +++ b/client.go @@ -134,7 +134,7 @@ func (s *Client) Run(ctx context.Context, if err != nil { return fmt.Errorf("GetInfo error: %v", err) } - logger.Infof("Connected to lnd node %v with pubkey %v", + log.Infof("Connected to lnd node %v with pubkey %v", info.Alias, hex.EncodeToString(info.IdentityPubkey[:]), ) @@ -172,22 +172,22 @@ func (s *Client) Run(ctx context.Context, } if err != nil { - logger.Errorf("Swap client terminating: %v", err) + log.Errorf("Swap client terminating: %v", err) } else { - logger.Info("Swap client terminating") + log.Info("Swap client terminating") } // Cancel all remaining active goroutines. mainCancel() // Wait for all to finish. - logger.Debug("Wait for executor to finish") + log.Debug("Wait for executor to finish") s.executor.waitFinished() - logger.Debug("Wait for goroutines to finish") + log.Debug("Wait for goroutines to finish") s.wg.Wait() - logger.Info("Swap client terminated") + log.Info("Swap client terminated") return err } @@ -206,7 +206,7 @@ func (s *Client) resumeSwaps(ctx context.Context, } swap, err := resumeLoopOutSwap(ctx, swapCfg, pend) if err != nil { - logger.Errorf("resuming swap: %v", err) + log.Errorf("resuming swap: %v", err) continue } @@ -226,7 +226,7 @@ func (s *Client) resumeSwaps(ctx context.Context, func (s *Client) LoopOut(globalCtx context.Context, request *OutRequest) (*lntypes.Hash, error) { - logger.Infof("LoopOut %v to %v (channel: %v)", + log.Infof("LoopOut %v to %v (channel: %v)", request.Amount, request.DestAddr, request.LoopOutChannel, ) @@ -276,7 +276,7 @@ func (s *Client) LoopOutQuote(ctx context.Context, return nil, ErrSwapAmountTooHigh } - logger.Infof("Offchain swap destination: %x", terms.SwapPaymentDest) + log.Infof("Offchain swap destination: %x", terms.SwapPaymentDest) swapFee := swap.CalcFee( request.Amount, terms.SwapFeeBase, terms.SwapFeeRate, diff --git a/executor.go b/executor.go index 93126e9..bd8f05c 100644 --- a/executor.go +++ b/executor.go @@ -59,7 +59,7 @@ func (s *executor) run(mainCtx context.Context, // Before starting, make sure we have an up to date block height. // Otherwise we might reveal a preimage for a swap that is already // expired. - logger.Infof("Wait for first block ntfn") + log.Infof("Wait for first block ntfn") var height int32 setHeight := func(h int32) { @@ -77,7 +77,7 @@ func (s *executor) run(mainCtx context.Context, } // Start main event loop. - logger.Infof("Starting event loop at height %v", height) + log.Infof("Starting event loop at height %v", height) // Signal that executor being ready with an up to date block height. close(s.ready) diff --git a/go.mod b/go.mod index 332ceb4..e04c716 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/golang/protobuf v1.3.1 github.com/grpc-ecosystem/grpc-gateway v1.8.5 github.com/jessevdk/go-flags v1.4.0 + github.com/jrick/logrotate v1.0.0 github.com/lightninglabs/neutrino v0.0.0-20190314214430-643615b8c132 // indirect github.com/lightningnetwork/lnd v0.0.0-20190314214430-b4a1024ac74fc576c65c8074288a5ffaf6bd1ec4 github.com/lightningnetwork/lnd/queue v1.0.1 diff --git a/log.go b/log.go index 3423dd5..2482e12 100644 --- a/log.go +++ b/log.go @@ -2,28 +2,33 @@ package loop import ( "fmt" - "os" "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/lntypes" ) -// log is a logger that is initialized with no output filters. This -// means the package will not perform any logging by default until the caller -// requests it. -var ( - backendLog = btclog.NewBackend(logWriter{}) - logger = backendLog.Logger("CLIENT") - servicesLogger = backendLog.Logger("SERVICES") -) +// log is a logger that is initialized with no output filters. This means the +// package will not perform any logging by default until the caller requests +// it. +var log btclog.Logger + +// The default amount of logging is none. +func init() { + UseLogger(build.NewSubLogger("LOOP", nil)) +} -// logWriter implements an io.Writer that outputs to both standard output and -// the write-end pipe of an initialized log rotator. -type logWriter struct{} +// DisableLog disables all library log output. Logging output is disabled by +// default until UseLogger is called. +func DisableLog() { + UseLogger(btclog.Disabled) +} -func (logWriter) Write(p []byte) (n int, err error) { - os.Stdout.Write(p) - return len(p), nil +// UseLogger uses a specified Logger to output package logging info. This +// should be used in preference to SetLogWriter if the caller is also using +// btclog. +func UseLogger(logger btclog.Logger) { + log = logger } // SwapLog logs with a short swap hash prefix. diff --git a/loopout.go b/loopout.go index 065eaaf..b3b8e37 100644 --- a/loopout.go +++ b/loopout.go @@ -50,7 +50,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, // Generate random preimage. var swapPreimage [32]byte if _, err := rand.Read(swapPreimage[:]); err != nil { - logger.Error("Cannot generate preimage") + log.Error("Cannot generate preimage") } swapHash := lntypes.Hash(sha256.Sum256(swapPreimage[:])) @@ -66,7 +66,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, // Post the swap parameters to the swap server. The response contains // the server revocation key and the swap and prepay invoices. - logger.Infof("Initiating swap request at height %v", currentHeight) + log.Infof("Initiating swap request at height %v", currentHeight) swapResp, err := cfg.server.NewLoopOutSwap(globalCtx, swapHash, request.Amount, receiverKey, @@ -135,7 +135,7 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig, hash := lntypes.Hash(sha256.Sum256(pend.Contract.Preimage[:])) - logger.Infof("Resuming swap %v", hash) + log.Infof("Resuming swap %v", hash) swapKit, err := newSwapKit( hash, TypeOut, cfg, &pend.Contract.SwapContract, @@ -291,7 +291,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error { // Verify amount if preimage hasn't been revealed yet. if s.state != loopdb.StatePreimageRevealed && htlcValue < s.AmountRequested { - logger.Warnf("Swap amount too low, expected %v but received %v", + log.Warnf("Swap amount too low, expected %v but received %v", s.AmountRequested, htlcValue) s.state = loopdb.StateFailInsufficientValue @@ -468,7 +468,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) ( case notification := <-s.blockEpochChan: s.height = notification.(int32) - logger.Infof("Received block %v", s.height) + log.Infof("Received block %v", s.height) if checkMaxRevealHeightExceeded() { return nil, nil @@ -653,21 +653,21 @@ func validateLoopOutContract(lnd *lndclient.LndServices, swapFee := swapInvoiceAmt + prepayInvoiceAmt - request.Amount if swapFee > request.MaxSwapFee { - logger.Warnf("Swap fee %v exceeding maximum of %v", + log.Warnf("Swap fee %v exceeding maximum of %v", swapFee, request.MaxSwapFee) return ErrSwapFeeTooHigh } if prepayInvoiceAmt > request.MaxPrepayAmount { - logger.Warnf("Prepay amount %v exceeding maximum of %v", + log.Warnf("Prepay amount %v exceeding maximum of %v", prepayInvoiceAmt, request.MaxPrepayAmount) return ErrPrepayAmountTooHigh } if response.expiry-height < MinLoopOutPreimageRevealDelta { - logger.Warnf("Proposed expiry %v (delta %v) too soon", + log.Warnf("Proposed expiry %v (delta %v) too soon", response.expiry, response.expiry-height) return ErrExpiryTooSoon diff --git a/swap.go b/swap.go index 31f2a5d..cef2331 100644 --- a/swap.go +++ b/swap.go @@ -48,7 +48,7 @@ func newSwapKit(hash lntypes.Hash, swapType Type, cfg *swapConfig, log := &SwapLog{ Hash: hash, - Logger: logger, + Logger: log, } log.Infof("Htlc address: %v", htlcAddress)