Skip to content

Commit

Permalink
Merge pull request #413 from astef/astef-optimize_quote
Browse files Browse the repository at this point in the history
Use strconv.AppendQuote instead of strconv.Quote for message formatting
  • Loading branch information
k8s-ci-robot authored Oct 17, 2024
2 parents 5496965 + a1604b5 commit 7f02688
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 7 additions & 3 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,12 +809,16 @@ func (l *loggingT) infoS(logger *logWriter, filter LogFilter, depth int, msg str
// printS is called from infoS and errorS if logger is not specified.
// set log severity by s
func (l *loggingT) printS(err error, s severity.Severity, depth int, msg string, keysAndValues ...interface{}) {
// Only create a new buffer if we don't have one cached.
b := buffer.GetBuffer()
// The message is always quoted, even if it contains line breaks.
// If developers want multi-line output, they should use a small, fixed
// message and put the multi-line output into a value.
b.WriteString(strconv.Quote(msg))
qMsg := make([]byte, 0, 1024)
qMsg = strconv.AppendQuote(qMsg, msg)

// Only create a new buffer if we don't have one cached.
b := buffer.GetBuffer()
b.Write(qMsg)

if err != nil {
serialize.KVListFormat(&b.Buffer, "err", err)
}
Expand Down
6 changes: 5 additions & 1 deletion klogr_slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ func slogOutput(file string, line int, now time.Time, err error, s severity.Seve
}

// See printS.
qMsg := make([]byte, 0, 1024)
qMsg = strconv.AppendQuote(qMsg, msg)

b := buffer.GetBuffer()
b.WriteString(strconv.Quote(msg))
b.Write(qMsg)

if err != nil {
serialize.KVListFormat(&b.Buffer, "err", err)
}
Expand Down
12 changes: 8 additions & 4 deletions textlogger/textlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func runtimeBacktrace(skip int) (string, int) {
}

func (l *tlogger) printWithInfos(file string, line int, now time.Time, err error, s severity.Severity, msg string, kvList []interface{}) {
// The message is always quoted, even if it contains line breaks.
// If developers want multi-line output, they should use a small, fixed
// message and put the multi-line output into a value.
qMsg := make([]byte, 0, 1024)
qMsg = strconv.AppendQuote(qMsg, msg)

// Only create a new buffer if we don't have one cached.
b := buffer.GetBuffer()
defer buffer.PutBuffer(b)
Expand All @@ -124,10 +130,8 @@ func (l *tlogger) printWithInfos(file string, line int, now time.Time, err error
}
b.FormatHeader(s, file, line, now)

// The message is always quoted, even if it contains line breaks.
// If developers want multi-line output, they should use a small, fixed
// message and put the multi-line output into a value.
b.WriteString(strconv.Quote(msg))
b.Write(qMsg)

if err != nil {
serialize.KVFormat(&b.Buffer, "err", err)
}
Expand Down

0 comments on commit 7f02688

Please sign in to comment.