Skip to content

Commit 3428138

Browse files
authored
Merge pull request #17 from ellemouton/slog
slog: Support structured logging
2 parents 09c4e92 + e9988e6 commit 3428138

File tree

11 files changed

+1386
-0
lines changed

11 files changed

+1386
-0
lines changed

v2/LICENSE

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ISC License
2+
3+
Copyright (c) 2013-2014 Conformal Systems LLC.
4+
Copyright (c) 2024 The btcsuite developers.
5+
6+
Permission to use, copy, modify, and distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

v2/attrs.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package btclog
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"log/slog"
7+
)
8+
9+
// Hex is a convenience function for a hex-encoded log attributes.
10+
func Hex(key string, value []byte) slog.Attr {
11+
h := hex.EncodeToString(value)
12+
13+
return slog.String(key, h)
14+
}
15+
16+
type attrsKey struct{}
17+
18+
// WithCtx returns a copy of the context with which the logging attributes are
19+
// associated.
20+
//
21+
// Usage:
22+
//
23+
// ctx := log.WithCtx(ctx, "height", 1234)
24+
// ...
25+
// log.Info(ctx, "Height processed") // Will contain attribute: height=1234
26+
func WithCtx(ctx context.Context, attrs ...any) context.Context {
27+
return context.WithValue(ctx, attrsKey{}, mergeAttrs(ctx, attrs))
28+
}
29+
30+
// mergeAttrs returns the attributes from the context merged with the provided
31+
// attributes.
32+
func mergeAttrs(ctx context.Context, attrs []any) []any {
33+
resp, _ := ctx.Value(attrsKey{}).([]any) // We know the type.
34+
resp = append(resp, attrs...)
35+
36+
return resp
37+
}

v2/buffer.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// Copyright 2009 The Go Authors.
6+
//
7+
// Redistribution and use in source and binary forms, with or without
8+
// modification, are permitted provided that the following conditions are
9+
// met:
10+
//
11+
// * Redistributions of source code must retain the above copyright
12+
// notice, this list of conditions and the following disclaimer.
13+
// * Redistributions in binary form must reproduce the above
14+
// copyright notice, this list of conditions and the following disclaimer
15+
// in the documentation and/or other materials provided with the
16+
// distribution.
17+
// * Neither the name of Google LLC nor the names of its
18+
// contributors may be used to endorse or promote products derived from
19+
// this software without specific prior written permission.
20+
//
21+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
// Adapted from go/src/log/slog/internal/buffer.go
34+
35+
package btclog
36+
37+
import "sync"
38+
39+
type buffer []byte
40+
41+
// bufferPool defines a concurrent safe free list of byte slices used to provide
42+
// temporary buffers for formatting log messages prior to outputting them.
43+
var bufferPool = sync.Pool{
44+
New: func() any {
45+
b := make([]byte, 0, 1024)
46+
return (*buffer)(&b)
47+
},
48+
}
49+
50+
// newBuffer returns a byte slice from the free list. A new buffer is allocated
51+
// if there are not any available on the free list. The returned byte slice
52+
// should be returned to the fee list by using the recycleBuffer function when
53+
// the caller is done with it.
54+
func newBuffer() *buffer {
55+
return bufferPool.Get().(*buffer)
56+
}
57+
58+
// free puts the provided byte slice, which should have been obtained via the
59+
// newBuffer function, back on the free list.
60+
func (b *buffer) free() {
61+
// To reduce peak allocation, return only smaller buffers to the pool.
62+
const maxBufferSize = 16 << 10
63+
if cap(*b) <= maxBufferSize {
64+
*b = (*b)[:0]
65+
bufferPool.Put(b)
66+
}
67+
}
68+
69+
func (b *buffer) writeByte(p byte) {
70+
*b = append(*b, p)
71+
}
72+
73+
func (b *buffer) writeBytes(p []byte) {
74+
*b = append(*b, p...)
75+
}
76+
77+
func (b *buffer) writeString(s string) {
78+
*b = append(*b, s...)
79+
}

v2/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/btcsuite/btclog/v2
2+
3+
require github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c
4+
5+
go 1.21

v2/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c h1:4HxD1lBUGUddhzgaNgrCPsFWd7cGYNpeFUgd9ZIgyM0=
2+
github.com/btcsuite/btclog v0.0.0-20241003133417-09c4e92e319c/go.mod h1:w7xnGOhwT3lmrS4H3b/D1XAXxvh+tbhUm8xeHN2y3TQ=

0 commit comments

Comments
 (0)