github.com/MetalBlockchain/metalgo@v1.11.9/utils/logging/sanitize.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package logging 5 6 import ( 7 "strings" 8 9 "go.uber.org/zap" 10 ) 11 12 type sanitizedString string 13 14 func (s sanitizedString) String() string { 15 return strings.ReplaceAll(string(s), "\n", `\n`) 16 } 17 18 // UserString constructs a field with the given key and the value stripped of 19 // newlines. The value is sanitized lazily. 20 func UserString(key, val string) zap.Field { 21 return zap.Stringer(key, sanitizedString(val)) 22 } 23 24 type sanitizedStrings []string 25 26 func (s sanitizedStrings) String() string { 27 var strs strings.Builder 28 for i, str := range s { 29 if i != 0 { 30 _, _ = strs.WriteString(", ") 31 } 32 _, _ = strs.WriteString(strings.ReplaceAll(str, "\n", `\n`)) 33 } 34 return strs.String() 35 } 36 37 // UserStrings constructs a field with the given key and the values stripped of 38 // newlines. The values are sanitized lazily. 39 func UserStrings(key string, val []string) zap.Field { 40 return zap.Stringer(key, sanitizedStrings(val)) 41 }