|
1 | 1 | btclog |
2 | 2 | ====== |
3 | 3 |
|
4 | | -[](https://travis-ci.org/btcsuite/btclog) |
5 | | -[](http://copyfree.org) |
6 | | -[](http://godoc.org/github.com/btcsuite/btclog) |
| 4 | +Forked and adapted from https://github.com/btcsuite/btclog. |
7 | 5 |
|
8 | 6 | Package btclog defines a logger interface and provides a default implementation |
9 | 7 | of a subsystem-aware leveled logger implementing the same interface. |
10 | 8 |
|
11 | 9 | ## Installation |
12 | 10 |
|
13 | 11 | ```bash |
14 | | -$ go get github.com/btcsuite/btclog |
| 12 | +$ go get github.com/lightninglabs/btclog |
15 | 13 | ``` |
16 | 14 |
|
17 | | -## GPG Verification Key |
| 15 | +## Usage |
18 | 16 |
|
19 | | -All official release tags are signed by Conformal so users can ensure the code |
20 | | -has not been tampered with and is coming from the btcsuite developers. To |
21 | | -verify the signature perform the following: |
| 17 | +`btclog.NewSLogger` can be used to construct a new `btclog.Logger` type which |
| 18 | +can then be used for logging calls. The `NewSLogger` function expects to be |
| 19 | +initialised with a type that implements the `btclog.Handler` interface which is |
| 20 | +responsible for writing logging records to a backend writer. Callers may provide |
| 21 | +their own `Handler` implementations (for example, the standard library `slog` |
| 22 | +package provides some handler implementations such as a JSON Handler and a Text |
| 23 | +Handler) or else they may use the default one provided with this package: |
| 24 | +`DefaultHandler`. |
22 | 25 |
|
23 | | -- Download the public key from the Conformal website at |
24 | | - https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt |
| 26 | +Example Usage: |
25 | 27 |
|
26 | | -- Import the public key into your GPG keyring: |
27 | | - ```bash |
28 | | - gpg --import GIT-GPG-KEY-conformal.txt |
29 | | - ``` |
| 28 | +``` |
| 29 | + // Create a new DefaultHandler that writes to stdout and set the |
| 30 | + // logging level to Trace. |
| 31 | + handler := btclog.NewDefaultHandler(os.Stdout) |
| 32 | + handler.SetLevel(btclog.LevelTrace) |
| 33 | +
|
| 34 | + // Use the above handler to construct a new logger. |
| 35 | + log := btclog.NewSLogger(handler) |
| 36 | +
|
| 37 | + /* |
| 38 | + 2024-09-18 11:53:03.287 [INF]: An info level log |
| 39 | + */ |
| 40 | + log.Info("An info level log") |
| 41 | +
|
| 42 | + // Create a subsystem logger with no timestamps. |
| 43 | + handler = btclog.NewDefaultHandler(os.Stdout, btclog.WithNoTimestamp()) |
| 44 | + log = btclog.NewSLogger(handler.SubSystem("SUBS")) |
| 45 | +
|
| 46 | + /* |
| 47 | + [INF] SUBS: An info level log |
| 48 | + */ |
| 49 | + log.Info("An info level log") |
30 | 50 |
|
31 | | -- Verify the release tag with the following command where `TAG_NAME` is a |
32 | | - placeholder for the specific tag: |
33 | | - ```bash |
34 | | - git tag -v TAG_NAME |
35 | | - ``` |
| 51 | + // Include log source. |
| 52 | + handler = btclog.NewDefaultHandler( |
| 53 | + os.Stdout, |
| 54 | + btclog.WithCallerFlags(btclog.Lshortfile), |
| 55 | + btclog.WithNoTimestamp(), |
| 56 | + ) |
| 57 | + log = btclog.NewSLogger(handler.SubSystem("SUBS")) |
| 58 | +
|
| 59 | + /* |
| 60 | + [INF] SUBS main.go:36: An info level log |
| 61 | + */ |
| 62 | + log.Info("An info level log") |
| 63 | +
|
| 64 | + // Attach attributes to a context. This will result in log lines |
| 65 | + // including these attributes if the context is passed to them. Also |
| 66 | + // pass in an attribute at log time. |
| 67 | + log = btclog.NewSLogger(btclog.NewDefaultHandler( |
| 68 | + os.Stdout, btclog.WithNoTimestamp(), |
| 69 | + ).SubSystem("SUBS")) |
| 70 | + ctx := btclog.WithCtx(context.Background(), "request_id", 5) |
| 71 | +
|
| 72 | + /* |
| 73 | + [INF] SUBS: A log line with context values request_id=5 another_key=another_value |
| 74 | + */ |
| 75 | + log.InfoS(ctx, "A log line with context values", "another_key", "another_value") |
| 76 | +``` |
36 | 77 |
|
37 | 78 | ## License |
38 | 79 |
|
|
0 commit comments