Skip to content

Conversation

@gengliangwang
Copy link
Member

@gengliangwang gengliangwang commented Jun 27, 2024

What changes were proposed in this pull request?

In the current implementation:

class LogEntry(messageWithContext: => MessageWithContext) {

  def message: String = messageWithContext.message

  def context: java.util.HashMap[String, String] = messageWithContext.context
}

def logInfo(entry: LogEntry): Unit = {
  if (log.isInfoEnabled) {
    withLogContext(entry.context) {
      log.info(entry.message)
    }
  }
}

The field messageWithContext is constructed twice, one from entry.context and another one from entry.message.

This PR is to improve this and ensure a log entry is constructed only once.

Why are the changes needed?

Improve the performance of logging

Does this PR introduce any user-facing change?

No

How was this patch tested?

New UT

Was this patch authored or co-authored using generative AI tooling?

No

@gengliangwang
Copy link
Member Author

cc @panbingkun

}
}

test("LogEntry should construct MessageWithContext only once") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should put the above testing logic into MDCSuite or a new UT (eg: MessageWithContextSuite), as it seems unrelated to the classification of structured and pattern logs, so it will be executed twice in GA?

Copy link
Member Author

Choose a reason for hiding this comment

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

A test takes 200ms, running twice is ok. Also, we need to ensure the log entry is constructed only once when structured logging is disabled, too.

@panbingkun
Copy link
Contributor

This is really a great improvement!

@gengliangwang
Copy link
Member Author

Merging to master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants