-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
106 lines (93 loc) · 3.41 KB
/
doc.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Package slogdedup provides structured logging (slog) deduplication for use with json logging
(or any other format where duplicates are not appreciated).
The main impetus behind this package is because most JSON tools do not like duplicate keys for their member
properties/fields. Some of them will give errors or fail to parse the log line, and some may even crash.
Unfortunately the default behavior of the stdlib slog handlers is to allow duplicate keys.
Additionally, this library includes convenience methods for formatting output to
match what is expected for various log aggregation tools (such as Graylog), as
well as cloud providers (such as Stackdriver / Google Cloud Operations / GCP Log Explorer).
Usage:
// OverwriteHandler
overwriter := slogdedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(overwriter))
// {
// "time": "2024-03-21T09:33:25Z",
// "level": "INFO",
// "msg": "this is the dedup overwrite handler",
// "duplicated": "two"
// }
slog.Info("this is the dedup overwrite handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
slog.String("duplicated", "two"),
)
// IgnoreHandler
ignorer := slogdedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(ignorer))
// {
// "time": "2024-03-21T09:33:25Z",
// "level": "INFO",
// "msg": "this is the dedup ignore handler",
// "duplicated": "zero"
// }
slog.Info("this is the dedup ignore handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
slog.String("duplicated", "two"),
)
// IncrementHandler
incrementer := slogdedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(incrementer))
// {
// "time": "2024-03-21T09:33:25Z",
// "level": "INFO",
// "msg": "this is the dedup incrementer handler",
// "duplicated": "zero",
// "duplicated#01": "one",
// "duplicated#02": "two"
// }
slog.Info("this is the dedup incrementer handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
slog.String("duplicated", "two"),
)
// AppendHandler
appender := slogdedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(appender))
// {
// "time": "2024-03-21T09:33:25Z",
// "level": "INFO",
// "msg": "this is the dedup appender handler",
// "duplicated": [
// "zero",
// "one",
// "two"
// ]
// }
slog.Info("this is the dedup appender handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
slog.String("duplicated", "two"),
)
logger := slog.New(slogdedup.NewOverwriteHandler(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
ReplaceAttr: slogdedup.ReplaceAttrStackdriver(nil), // Needed for builtin's
}),
&slogdedup.OverwriteHandlerOptions{ResolveKey: slogdedup.ResolveKeyStackdriver(nil)}, // Needed for everything else, and deduplication
))
// {
// "time": "2024-03-21T09:59:19.652284-06:00",
// "severity": "WARNING",
// "logging.googleapis.com/sourceLocation": {
// "function": "main.main",
// "file": "/go/src/github.com/veqryn/slog-dedup/cmd/replacers/cmd.go",
// "line": "19"
// },
// "message": "this is the main message",
// "duplicated": "one"
// }
logger.Warn("this is the main message", slog.String("duplicated", "zero"), slog.String("duplicated", "one"))
*/
package slogdedup