/
hook.go
77 lines (66 loc) · 2.11 KB
/
hook.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package logging
import (
"bytes"
"encoding/json"
"log/slog"
"time"
"github.com/tidwall/gjson"
)
// Inspired by Logrus Testing Hook, this is my version for slog.
type Hook struct {
buffer *bytes.Buffer
logger *slog.Logger
}
func NewHookLogger(level slog.Level) (Hook, *slog.Logger) {
return NewHookLoggerWithOptions(&slog.HandlerOptions{Level: level})
}
func NewHookLoggerWithOptions(options *slog.HandlerOptions) (Hook, *slog.Logger) {
var buf bytes.Buffer
handler := slog.NewJSONHandler(&buf, options)
logger := slog.New(handler)
hook := Hook{
buffer: &buf,
logger: logger,
}
return hook, logger
}
func (h Hook) Logs() []Entry {
split := bytes.Split(h.buffer.Bytes(), []byte("\n"))
// the last element is a lot of times just an empty array.
// this is due to how buffer allocates the capacity of the slice.
// here we strip the last element if it is empty
if len(split[len(split)-1]) == 0 {
split = split[:len(split)-1]
}
entries := make([]Entry, len(split))
for i, s := range split {
json.Unmarshal(s, &entries[i])
entries[i].Raw = s
}
return entries
}
// Clear the Hooks buffer, aka delete all Log Entries.
func (h Hook) Reset() {
h.buffer.Reset()
}
// Entry is a convenience struct containing basic information
// TODO replace the fields with methods to save ram
type Entry struct {
Time time.Time `json:"time"`
Level slog.Level `json:"level"`
Message string `json:"msg"`
Error string `json:"error"`
Raw json.RawMessage
}
// Use Get if you want to get a value from a LogEntry that is not contained within the message.
// Use the dot notation for that.
// For more information about dot notation see https://github.com/tidwall/gjson
func (e Entry) Get(path string) gjson.Result {
return gjson.GetBytes(e.Raw, path)
}
// GetMany searches json for the multiple paths. The return value is a Result array where the number of items will be equal to the number of input paths.
// Use the dot notation for that.
// For more information about dot notation see https://github.com/tidwall/gjson
func (e Entry) GetMany(paths ...string) []gjson.Result {
return gjson.GetManyBytes(e.Raw, paths...)
}