-
Notifications
You must be signed in to change notification settings - Fork 9
build: add context-based logging infrastructure #24
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
395cd98
build: add log type enum and NewSubLogger
Roasbeef e59566a
build: add log level build tag files
Roasbeef a041e41
build: add log type build tag files
Roasbeef adfab23
build: add context-based logger utilities
Roasbeef d05e18a
build: add log parameter support to Makefile
Roasbeef d7cf785
docs: update testing commands with log parameter
Roasbeef 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
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,43 @@ | ||
| package build | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/btcsuite/btclog/v2" | ||
| ) | ||
|
|
||
| // loggerKey is the context key for storing a logger. Using an empty struct as | ||
| // the key type ensures that only this package can create keys of this type, | ||
| // preventing key collisions with other packages. | ||
| type loggerKey struct{} | ||
|
|
||
| // ContextWithLogger returns a new context with the given logger attached. This | ||
| // allows loggers to be propagated through the call stack via context, reducing | ||
| // the need to pass loggers as explicit function parameters. | ||
| func ContextWithLogger(ctx context.Context, log btclog.Logger) context.Context { | ||
| return context.WithValue(ctx, loggerKey{}, log) | ||
| } | ||
|
|
||
| // LoggerFromContext extracts a logger from the context. If no logger is | ||
| // present in the context, it returns btclog.Disabled which safely no-ops all | ||
| // log calls. This makes it safe to use in any context without nil checks. | ||
| func LoggerFromContext(ctx context.Context) btclog.Logger { | ||
| log, ok := ctx.Value(loggerKey{}).(btclog.Logger) | ||
| if !ok || log == nil { | ||
| return btclog.Disabled | ||
| } | ||
|
|
||
| return log | ||
| } | ||
|
|
||
| // MustLoggerFromContext extracts a logger from the context. If no logger is | ||
| // present, it panics. Use this only in code paths where you're certain a logger | ||
| // should have been added to the context earlier in the call chain. | ||
| func MustLoggerFromContext(ctx context.Context) btclog.Logger { | ||
| log, ok := ctx.Value(loggerKey{}).(btclog.Logger) | ||
| if !ok || log == nil { | ||
| panic("no logger in context") | ||
| } | ||
|
|
||
| return log | ||
| } |
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,102 @@ | ||
| package build | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/btcsuite/btclog/v2" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // TestContextWithLoggerRoundTrip tests that a logger can be stored in a context | ||
| // and retrieved successfully. | ||
| func TestContextWithLoggerRoundTrip(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Create a real logger for testing. | ||
| backend := btclog.NewDefaultHandler(os.Stdout) | ||
| logger := btclog.NewSLogger(backend.SubSystem("TEST")) | ||
|
|
||
| // Store the logger in a context. | ||
| ctx := ContextWithLogger(t.Context(), logger) | ||
|
|
||
| // Retrieve the logger from the context. | ||
| retrieved := LoggerFromContext(ctx) | ||
|
|
||
| // Verify we got the same logger back. | ||
| require.Equal(t, logger, retrieved) | ||
| } | ||
|
|
||
| // TestLoggerFromContextReturnsDisabledWhenMissing tests that LoggerFromContext | ||
| // returns btclog.Disabled when no logger is present in the context. | ||
| func TestLoggerFromContextReturnsDisabledWhenMissing(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Use an empty context with no logger. | ||
| ctx := t.Context() | ||
|
|
||
| // LoggerFromContext should return the disabled logger. | ||
| logger := LoggerFromContext(ctx) | ||
|
|
||
| require.Equal(t, btclog.Disabled, logger) | ||
| } | ||
|
|
||
| // TestLoggerFromContextReturnsDisabledWhenNil tests that LoggerFromContext | ||
| // returns btclog.Disabled when a nil logger was stored in the context. | ||
| func TestLoggerFromContextReturnsDisabledWhenNil(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Store a nil logger in the context. | ||
| ctx := ContextWithLogger(t.Context(), nil) | ||
|
|
||
| // LoggerFromContext should return the disabled logger. | ||
| logger := LoggerFromContext(ctx) | ||
|
|
||
| require.Equal(t, btclog.Disabled, logger) | ||
| } | ||
|
|
||
| // TestMustLoggerFromContextPanicsWhenMissing tests that MustLoggerFromContext | ||
| // panics when no logger is present in the context. | ||
| func TestMustLoggerFromContextPanicsWhenMissing(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Use an empty context with no logger. | ||
| ctx := t.Context() | ||
|
|
||
| // MustLoggerFromContext should panic. | ||
| require.Panics(t, func() { | ||
| MustLoggerFromContext(ctx) | ||
| }) | ||
| } | ||
|
|
||
| // TestMustLoggerFromContextPanicsWhenNil tests that MustLoggerFromContext | ||
| // panics when a nil logger was stored in the context. | ||
| func TestMustLoggerFromContextPanicsWhenNil(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Store a nil logger in the context. | ||
| ctx := ContextWithLogger(t.Context(), nil) | ||
|
|
||
| // MustLoggerFromContext should panic. | ||
| require.Panics(t, func() { | ||
| MustLoggerFromContext(ctx) | ||
| }) | ||
| } | ||
|
|
||
| // TestMustLoggerFromContextSucceeds tests that MustLoggerFromContext returns | ||
| // the logger when one is present. | ||
| func TestMustLoggerFromContextSucceeds(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Create a real logger for testing. | ||
| backend := btclog.NewDefaultHandler(os.Stdout) | ||
| logger := btclog.NewSLogger(backend.SubSystem("TEST")) | ||
|
|
||
| // Store the logger in a context. | ||
| ctx := ContextWithLogger(t.Context(), logger) | ||
|
|
||
| // MustLoggerFromContext should return the logger without panicking. | ||
| retrieved := MustLoggerFromContext(ctx) | ||
|
|
||
| require.Equal(t, logger, retrieved) | ||
| } |
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,110 @@ | ||
| package build | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/btcsuite/btclog/v2" | ||
| ) | ||
|
|
||
| // LogType is an indicating the type of logging specified by the build flag. | ||
| type LogType byte | ||
|
|
||
| const ( | ||
| // LogTypeNone indicates no logging. | ||
| LogTypeNone LogType = iota | ||
|
|
||
| // LogTypeStdOut all logging is written directly to stdout. | ||
| LogTypeStdOut | ||
|
|
||
| // LogTypeDefault logs to both stdout and a given io.PipeWriter. | ||
| LogTypeDefault | ||
| ) | ||
|
|
||
| // String returns a human readable identifier for the logging type. | ||
| func (t LogType) String() string { | ||
| switch t { | ||
| case LogTypeNone: | ||
| return "none" | ||
| case LogTypeStdOut: | ||
| return "stdout" | ||
| case LogTypeDefault: | ||
| return "default" | ||
| default: | ||
| return "unknown" | ||
| } | ||
| } | ||
|
|
||
| // Declare the supported log file compressors as exported consts for easier use | ||
| // from other projects. | ||
| const ( | ||
| // Gzip is the default compressor. | ||
| Gzip = "gzip" | ||
|
|
||
| // Zstd is a modern compressor that compresses better than Gzip, in less | ||
| // time. | ||
| Zstd = "zstd" | ||
| ) | ||
|
|
||
| // logCompressors maps the identifier for each supported compression algorithm | ||
| // to the extension used for the compressed log files. | ||
| var logCompressors = map[string]string{ | ||
| Gzip: "gz", | ||
| Zstd: "zst", | ||
| } | ||
|
|
||
| // SupportedLogCompressor returns whether or not logCompressor is a supported | ||
| // compression algorithm for log files. | ||
| func SupportedLogCompressor(logCompressor string) bool { | ||
| _, ok := logCompressors[logCompressor] | ||
|
|
||
| return ok | ||
| } | ||
|
|
||
| // 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. | ||
| func NewSubLogger(subsystem string, | ||
| genSubLogger func(string) btclog.Logger) btclog.Logger { | ||
|
|
||
| switch Deployment { | ||
| // For production builds, generate a new subsystem logger from the | ||
| // primary log backend. If no function is provided, logging will be | ||
| // disabled. | ||
| case Production: | ||
| if genSubLogger != nil { | ||
| return genSubLogger(subsystem) | ||
| } | ||
|
|
||
| // For development builds, we must handle two distinct types of logging: | ||
| // unit tests and running the live daemon, e.g. for integration testing. | ||
| case Development: | ||
| switch LoggingType { | ||
| // Default logging is used when running the standalone daemon. | ||
| // We'll use the optional sublogger constructor to mimic the | ||
| // production behavior. | ||
| case LogTypeDefault: | ||
| if genSubLogger != nil { | ||
| return genSubLogger(subsystem) | ||
| } | ||
|
|
||
| // Logging to stdout is used in unit tests. It is not important | ||
| // that they share the same backend, since all output is written | ||
| // to std out. | ||
| case LogTypeStdOut: | ||
| 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. | ||
| level, _ := btclog.LevelFromString(LogLevel) | ||
| logger.SetLevel(level) | ||
|
|
||
| return logger | ||
| } | ||
| } | ||
|
|
||
| // For any other configurations, we'll disable logging. | ||
| return btclog.Disabled | ||
| } | ||
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,8 @@ | ||
| //go:build !stdlog && !nolog | ||
| // +build !stdlog,!nolog | ||
|
|
||
| package build | ||
|
|
||
| // LoggingType is a log type that writes to both stdout and the log rotator, if | ||
| // present. | ||
| const LoggingType = LogTypeDefault |
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,7 @@ | ||
| //go:build nolog | ||
| // +build nolog | ||
|
|
||
| package build | ||
|
|
||
| // LoggingType is a log type that writes no logs. | ||
| const LoggingType = LogTypeNone |
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,7 @@ | ||
| //go:build stdlog | ||
| // +build stdlog | ||
|
|
||
| package build | ||
|
|
||
| // LoggingType is a log type that only writes to stdout. | ||
| const LoggingType = LogTypeStdOut |
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,7 @@ | ||
| //go:build dev && critical | ||
| // +build dev,critical | ||
|
|
||
| package build | ||
|
|
||
| // LogLevel specifies a critical log level. | ||
| var LogLevel = "critical" |
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,7 @@ | ||
| //go:build dev && debug | ||
| // +build dev,debug | ||
|
|
||
| package build | ||
|
|
||
| // LogLevel specifies a debug log level. | ||
| var LogLevel = "debug" |
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,7 @@ | ||
| //go:build !info && !debug && !trace && !warn && !error && !critical && !off | ||
| // +build !info,!debug,!trace,!warn,!error,!critical,!off | ||
|
|
||
| package build | ||
|
|
||
| // LogLevel specifies a default log level of info. | ||
| var LogLevel = "info" |
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,7 @@ | ||
| //go:build dev && error | ||
| // +build dev,error | ||
|
|
||
| package build | ||
|
|
||
| // LogLevel specifies an error log level. | ||
| var LogLevel = "error" |
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,7 @@ | ||
| //go:build dev && info | ||
| // +build dev,info | ||
|
|
||
| package build | ||
|
|
||
| // LogLevel specifies an info log level. | ||
| var LogLevel = "info" |
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,7 @@ | ||
| //go:build dev && off | ||
| // +build dev,off | ||
|
|
||
| package build | ||
|
|
||
| // LogLevel specifies logging is disabled. | ||
| var LogLevel = "off" |
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,7 @@ | ||
| //go:build dev && trace | ||
| // +build dev,trace | ||
|
|
||
| package build | ||
|
|
||
| // LogLevel specifies a trace log level. | ||
| var LogLevel = "trace" |
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.