Skip to content

Commit

Permalink
recommend: add log for subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
carlaKC committed Dec 19, 2019
1 parent d10cf1b commit 9c664f2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
34 changes: 29 additions & 5 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package terminator

import (
"github.com/btcsuite/btclog"
"github.com/lightninglabs/terminator/recommend"
"github.com/lightningnetwork/lnd/build"
)

// Subsystem defines the logging code for this subsystem.
const Subsystem = "TERM"

// 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
var (
logWriter = build.NewRotatingLogWriter()

// 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.
log = build.NewSubLogger(Subsystem, logWriter.GenSubLogger)
)

// The default amount of logging is none.
func init() {
UseLogger(build.NewSubLogger(Subsystem, nil))
setSubLogger(Subsystem, log, nil)
addSubLogger(recommend.Subsystem, recommend.UseLogger)
}

// UseLogger uses a specified Logger to output package logging info.
Expand All @@ -24,3 +30,21 @@ func init() {
func UseLogger(logger btclog.Logger) {
log = logger
}

// addSubLogger is a helper method to conveniently create and register the
// logger of a sub system.
func addSubLogger(subsystem string, useLogger func(btclog.Logger)) {
logger := build.NewSubLogger(subsystem, logWriter.GenSubLogger)
setSubLogger(subsystem, logger, useLogger)
}

// setSubLogger is a helper method to conveniently register the logger of a sub
// system.
func setSubLogger(subsystem string, logger btclog.Logger,
useLogger func(btclog.Logger)) {

logWriter.RegisterSubLogger(subsystem, logger)
if useLogger != nil {
useLogger(logger)
}
}
26 changes: 26 additions & 0 deletions recommend/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package recommend

import (
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/build"
)

// Subsystem defines the logging code for this subsystem.
const Subsystem = "RECM"

// 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(Subsystem, 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
}

0 comments on commit 9c664f2

Please sign in to comment.