Skip to content

Evaluate message formatting only once while formatting the log #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion v2/slogger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type Log struct {
MessageFmt string
Args []interface{}
Context *Context

// Appendages to evaluate a message only once
message string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the lifecycle of a Log struct? We've now ~doubled the effective size of it, assuming MessageFmt (and now message) is long compared to the other fields...

evalMsgOnce sync.Once
}

func SimpleLog(prefix string, level Level, errorCode ErrorCode, callerSkip int, messageFmt string, args ...interface{}) *Log {
Expand Down Expand Up @@ -101,7 +105,10 @@ func getTruncatedMessage(old string) string {
}

func (self *Log) Message() string {
return getTruncatedMessage(fmt.Sprintf(self.MessageFmt, self.Args...))
self.evalMsgOnce.Do(func() {
self.message = getTruncatedMessage(fmt.Sprintf(self.MessageFmt, self.Args...))
})
return self.message
}

type Logger struct {
Expand Down