mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 14:11:04 +02:00
build: Append commit hash to the main LND context
It can be disabled via the new `logging.no-commit-hash` config option.
This commit is contained in:
@@ -25,8 +25,9 @@ const (
|
||||
//
|
||||
//nolint:ll
|
||||
type LogConfig struct {
|
||||
Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
|
||||
File *FileLoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
|
||||
Console *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
|
||||
File *FileLoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
|
||||
NoCommitHash bool `long:"no-commit-hash" description:"If set, the commit-hash of the current build will not be included in log lines by default."`
|
||||
}
|
||||
|
||||
// Validate validates the LogConfig struct values.
|
||||
|
@@ -6,9 +6,13 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"github.com/btcsuite/btclog/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -101,3 +105,28 @@ func Tags() []string {
|
||||
|
||||
return strings.Split(RawTags, ",")
|
||||
}
|
||||
|
||||
// WithBuildInfo derives a child context with the build information attached as
|
||||
// attributes. At the moment, this only includes the current build's commit
|
||||
// hash.
|
||||
func WithBuildInfo(ctx context.Context, cfg *LogConfig) (context.Context,
|
||||
error) {
|
||||
|
||||
if cfg.NoCommitHash {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
// Convert the commit hash to a byte slice.
|
||||
commitHash, err := hex.DecodeString(CommitHash)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to decode commit hash: %w", err)
|
||||
}
|
||||
|
||||
// Include the first 3 bytes of the commit hash in the context as an
|
||||
// slog attribute.
|
||||
if len(commitHash) > 3 {
|
||||
commitHash = commitHash[:3]
|
||||
}
|
||||
|
||||
return btclog.WithCtx(ctx, btclog.Hex("rev", commitHash)), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user