-
Notifications
You must be signed in to change notification settings - Fork 28
/
redacting_sink.go
61 lines (52 loc) · 1.24 KB
/
redacting_sink.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
package lager
import (
"encoding/json"
)
type redactingSink struct {
sink Sink
jsonRedacter *JSONRedacter
}
// NewRedactingSink creates a sink that redacts sensitive information from the
// data field. The old behavior of NewRedactingWriterSink (which was removed
// in v2) can be obtained using the following code:
//
// redactingSink, err := NewRedactingSink(
// NewWriterSink(writer, minLogLevel),
// keyPatterns,
// valuePatterns,
// )
//
// if err != nil {
// return nil, err
// }
//
// return NewReconfigurableSink(
// redactingSink,
// minLogLevel,
// ), nil
func NewRedactingSink(sink Sink, keyPatterns []string, valuePatterns []string) (Sink, error) {
jsonRedacter, err := NewJSONRedacter(keyPatterns, valuePatterns)
if err != nil {
return nil, err
}
return &redactingSink{
sink: sink,
jsonRedacter: jsonRedacter,
}, nil
}
func (sink *redactingSink) Log(log LogFormat) {
rawJSON, err := json.Marshal(log.Data)
if err != nil {
log.Data = dataForJSONMarhallingError(err, log.Data)
rawJSON, err = json.Marshal(log.Data)
if err != nil {
panic(err)
}
}
redactedJSON := sink.jsonRedacter.Redact(rawJSON)
err = json.Unmarshal(redactedJSON, &log.Data)
if err != nil {
panic(err)
}
sink.sink.Log(log)
}