-
Notifications
You must be signed in to change notification settings - Fork 2.2k
log: structured logging #9083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
log: structured logging #9083
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ec5b39c
build: move LevelSubLogger to own file
ellemouton 387a1a8
build: separate sublogger and rotator pipe management
ellemouton 9a97610
build: add handler set implementations
ellemouton cd69791
build: switch to slog Handlers
ellemouton 5ed7bf1
config: add logging config options
ellemouton cfa7fce
build+config: add default handler constructor
ellemouton a8da3e5
build: add styling option for console logger
ellemouton 23602e0
multi: start updating various loggers to use the new v2 type
ellemouton ba1ce84
build: update prefixed logger
ellemouton 49bfbec
build: add CriticalS to ShutdownLogger
ellemouton 9d0cd3f
multi: update more loggers to the v2 type
ellemouton eddcdb2
docs: add release note entry
ellemouton 30d39ac
build+sample_conf: option to print log call-site
ellemouton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//go:build !dev | ||
// +build !dev | ||
|
||
package build | ||
|
||
import "github.com/btcsuite/btclog/v2" | ||
|
||
const ( | ||
callSiteOff = "off" | ||
callSiteShort = "short" | ||
callSiteLong = "long" | ||
) | ||
|
||
// LogConfig holds logging configuration options. | ||
// | ||
//nolint:lll | ||
type LogConfig struct { | ||
Console *LoggerConfig `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."` | ||
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: &LoggerConfig{ | ||
CallSite: callSiteOff, | ||
}, | ||
File: &LoggerConfig{ | ||
CallSite: callSiteOff, | ||
}, | ||
} | ||
} | ||
|
||
// LoggerConfig holds options for a particular logger. | ||
// | ||
//nolint:lll | ||
type LoggerConfig struct { | ||
Disable bool `long:"disable" description:"Disable this logger."` | ||
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 | ||
// config struct translates to. | ||
func (cfg *LoggerConfig) HandlerOptions() []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), | ||
ziggie1984 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if cfg.NoTimestamps { | ||
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
//go:build dev | ||
// +build dev | ||
|
||
package build | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
btclogv1 "github.com/btcsuite/btclog" | ||
"github.com/btcsuite/btclog/v2" | ||
) | ||
|
||
const ( | ||
resetSeq = "0" | ||
ziggie1984 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
boldSeq = "1" | ||
faintSeq = "2" | ||
esc = '\x1b' | ||
csi = string(esc) + "[" | ||
|
||
callSiteOff = "off" | ||
callSiteShort = "short" | ||
callSiteLong = "long" | ||
) | ||
|
||
// LogConfig holds logging configuration options. | ||
// | ||
//nolint:lll | ||
type LogConfig struct { | ||
Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."` | ||
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. | ||
// | ||
//nolint:lll | ||
type LoggerConfig struct { | ||
Disable bool `long:"disable" description:"Disable this logger."` | ||
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 | ||
// config struct translates to. | ||
func (cfg *LoggerConfig) HandlerOptions() []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), | ||
ziggie1984 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if cfg.NoTimestamps { | ||
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 | ||
} | ||
|
||
// consoleLoggerCfg extends the LoggerConfig struct by adding a Color option | ||
// which is only available for a console logger. | ||
// | ||
//nolint:lll | ||
type consoleLoggerCfg struct { | ||
LoggerConfig | ||
Style bool `long:"style" description:"If set, the output will be styled with color and fonts"` | ||
} | ||
|
||
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the | ||
// config struct translates to. | ||
func (cfg *consoleLoggerCfg) HandlerOptions() []btclog.HandlerOption { | ||
opts := cfg.LoggerConfig.HandlerOptions() | ||
|
||
if !cfg.Style { | ||
return opts | ||
} | ||
|
||
return append( | ||
opts, btclog.WithStyledLevel( | ||
func(l btclogv1.Level) string { | ||
return styleString( | ||
fmt.Sprintf("[%s]", l), | ||
boldSeq, | ||
string(ansiColoSeq(l)), | ||
) | ||
}, | ||
), | ||
btclog.WithStyledCallSite( | ||
func(file string, line int) string { | ||
str := fmt.Sprintf("%s:%d", file, line) | ||
|
||
return styleString(str, faintSeq) | ||
}, | ||
), | ||
btclog.WithStyledKeys(func(key string) string { | ||
return styleString(key, faintSeq) | ||
}), | ||
) | ||
} | ||
|
||
func styleString(s string, styles ...string) string { | ||
if len(styles) == 0 { | ||
return s | ||
} | ||
|
||
seq := strings.Join(styles, ";") | ||
if seq == "" { | ||
return s | ||
} | ||
|
||
return fmt.Sprintf("%s%sm%s%sm", csi, seq, s, csi+resetSeq) | ||
} | ||
|
||
type ansiColorSeq string | ||
|
||
const ( | ||
ansiColorSeqDarkTeal ansiColorSeq = "38;5;30" | ||
ansiColorSeqDarkBlue ansiColorSeq = "38;5;63" | ||
ansiColorSeqLightBlue ansiColorSeq = "38;5;86" | ||
ansiColorSeqYellow ansiColorSeq = "38;5;192" | ||
ansiColorSeqRed ansiColorSeq = "38;5;204" | ||
ansiColorSeqPink ansiColorSeq = "38;5;134" | ||
) | ||
|
||
func ansiColoSeq(l btclogv1.Level) ansiColorSeq { | ||
switch l { | ||
case btclog.LevelTrace: | ||
return ansiColorSeqDarkTeal | ||
case btclog.LevelDebug: | ||
return ansiColorSeqDarkBlue | ||
case btclog.LevelWarn: | ||
return ansiColorSeqYellow | ||
case btclog.LevelError: | ||
return ansiColorSeqRed | ||
case btclog.LevelCritical: | ||
return ansiColorSeqPink | ||
default: | ||
return ansiColorSeqLightBlue | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.