forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sanitize.go
41 lines (32 loc) · 1016 Bytes
/
sanitize.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package logging
import (
"strings"
"go.uber.org/zap"
)
type sanitizedString string
func (s sanitizedString) String() string {
return strings.ReplaceAll(string(s), "\n", "\\n")
}
// UserString constructs a field with the given key and the value stripped of
// newlines. The value is sanitized lazily.
func UserString(key, val string) zap.Field {
return zap.Stringer(key, sanitizedString(val))
}
type sanitizedStrings []string
func (s sanitizedStrings) String() string {
var strs strings.Builder
for i, str := range s {
if i != 0 {
_, _ = strs.WriteString(", ")
}
_, _ = strs.WriteString(strings.ReplaceAll(str, "\n", "\\n"))
}
return strs.String()
}
// UserStrings constructs a field with the given key and the values stripped of
// newlines. The values are sanitized lazily.
func UserStrings(key string, val []string) zap.Field {
return zap.Stringer(key, sanitizedStrings(val))
}