Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class PatternLoggingSuite extends LoggingSuiteBase with BeforeAndAfterAll {
s""".*$level $className: This is a log message\nThis is a new line \t other msg\n"""
}

override def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String = {
s""".*$level $className: The first message\nthe first new line\tthe first other msg\n""" +
s"""[\\s\\S]*The second message\nthe second new line\tthe second other msg\n"""
}

override def expectedPatternForBasicMsgWithException(level: Level): String = {
s""".*$level $className: This is a log message\n[\\s\\S]*"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ trait LoggingSuiteBase
def basicMsgWithEscapeCharMDC: LogEntry =
log"This is a log message\nThis is a new line \t other msg"

// scalastyle:off line.size.limit
def msgWithMDCAndEscapeChar: LogEntry =
log"The first message\nthe first new line\tthe first other msg\n${MDC(LogKeys.PATHS, "C:\\Users\\run-all_1.R\nC:\\Users\\run-all_2.R")}\nThe second message\nthe second new line\tthe second other msg"
// scalastyle:on line.size.limit

def msgWithMDC: LogEntry = log"Lost executor ${MDC(LogKeys.EXECUTOR_ID, "1")}."

def msgWithMDCValueIsNull: LogEntry = log"Lost executor ${MDC(LogKeys.EXECUTOR_ID, null)}."
Expand All @@ -77,6 +82,9 @@ trait LoggingSuiteBase
// test for basic message (with escape char mdc)
def expectedPatternForBasicMsgWithEscapeCharMDC(level: Level): String

// test for message (with mdc and escape char)
def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String

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

Expand Down Expand Up @@ -130,6 +138,19 @@ trait LoggingSuiteBase
}
}

test("Logging with MDC and escape char") {
Seq(
(Level.ERROR, () => logError(msgWithMDCAndEscapeChar)),
(Level.WARN, () => logWarning(msgWithMDCAndEscapeChar)),
(Level.INFO, () => logInfo(msgWithMDCAndEscapeChar)),
(Level.DEBUG, () => logDebug(msgWithMDCAndEscapeChar)),
(Level.TRACE, () => logTrace(msgWithMDCAndEscapeChar))
).foreach { case (level, logFunc) =>
val logOutput = captureLogOutput(logFunc)
assert(expectedPatternForMsgWithMDCAndEscapeChar(level).r.matches(logOutput))
}
}

test("Basic logging with Exception") {
val exception = new RuntimeException("OOM")
Seq(
Expand Down Expand Up @@ -218,6 +239,7 @@ class StructuredLoggingSuite extends LoggingSuiteBase {
jsonMapper.readTree(json).toString.
replace("<timestamp>", """[^"]+""").
replace(""""<stacktrace>"""", """.*""").
replace("<windows_paths>", """.*""").
replace("{", """\{""") + "\n"
}

Expand Down Expand Up @@ -254,6 +276,22 @@ class StructuredLoggingSuite extends LoggingSuiteBase {
}""")
}

override def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String = {
// scalastyle:off line.size.limit
compactAndToRegexPattern(
s"""
{
"ts": "<timestamp>",
"level": "$level",
"msg": "The first message\\\\nthe first new line\\\\tthe first other msg\\\\n<windows_paths>\\\\nThe second message\\\\nthe second new line\\\\tthe second other msg",
"context": {
"paths": "<windows_paths>"
},
"logger": "$className"
}""")
// scalastyle:on line.size.limit
}

override def expectedPatternForBasicMsgWithException(level: Level): String = {
compactAndToRegexPattern(
s"""
Expand Down