Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix
  • Loading branch information
panbingkun committed Apr 3, 2024
commit 338fbcd76ea25a5d7cdf7e889d100f5c229961ce
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,15 @@ trait Logging {
}

implicit class LogStringContext(val sc: StringContext) {
def log(args: Any*): MessageWithContext = {
def log(args: MDC*): MessageWithContext = {
val processedParts = sc.parts.iterator
val sb = new StringBuilder(processedParts.next())
val context = new java.util.HashMap[String, String]()

args.foreach { arg =>
arg match {
case MDC(k, v) =>
sb.append(v)
if (Logging.isStructuredLoggingEnabled) {
context.put(k.toString.toLowerCase(Locale.ROOT), v)
}
case v: Any =>
sb.append(v.toString)
args.foreach { mdc =>
sb.append(mdc.value.toString)
if (Logging.isStructuredLoggingEnabled) {
context.put(mdc.key.toString.toLowerCase(Locale.ROOT), mdc.value.toString)
}
if (processedParts.hasNext) {
sb.append(processedParts.next())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ class PatternLoggingSuite extends LoggingSuiteBase with BeforeAndAfterAll {
override def expectedPatternForMsgWithMDCAndException(level: Level): String =
s""".*$level $className: Error in executor 1.\njava.lang.RuntimeException: OOM\n[\\s\\S]*"""

override def expectedPatternForVariableAndMDC(level: Level): String = {
s""".*$level $className: Hello This is a log message, Lost executor 1.\n"""
}

override def expectedPatternForConcatStringAndMDC(level: Level): String = {
s""".*$level $className: Hello This is a log message, Lost executor 1.\n"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,33 @@ trait LoggingSuiteBase
log"Max Size: ${MDC(MAX_SIZE, "4")}. " +
log"Please double check."

def logVariableAndMDC: LogEntry =
log"Hello $basicMsg, Lost executor ${MDC(EXECUTOR_ID, "1")}."
def concatMDCAndString: LogEntry = log"Lost executor ${MDC(EXECUTOR_ID, "1")}." ++
s" Hello $basicMsg."

def concatMDCAndString: LogEntry =
log"Lost executor ${MDC(EXECUTOR_ID, "1")}." ++ s" Hello $basicMsg."
def concatStringAndMDC: LogEntry = s"Hello $basicMsg, " ++
log"Lost executor ${MDC(EXECUTOR_ID, "1")}."

def concatStringAndMDC: LogEntry =
s"Hello $basicMsg, " ++ log"Lost executor ${MDC(EXECUTOR_ID, "1")}."
// test for basic message (without any mdc)
def expectedPatternForBasicMsg(level: Level): String

// test for basic message and exception
def expectedPatternForBasicMsgWithException(level: Level): String

// test for message (with mdc)
def expectedPatternForMsgWithMDC(level: Level): String

// test for message and exception
def expectedPatternForMsgWithMDCAndException(level: Level): String

def verifyMsgWithConcat(level: Level, logOutput: String): Unit

def expectedPatternForVariableAndMDC(level: Level): String

// test for concat string and MDC
def expectedPatternForConcatStringAndMDC(level: Level): String

// test for concat MDC and string
def expectedPatternForConcatMDCAndString(level: Level): String

// test for concat MDC and MDC
def verifyMsgWithConcat(level: Level, logOutput: String): Unit

test("Basic logging") {
Seq(
(Level.ERROR, () => logError(basicMsg)),
Expand Down Expand Up @@ -140,17 +145,6 @@ trait LoggingSuiteBase
}
}

test("Logging variable and MDC") {
Seq(
(Level.ERROR, () => logError(logVariableAndMDC)),
(Level.WARN, () => logWarning(logVariableAndMDC)),
(Level.INFO, () => logInfo(logVariableAndMDC))).foreach {
case (level, logFunc) =>
val logOutput = captureLogOutput(logFunc)
assert(expectedPatternForVariableAndMDC(level).r.matches(logOutput))
}
}

test("Logging concat string and MDC") {
Seq(
(Level.ERROR, () => logError(concatStringAndMDC)),
Expand Down Expand Up @@ -246,20 +240,6 @@ class StructuredLoggingSuite extends LoggingSuiteBase {
}""")
}

override def expectedPatternForVariableAndMDC(level: Level): String = {
compactAndToRegexPattern(
s"""
{
"ts": "<timestamp>",
"level": "$level",
"msg": "Hello This is a log message, Lost executor 1.",
"context": {
"executor_id": "1"
},
"logger": "$className"
}""")
}

override def expectedPatternForConcatStringAndMDC(level: Level): String = {
compactAndToRegexPattern(
s"""
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.