build+sample_conf: option to print log call-site

This commit is contained in:
Elle Mouton
2024-10-15 18:23:18 +02:00
parent eddcdb2a5a
commit 30d39ac595
4 changed files with 85 additions and 19 deletions

View File

@ -5,6 +5,12 @@ package build
import "github.com/btcsuite/btclog/v2" import "github.com/btcsuite/btclog/v2"
const (
callSiteOff = "off"
callSiteShort = "short"
callSiteLong = "long"
)
// LogConfig holds logging configuration options. // LogConfig holds logging configuration options.
// //
//nolint:lll //nolint:lll
@ -16,8 +22,12 @@ type LogConfig struct {
// DefaultLogConfig returns the default logging config options. // DefaultLogConfig returns the default logging config options.
func DefaultLogConfig() *LogConfig { func DefaultLogConfig() *LogConfig {
return &LogConfig{ return &LogConfig{
Console: &LoggerConfig{}, Console: &LoggerConfig{
File: &LoggerConfig{}, CallSite: callSiteOff,
},
File: &LoggerConfig{
CallSite: callSiteOff,
},
} }
} }
@ -25,17 +35,31 @@ func DefaultLogConfig() *LogConfig {
// //
//nolint:lll //nolint:lll
type LoggerConfig struct { type LoggerConfig struct {
Disable bool `long:"disable" description:"Disable this logger."` Disable bool `long:"disable" description:"Disable this logger."`
NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."` NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."`
CallSite string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"`
} }
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the // HandlerOptions returns the set of btclog.HandlerOptions that the state of the
// config struct translates to. // config struct translates to.
func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption {
var opts []btclog.HandlerOption opts := []btclog.HandlerOption{
// The default skip depth used by the logging library is 6 but
// since we wrap the logging handlers with another level of
// abstraction with the handlerSet, we increase the skip depth
// to 7 here.
btclog.WithCallSiteSkipDepth(7),
}
if cfg.NoTimestamps { if cfg.NoTimestamps {
opts = append(opts, btclog.WithNoTimestamp()) opts = append(opts, btclog.WithNoTimestamp())
} }
switch cfg.CallSite {
case callSiteShort:
opts = append(opts, btclog.WithCallerFlags(btclog.Lshortfile))
case callSiteLong:
opts = append(opts, btclog.WithCallerFlags(btclog.Llongfile))
}
return opts return opts
} }

View File

@ -17,6 +17,10 @@ const (
faintSeq = "2" faintSeq = "2"
esc = '\x1b' esc = '\x1b'
csi = string(esc) + "[" csi = string(esc) + "["
callSiteOff = "off"
callSiteShort = "short"
callSiteLong = "long"
) )
// LogConfig holds logging configuration options. // LogConfig holds logging configuration options.
@ -27,22 +31,51 @@ type LogConfig struct {
File *LoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."` File *LoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
} }
// DefaultLogConfig returns the default logging config options.
func DefaultLogConfig() *LogConfig {
return &LogConfig{
Console: &consoleLoggerCfg{
LoggerConfig: LoggerConfig{
CallSite: callSiteShort,
},
},
File: &LoggerConfig{
CallSite: callSiteOff,
},
}
}
// LoggerConfig holds options for a particular logger. // LoggerConfig holds options for a particular logger.
// //
//nolint:lll //nolint:lll
type LoggerConfig struct { type LoggerConfig struct {
Disable bool `long:"disable" description:"Disable this logger."` Disable bool `long:"disable" description:"Disable this logger."`
NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."` NoTimestamps bool `long:"no-timestamps" description:"Omit timestamps from log lines."`
CallSite string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"`
} }
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the // HandlerOptions returns the set of btclog.HandlerOptions that the state of the
// config struct translates to. // config struct translates to.
func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption { func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption {
var opts []btclog.HandlerOption opts := []btclog.HandlerOption{
// The default skip depth used by the logging library is 6 but
// since we wrap the logging handlers with another level of
// abstraction with the handlerSet, we increase the skip depth
// to 7 here.
btclog.WithCallSiteSkipDepth(7),
}
if cfg.NoTimestamps { if cfg.NoTimestamps {
opts = append(opts, btclog.WithNoTimestamp()) opts = append(opts, btclog.WithNoTimestamp())
} }
switch cfg.CallSite {
case callSiteShort:
opts = append(opts, btclog.WithCallerFlags(btclog.Lshortfile))
case callSiteLong:
opts = append(opts, btclog.WithCallerFlags(btclog.Llongfile))
}
return opts return opts
} }
@ -87,14 +120,6 @@ func (cfg *consoleLoggerCfg) HandlerOptions() []btclog.HandlerOption {
) )
} }
// DefaultLogConfig returns the default logging config options.
func DefaultLogConfig() *LogConfig {
return &LogConfig{
Console: &consoleLoggerCfg{},
File: &LoggerConfig{},
}
}
func styleString(s string, styles ...string) string { func styleString(s string, styles ...string) string {
if len(styles) == 0 { if len(styles) == 0 {
return s return s

View File

@ -81,9 +81,12 @@
written to stdout and a new `--logging.file.disable` option to disable writing written to stdout and a new `--logging.file.disable` option to disable writing
logs to the standard log file. It also adds `--logging.console.no-timestamps` logs to the standard log file. It also adds `--logging.console.no-timestamps`
and `--logging.file.no-timestamps` which can be used to omit timestamps in and `--logging.file.no-timestamps` which can be used to omit timestamps in
log messages for the respective loggers. Finally, the new log messages for the respective loggers. The new `--logging.console.call-site`
`--logging.console.style` option can be used under the `dev` build tag to add and `--logging.file.call-site` options can be used to include the call-site of
styling to console logging. a log line. The options for this include "off" (default), "short" (source file
name and line number) and "long" (full path to source file and line number).
Finally, the new `--logging.console.style` option can be used under the `dev`
build tag to add styling to console logging.
## Breaking Changes ## Breaking Changes
## Performance Improvements ## Performance Improvements

View File

@ -978,12 +978,26 @@
; Don't add timestamps to logs written to stdout and stderr. ; Don't add timestamps to logs written to stdout and stderr.
; logging.console.no-timestamps=false ; logging.console.no-timestamps=false
; Include the log call-site in the log line written to stdout
; and stderr. Options include 'off', 'short' and 'long'.
; Default:
; logging.console.call-site=off
; Example:
; logging.console.call-site=short
; Disable logging to the standard LND log file. ; Disable logging to the standard LND log file.
; logging.file.disable=false ; logging.file.disable=false
; Don't add timestamps to logs written to the standard LND log file. ; Don't add timestamps to logs written to the standard LND log file.
; logging.file.no-timestamps=false ; logging.file.no-timestamps=false
; Include the log call-site in the log line written the standard LND
; log file. Options include 'off', 'short' and 'long'.
; Default:
; logging.file.call-site=off
; Example:
; logging.file.call-site=short
[watchtower] [watchtower]
; Enable integrated watchtower listening on :9911 by default. ; Enable integrated watchtower listening on :9911 by default.