Skip to content

Commit

Permalink
build: switch to slog Handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Sep 10, 2024
1 parent 7162568 commit 79e8549
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 54 deletions.
24 changes: 24 additions & 0 deletions build/handler_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,27 @@ func (r *reducedSet) WithGroup(name string) slog.Handler {

// A compile-time check to ensure that handlerSet implements slog.Handler.
var _ slog.Handler = (*reducedSet)(nil)

// subLogGenerator implements the SubLogCreator backed by a Handler.
type subLogGenerator struct {
handler Handler
}

// newSubLogGenerator constructs a new subLogGenerator from a Handler.
func newSubLogGenerator(handler Handler) *subLogGenerator {
return &subLogGenerator{
handler: handler,
}
}

// Logger returns a new logger for a particular sub-system.
//
// NOTE: this is part of the SubLogCreator interface.
func (b *subLogGenerator) Logger(subsystemTag string) btclog.Logger {
handler := b.handler.Subsystem(subsystemTag)

return btclog.NewSLogger(handler)
}

// A compile-time check to ensure that handlerSet implements slog.Handler.
var _ SubLogCreator = (*subLogGenerator)(nil)
15 changes: 2 additions & 13 deletions build/log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package build

import (
"io"
"os"

"github.com/btcsuite/btclog"
)
Expand Down Expand Up @@ -34,17 +34,6 @@ func (t LogType) String() string {
}
}

// LogWriter is a stub type whose behavior can be changed using the build flags
// "stdlog" and "nolog". The default behavior is to write to both stdout and the
// RotatorPipe. Passing "stdlog" will cause it only to write to stdout, and
// "nolog" implements Write as a no-op.
type LogWriter struct {
// RotatorPipe is the write-end pipe for writing to the log rotator. It
// is written to by the Write method of the LogWriter type. This only
// needs to be set if neither the stdlog or nolog builds are set.
RotatorPipe *io.PipeWriter
}

