-
Notifications
You must be signed in to change notification settings - Fork 78
slog: Support structured logging #17
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| ISC License | ||
|
|
||
| Copyright (c) 2013-2014 Conformal Systems LLC. | ||
| Copyright (c) 2024 The btcsuite developers. | ||
|
|
||
| Permission to use, copy, modify, and distribute this software for any | ||
| purpose with or without fee is hereby granted, provided that the above | ||
| copyright notice and this permission notice appear in all copies. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package btclog | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "log/slog" | ||
| ) | ||
|
|
||
| // Hex is a convenience function for a hex-encoded log attributes. | ||
| func Hex(key string, value []byte) slog.Attr { | ||
| h := hex.EncodeToString(value) | ||
|
|
||
| return slog.String(key, h) | ||
| } | ||
|
|
||
| type attrsKey struct{} | ||
|
|
||
| // WithCtx returns a copy of the context with which the logging attributes are | ||
| // associated. | ||
| // | ||
| // Usage: | ||
| // | ||
| // ctx := log.WithCtx(ctx, "height", 1234) | ||
| // ... | ||
| // log.Info(ctx, "Height processed") // Will contain attribute: height=1234 | ||
| func WithCtx(ctx context.Context, attrs ...any) context.Context { | ||
| return context.WithValue(ctx, attrsKey{}, mergeAttrs(ctx, attrs)) | ||
| } | ||
|
|
||
| // mergeAttrs returns the attributes from the context merged with the provided | ||
| // attributes. | ||
| func mergeAttrs(ctx context.Context, attrs []any) []any { | ||
| resp, _ := ctx.Value(attrsKey{}).([]any) // We know the type. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if that's the case maybe we can just panic here if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we dont want it to panic cause if nothing has been set yet (ie, there is nothing to append to), then ok will actually be false |
||
| resp = append(resp, attrs...) | ||
|
|
||
| return resp | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2022 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
ellemouton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // | ||
| // Copyright 2009 The Go Authors. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google LLC nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| // Adapted from go/src/log/slog/internal/buffer.go | ||
|
|
||
| package btclog | ||
|
|
||
| import "sync" | ||
|
|
||
| type buffer []byte | ||
|
|
||
| // bufferPool defines a concurrent safe free list of byte slices used to provide | ||
| // temporary buffers for formatting log messages prior to outputting them. | ||
| var bufferPool = sync.Pool{ | ||
| New: func() any { | ||
| b := make([]byte, 0, 1024) | ||
| return (*buffer)(&b) | ||
| }, | ||
| } | ||
|
|
||
| // newBuffer returns a byte slice from the free list. A new buffer is allocated | ||
| // if there are not any available on the free list. The returned byte slice | ||
| // should be returned to the fee list by using the recycleBuffer function when | ||
| // the caller is done with it. | ||
| func newBuffer() *buffer { | ||
| return bufferPool.Get().(*buffer) | ||
| } | ||
|
|
||
| // free puts the provided byte slice, which should have been obtained via the | ||
| // newBuffer function, back on the free list. | ||
| func (b *buffer) free() { | ||
| // To reduce peak allocation, return only smaller buffers to the pool. | ||
| const maxBufferSize = 16 << 10 | ||
| if cap(*b) <= maxBufferSize { | ||
| *b = (*b)[:0] | ||
| bufferPool.Put(b) | ||
| } | ||
| } | ||
|
|
||
| func (b *buffer) writeByte(p byte) { | ||
| *b = append(*b, p) | ||
| } | ||
|
|
||
| func (b *buffer) writeBytes(p []byte) { | ||
| *b = append(*b, p...) | ||
| } | ||
|
|
||
| func (b *buffer) writeString(s string) { | ||
| *b = append(*b, s...) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module github.com/btcsuite/btclog/v2 | ||
|
|
||
| require github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c | ||
|
|
||
| go 1.21 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c h1:4HxD1lBUGUddhzgaNgrCPsFWd7cGYNpeFUgd9ZIgyM0= | ||
| github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c/go.mod h1:w7xnGOhwT3lmrS4H3b/D1XAXxvh+tbhUm8xeHN2y3TQ= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you can describe a short how-to create a v2 library of a specific module in case we need it in the future.