mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-29 23:21:12 +02:00
build: update prefixed logger
Such that it implements the new expanded interface of btclog v2.
This commit is contained in:
@@ -1,18 +1,25 @@
|
|||||||
package build
|
package build
|
||||||
|
|
||||||
import "github.com/btcsuite/btclog"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
btclogv1 "github.com/btcsuite/btclog"
|
||||||
|
"github.com/btcsuite/btclog/v2"
|
||||||
|
)
|
||||||
|
|
||||||
// PrefixLog is a pass-through logger that adds a prefix to every logged line.
|
// PrefixLog is a pass-through logger that adds a prefix to every logged line.
|
||||||
type PrefixLog struct {
|
type PrefixLog struct {
|
||||||
log btclog.Logger
|
log btclog.Logger
|
||||||
prefix string
|
prefix string
|
||||||
|
attr []any
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPrefixLog instantiates a new prefixed logger.
|
// NewPrefixLog instantiates a new prefixed logger.
|
||||||
func NewPrefixLog(prefix string, log btclog.Logger) *PrefixLog {
|
func NewPrefixLog(prefix string, log btclog.Logger, attrs ...any) *PrefixLog {
|
||||||
return &PrefixLog{
|
return &PrefixLog{
|
||||||
prefix: prefix,
|
|
||||||
log: log,
|
log: log,
|
||||||
|
prefix: prefix,
|
||||||
|
attr: attrs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,10 +29,16 @@ func (p *PrefixLog) addFormatPrefix(s string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// addArgsPrefix prepends the prefix to a list of arguments.
|
// addArgsPrefix prepends the prefix to a list of arguments.
|
||||||
func (p *PrefixLog) addArgsPrefix(args []interface{}) []interface{} {
|
func (p *PrefixLog) addArgsPrefix(args []any) []any {
|
||||||
return append([]interface{}{p.prefix}, args...)
|
return append([]interface{}{p.prefix}, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mergeAttr merges the given set of attributes with any attributes that the
|
||||||
|
// logger was initialised with.
|
||||||
|
func (p *PrefixLog) mergeAttr(attrs []any) []any {
|
||||||
|
return append(append([]any{}, attrs...), p.attr...)
|
||||||
|
}
|
||||||
|
|
||||||
// Tracef formats message according to format specifier and writes to to log
|
// Tracef formats message according to format specifier and writes to to log
|
||||||
// with LevelTrace.
|
// with LevelTrace.
|
||||||
func (p *PrefixLog) Tracef(format string, params ...interface{}) {
|
func (p *PrefixLog) Tracef(format string, params ...interface{}) {
|
||||||
@@ -98,13 +111,55 @@ func (p *PrefixLog) Critical(v ...interface{}) {
|
|||||||
p.log.Critical(p.addArgsPrefix(v)...)
|
p.log.Critical(p.addArgsPrefix(v)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TraceS writes a structured log with the given message and key-value pair
|
||||||
|
// attributes with LevelTrace to the log.
|
||||||
|
func (p *PrefixLog) TraceS(ctx context.Context, msg string, attrs ...any) {
|
||||||
|
p.log.TraceS(ctx, p.addFormatPrefix(msg), p.mergeAttr(attrs)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DebugS writes a structured log with the given message and key-value pair
|
||||||
|
// attributes with LevelDebug to the log.
|
||||||
|
func (p *PrefixLog) DebugS(ctx context.Context, msg string, attrs ...any) {
|
||||||
|
p.log.DebugS(ctx, p.addFormatPrefix(msg), p.mergeAttr(attrs)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// InfoS writes a structured log with the given message and key-value pair
|
||||||
|
// attributes with LevelInfo to the log.
|
||||||
|
func (p *PrefixLog) InfoS(ctx context.Context, msg string, attrs ...any) {
|
||||||
|
p.log.InfoS(ctx, p.addFormatPrefix(msg), p.mergeAttr(attrs)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WarnS writes a structured log with the given message and key-value pair
|
||||||
|
// attributes with LevelWarn to the log.
|
||||||
|
func (p *PrefixLog) WarnS(ctx context.Context, msg string, err error,
|
||||||
|
attrs ...any) {
|
||||||
|
|
||||||
|
p.log.WarnS(ctx, p.addFormatPrefix(msg), err, p.mergeAttr(attrs)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrorS writes a structured log with the given message and key-value pair
|
||||||
|
// attributes with LevelError to the log.
|
||||||
|
func (p *PrefixLog) ErrorS(ctx context.Context, msg string, err error,
|
||||||
|
attrs ...any) {
|
||||||
|
|
||||||
|
p.log.ErrorS(ctx, p.addFormatPrefix(msg), err, p.mergeAttr(attrs)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CriticalS writes a structured log with the given message and key-value pair
|
||||||
|
// attributes with LevelCritical to the log.
|
||||||
|
func (p *PrefixLog) CriticalS(ctx context.Context, msg string, err error,
|
||||||
|
attrs ...any) {
|
||||||
|
|
||||||
|
p.log.CriticalS(ctx, p.addFormatPrefix(msg), err, p.mergeAttr(attrs)...)
|
||||||
|
}
|
||||||
|
|
||||||
// Level returns the current logging level.
|
// Level returns the current logging level.
|
||||||
func (p *PrefixLog) Level() btclog.Level {
|
func (p *PrefixLog) Level() btclogv1.Level {
|
||||||
return p.log.Level()
|
return p.log.Level()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLevel changes the logging level to the passed level.
|
// SetLevel changes the logging level to the passed level.
|
||||||
func (p *PrefixLog) SetLevel(level btclog.Level) {
|
func (p *PrefixLog) SetLevel(level btclogv1.Level) {
|
||||||
p.log.SetLevel(level)
|
p.log.SetLevel(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user