Skip to content

Commit

Permalink
[SPARK-47576][INFRA] Implement logInfo API in structured logging fram…
Browse files Browse the repository at this point in the history
…ework

### What changes were proposed in this pull request?

Implement logWarning API in structured logging framework. Also, revise the test case names to make it more reasonable for the `PatternLoggingSuite`

### Why are the changes needed?

To enhance Apache Spark's logging system by implementing structured 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

Closes apache#45777 from gengliangwang/logInfo.

Authored-by: Gengliang Wang <[email protected]>
Signed-off-by: Gengliang Wang <[email protected]>
  • Loading branch information
gengliangwang committed Mar 29, 2024
1 parent db14be8 commit 11d76c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ trait Logging {
if (log.isInfoEnabled) log.info(msg)
}

protected def logInfo(entry: LogEntry): Unit = {
if (log.isInfoEnabled) {
log.info(entry.message)
entry.context.map(_.close())
}
}

protected def logInfo(entry: LogEntry, throwable: Throwable): Unit = {
if (log.isInfoEnabled) {
log.info(entry.message, throwable)
entry.context.map(_.close())
}
}

protected def logDebug(msg: => String): Unit = {
if (log.isDebugEnabled) log.debug(msg)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,34 @@ abstract class LoggingSuiteBase extends AnyFunSuite // scalastyle:ignore funsuit

def expectedPatternForMsgWithMDCAndException(level: String): String

test("Structured logging") {
test("Basic logging") {
val msg = "This is a log message"
Seq(
("ERROR", () => logError(msg)),
("WARN", () => logWarning(msg))).foreach { case (level, logFunc) =>
("WARN", () => logWarning(msg)),
("INFO", () => logInfo(msg))).foreach { case (level, logFunc) =>
val logOutput = captureLogOutput(logFunc)
assert(expectedPatternForBasicMsg(level).r.matches(logOutput))
}
}

test("Structured logging with MDC") {
test("Logging with MDC") {
Seq(
("ERROR", () => logError(log"Lost executor ${MDC(EXECUTOR_ID, "1")}.")),
("WARN", () => logWarning(log"Lost executor ${MDC(EXECUTOR_ID, "1")}.")))
.foreach {
("ERROR", () => logError(msgWithMDC)),
("WARN", () => logWarning(msgWithMDC)),
("INFO", () => logInfo(msgWithMDC))).foreach {
case (level, logFunc) =>
val logOutput = captureLogOutput(logFunc)
assert(expectedPatternForMsgWithMDC(level).r.matches(logOutput))
}
}

test("Structured exception logging with MDC") {
test("Logging with MDC and Exception") {
val exception = new RuntimeException("OOM")
Seq(
("ERROR", () => logError(log"Error in executor ${MDC(EXECUTOR_ID, "1")}.", exception)),
("WARN", () => logWarning(log"Error in executor ${MDC(EXECUTOR_ID, "1")}.", exception)))
.foreach {
("ERROR", () => logError(msgWithMDCAndException, exception)),
("WARN", () => logWarning(msgWithMDCAndException, exception)),
("INFO", () => logInfo(msgWithMDCAndException, exception))).foreach {
case (level, logFunc) =>
val logOutput = captureLogOutput(logFunc)
assert(expectedPatternForMsgWithMDCAndException(level).r.findFirstIn(logOutput).isDefined)
Expand Down

0 comments on commit 11d76c9

Please sign in to comment.