mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-29 15:11:09 +02:00
build: switch to slog Handlers
Start using the new slog handlers. With this commit we also remove the need for the LogWriter since we let our LogRotator implement io.Writer and pass that in to our log file handler.
This commit is contained in:
@@ -175,3 +175,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 btclog.Handler
|
||||
}
|
||||
|
||||
// newSubLogGenerator constructs a new subLogGenerator from a Handler.
|
||||
func newSubLogGenerator(handler btclog.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)
|
||||
|
21
build/log.go
21
build/log.go
@@ -1,9 +1,9 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/btcsuite/btclog"
|
||||
"github.com/btcsuite/btclog/v2"
|
||||
)
|
||||
|
||||
// LogType is an indicating the type of logging specified by the build flag.
|
||||
@@ -60,17 +60,6 @@ func SuportedLogCompressor(logCompressor string) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -104,8 +93,10 @@ func NewSubLogger(subsystem string,
|
||||
// that they share the same backend, since all output is written
|
||||
// to std out.
|
||||
case LogTypeStdOut:
|
||||
backend := btclog.NewBackend(&LogWriter{})
|
||||
logger := backend.Logger(subsystem)
|
||||
backend := btclog.NewDefaultHandler(os.Stdout)
|
||||
logger := btclog.NewSLogger(
|
||||
backend.SubSystem(subsystem),
|
||||
)
|
||||
|
||||
// Set the logging level of the stdout logger to use the
|
||||
// configured logging level specified by build flags.
|
||||
|
@@ -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,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
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"github.com/btcsuite/btclog"
|
||||
"github.com/btcsuite/btclog/v2"
|
||||
)
|
||||
|
||||
// ShutdownLogger wraps an existing logger with a shutdown function which will
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@ package build_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btclog"
|
||||
"github.com/btcsuite/btclog/v2"
|
||||
"github.com/lightningnetwork/lnd/build"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
@@ -14,7 +14,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
|
||||
}
|
||||
@@ -23,8 +24,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
|
||||
@@ -80,11 +81,20 @@ func (r *RotatingLogWriter) InitLogRotator(logFile, logCompressor string,
|
||||
}
|
||||
}()
|
||||
|
||||
r.logWriter.RotatorPipe = pw
|
||||
r.pipe = pw
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write writes the byte slice to 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 {
|
||||
|
@@ -2,12 +2,11 @@ package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/btcsuite/btclog"
|
||||
"github.com/btcsuite/btclog/v2"
|
||||
)
|
||||
|
||||
// SubLogCreator can be used to create a new logger for a particular subsystem.
|
||||
@@ -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 ...btclog.Handler) *SubLoggerManager {
|
||||
return &SubLoggerManager{
|
||||
loggers: SubLoggers{},
|
||||
genLogger: btclog.NewBackend(w),
|
||||
loggers: make(SubLoggers),
|
||||
genLogger: newSubLogGenerator(
|
||||
newHandlerSet(btclog.LevelInfo, handlers...),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user