-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotoslog.go
47 lines (39 loc) · 1.5 KB
/
protoslog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Package protoslog provides utilities for using protocol buffer messages with
// the log/slog package.
package protoslog
import (
"log/slog"
"google.golang.org/protobuf/proto"
)
// Message returns a [slog.Attr] for a [proto.Message] using the provided
// options. Note that these options are ignored if the [slog.Value] is handled
// by a [Handler].
func Message(key string, msg proto.Message, options ...Option) slog.Attr {
return slog.Any(key, MessageValuer(msg, options...))
}
// MessageValue returns a [slog.Value] for a [proto.Message] using the provided
// options. Note that these options are ignored if the [slog.Value] is handled
// by a [Handler].
func MessageValue(msg proto.Message, options ...Option) slog.Value {
return slog.AnyValue(MessageValuer(msg, options...))
}
// Valuer implements [slog.LogValuer] for a [proto.Message] to defer computing a
// [slog.Value] until it's needed.
type Valuer struct {
// Message is the proto.Message to produce the slog.Value.
Message proto.Message
options options
}
// MessageValuer returns a [Valuer] for a [proto.Message] using the provided
// options. Note that these options are ignored if the [Valuer] is handled by a
// [Handler].
func MessageValuer(msg proto.Message, options ...Option) Valuer {
return Valuer{Message: msg, options: newOptions(options)}
}
// LogValue satisfies the [slog.LogValuer] interface.
func (v Valuer) LogValue() slog.Value {
if v.Message == nil {
return slog.Value{}
}
return v.options.MessageValue(v.Message.ProtoReflect())
}