// NewSubLogger constructs a new subsystem log from the current LogWriter
// implementation. This is primarily intended for use with stdlog, as the actual
// writer is shared amongst all instantiations.
Expand Down Expand Up @@ -78,7 +67,7 @@ func NewSubLogger(subsystem string,
// that they share the same backend, since all output is written
// to std out.
case LogTypeStdOut:
backend := btclog.NewBackend(&LogWriter{})
backend := btclog.NewBackend(os.Stdout)
logger := backend.Logger(subsystem)

// Set the logging level of the stdout logger to use the
Expand Down
11 changes: 0 additions & 11 deletions build/log_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@

package build

import "os"

// LoggingType is a log type that writes to both stdout and the log rotator, if
// present.
const LoggingType = LogTypeDefault

// Write writes the byte slice to both stdout and the log rotator, if present.
func (w *LogWriter) Write(b []byte) (int, error) {
os.Stdout.Write(b)
if w.RotatorPipe != nil {
w.RotatorPipe.Write(b)
}
return len(b), nil
}
5 changes: 0 additions & 5 deletions build/log_nolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,3 @@ package build

// LoggingType is a log type that writes no logs.
const LoggingType = LogTypeNone

// Write is a noop.
func (w *LogWriter) Write(b []byte) (int, error) {
return len(b), nil
}
8 changes: 0 additions & 8 deletions build/log_stdlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@

package build

import "os"

// LoggingType is a log type that only writes to stdout.
const LoggingType = LogTypeStdOut

// Write writes the provided byte slice to stdout.
func (w *LogWriter) Write(b []byte) (int, error) {
os.Stdout.Write(b)
return len(b), nil
}
17 changes: 13 additions & 4 deletions build/logrotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
// RotatingLogWriter is a wrapper around the LogWriter that supports log file
// rotation.
type RotatingLogWriter struct {
logWriter *LogWriter
// pipe is the write-end pipe for writing to the log rotator
pipe *io.PipeWriter

rotator *rotator.Rotator
}
Expand All @@ -21,8 +22,8 @@ type RotatingLogWriter struct {
//
// NOTE: `InitLogRotator` must be called to set up log rotation after creating
// the writer.
func NewRotatingLogWriter(w *LogWriter) *RotatingLogWriter {
return &RotatingLogWriter{logWriter: w}
func NewRotatingLogWriter() *RotatingLogWriter {
return &RotatingLogWriter{}
}

// InitLogRotator initializes the log file rotator to write logs to logFile and
Expand Down Expand Up @@ -57,11 +58,19 @@ func (r *RotatingLogWriter) InitLogRotator(logFile string, maxLogFileSize int,
}
}()

r.logWriter.RotatorPipe = pw
r.pipe = pw

return nil
}

// Write writes the byte slice to both stdout and the log rotator, if present.
func (r *RotatingLogWriter) Write(b []byte) (int, error) {
if r.rotator != nil {
return r.rotator.Write(b)
}
return len(b), nil
}

// Close closes the underlying log rotator if it has already been created.
func (r *RotatingLogWriter) Close() error {
if r.rotator != nil {
Expand Down
9 changes: 5 additions & 4 deletions build/sub_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package build

import (
"fmt"
"io"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -30,10 +29,12 @@ type SubLoggerManager struct {
var _ LeveledSubLogger = (*SubLoggerManager)(nil)

// NewSubLoggerManager constructs a new SubLoggerManager.
func NewSubLoggerManager(w io.Writer) *SubLoggerManager {
func NewSubLoggerManager(handlers ...Handler) *SubLoggerManager {
return &SubLoggerManager{
loggers: SubLoggers{},
genLogger: btclog.NewBackend(w),
loggers: SubLoggers{},
genLogger: newSubLogGenerator(
newHandlerSet(btclog.LevelInfo, handlers...),
),
}
}

Expand Down
23 changes: 14 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ type Config struct {
// LogMgr is the root logger that all the daemon's subloggers are
// hooked up to.
LogMgr *build.SubLoggerManager
LogWriter *build.LogWriter
LogRotator *build.RotatingLogWriter

// networkDir is the path to the directory of the currently active
Expand Down Expand Up @@ -713,7 +712,7 @@ func DefaultConfig() Config {
MaxChannelFeeAllocation: htlcswitch.DefaultMaxLinkFeeAllocation,
MaxCommitFeeRateAnchors: lnwallet.DefaultAnchorsCommitMaxFeeRateSatPerVByte,
MaxFeeExposure: uint64(htlcswitch.DefaultMaxFeeExposure.ToSatoshis()),
LogWriter: &build.LogWriter{},
LogRotator: build.NewRotatingLogWriter(),
DB: lncfg.DefaultDB(),
Cluster: lncfg.DefaultCluster(),
RPCMiddleware: lncfg.DefaultRPCMiddleware(),
Expand Down Expand Up @@ -1435,14 +1434,20 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser,
lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name),
)

// A log writer must be passed in, otherwise we can't function and would
// run into a panic later on.
if cfg.LogWriter == nil {
return nil, mkErr("log writer missing in config")
var (
cliLogHander = build.NewCLIHandler(os.Stdout, false)
logHandlers []build.Handler
)
switch build.LoggingType {
case build.LogTypeStdOut:
logHandlers = []build.Handler{cliLogHander}
case build.LogTypeDefault:
logHandlers = []build.Handler{
cliLogHander,
build.NewLogFileHandler(cfg.LogRotator),
}
}

cfg.LogRotator = build.NewRotatingLogWriter(cfg.LogWriter)
cfg.LogMgr = build.NewSubLoggerManager(cfg.LogWriter)
cfg.LogMgr = build.NewSubLoggerManager(logHandlers...)

// Special show command to list supported subsystems and exit.
if cfg.DebugLevel == "show" {
Expand Down

0 comments on commit 79e8549

Please sign in to comment.