Skip to content

Commit

Permalink
not log context when explicitly constructing the MessageWithContext w…
Browse files Browse the repository at this point in the history
…hen conf is disabled:
  • Loading branch information
anchovYu committed Aug 13, 2024
1 parent 70b814b commit d5e37d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ trait Logging {
args.foreach { mdc =>
val value = if (mdc.value != null) mdc.value.toString else null
sb.append(value)
if (Logging.isStructuredLoggingEnabled) {
context.put(mdc.key.name, value)
}
context.put(mdc.key.name, value)

if (processedParts.hasNext) {
sb.append(StringContext.processEscapes(processedParts.next()))
Expand All @@ -165,7 +163,13 @@ trait Logging {
}

protected def withLogContext(context: java.util.HashMap[String, String])(body: => Unit): Unit = {
val threadContext = CloseableThreadContext.putAll(context)
// put into context only when structured logging is enabled
val updatedContext = if (Logging.isStructuredLoggingEnabled) {
context
} else {
new java.util.HashMap[String, String]()
}
val threadContext = CloseableThreadContext.putAll(updatedContext)
try {
body
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ trait LoggingSuiteBase
}

// Return the newly added log contents in the log file after executing the function `f`
private def captureLogOutput(f: () => Unit): String = {
protected def captureLogOutput(f: () => Unit): String = {
val content = if (logFile.exists()) {
Files.readString(logFile.toPath)
} else {
Expand Down Expand Up @@ -438,6 +438,44 @@ class StructuredLoggingSuite extends LoggingSuiteBase {
assert((log"\r" + log"\n" + log"\t" + log"\b").message == "\r\n\t\b")
assert((log"\r${MDC(LogKeys.EXECUTOR_ID, 1)}\n".message == "\r1\n"))
}

test("disabled structured logging won't log context") {
Logging.disableStructuredLogging()
val expectedPatternWithoutContext = compactAndToRegexPattern(
s"""
{
"ts": "<timestamp>",
"level": "INFO",
"msg": "Lost executor 1.",
"logger": "$className"
}""")

Seq(
() => logInfo(log"Lost executor ${MDC(LogKeys.EXECUTOR_ID, "1")}."),
() => logInfo( // blocked when explicitly constructing the MessageWithContext
MessageWithContext(
"Lost executor 1.",
new java.util.HashMap[String, String] { put(LogKeys.EXECUTOR_ID.name, "1") }
)
)
).foreach { f =>
val logOutput = captureLogOutput(f)
assert(expectedPatternWithoutContext.r.matches(logOutput))
}
Logging.enableStructuredLogging()
}

test("setting to MDC gets logged") {
val mdcPattern = s""""${LogKeys.DATA.name}":"some-data""""

org.slf4j.MDC.put(LogKeys.DATA.name, "some-data")
val logOutputWithMDCSet = captureLogOutput(() => logInfo(msgWithMDC))
assert(mdcPattern.r.findFirstIn(logOutputWithMDCSet).isDefined)

org.slf4j.MDC.remove(LogKeys.DATA.name)
val logOutputWithoutMDCSet = captureLogOutput(() => logInfo(msgWithMDC))
assert(mdcPattern.r.findFirstIn(logOutputWithoutMDCSet).isEmpty)
}
}

object CustomLogKeys {
Expand Down

0 comments on commit d5e37d8

Please sign in to comment.