Skip to content

Commit 0f5f170

Browse files
authored
Merge pull request #1517 from thaJeztah/update_quoting_docs
TextFormatter: touch-up GoDoc for quoting
2 parents 63302e6 + a41edc6 commit 0f5f170

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

text_formatter.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ import (
1515

1616
var baseTimestamp = time.Now()
1717

18-
// TextFormatter formats logs into text
18+
// TextFormatter formats logs into text.
19+
//
20+
// Output is logfmt-like: key=value pairs separated by spaces. Field keys are
21+
// written as-is (unquoted and unescaped) in the plain (non-colored) format;
22+
// only field values may be quoted depending on DisableQuote, ForceQuote,
23+
// QuoteEmptyFields, and the value content.
24+
//
25+
// When colors are enabled, ANSI escape sequences may be added for presentation.
26+
// For fully escaped structured output (including safe keys), use JSONFormatter.
1927
type TextFormatter struct {
2028
// Set to true to bypass checking for a TTY before outputting colors.
2129
ForceColors bool
@@ -71,12 +79,17 @@ type TextFormatter struct {
7179
terminal bool
7280

7381
// FieldMap allows users to customize the names of keys for default fields.
82+
// Mapped keys are written as-is, so they should be safe for plain-text output.
83+
//
7484
// As an example:
85+
//
7586
// formatter := &TextFormatter{
76-
// FieldMap: FieldMap{
77-
// FieldKeyTime: "@timestamp",
78-
// FieldKeyLevel: "@level",
79-
// FieldKeyMsg: "@message"}}
87+
// FieldMap: FieldMap{
88+
// FieldKeyTime: "@timestamp",
89+
// FieldKeyLevel: "@level",
90+
// FieldKeyMsg: "@message",
91+
// },
92+
// }
8093
FieldMap FieldMap
8194

8295
// CallerPrettyfier can be set by the user to modify the content
@@ -287,6 +300,8 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
287300
b.WriteByte('\n')
288301
}
289302

303+
// appendKeyValue writes key=value. Keys are written verbatim (unquoted/unescaped);
304+
// values are subject to quoting/escaping.
290305
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value any) {
291306
if b.Len() > 0 {
292307
b.WriteByte(' ')

0 commit comments

Comments
 (0)