-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-47659][CORE][TESTS] Improve *LoggingSuite*
#45784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -19,23 +19,28 @@ package org.apache.spark.util | |||
| import java.io.File | ||||
| import java.nio.file.Files | ||||
|
|
||||
| import com.fasterxml.jackson.databind.ObjectMapper | ||||
| import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||||
| import org.apache.logging.log4j.Level | ||||
| import org.scalatest.funsuite.AnyFunSuite // scalastyle:ignore funsuite | ||||
|
|
||||
| import org.apache.spark.internal.{LogEntry, Logging, MDC} | ||||
| import org.apache.spark.internal.LogKey.{EXECUTOR_ID, MAX_SIZE, MIN_SIZE} | ||||
|
|
||||
| abstract class LoggingSuiteBase extends AnyFunSuite // scalastyle:ignore funsuite | ||||
| with Logging { | ||||
| trait LoggingSuiteBase | ||||
| extends AnyFunSuite // scalastyle:ignore funsuite | ||||
| with Logging { | ||||
|
|
||||
| protected def logFilePath: String | ||||
| def className: String | ||||
| def logFilePath: String | ||||
|
|
||||
| protected lazy val logFile: File = { | ||||
| private lazy val logFile: File = { | ||||
| val pwd = new File(".").getCanonicalPath | ||||
| new File(pwd + "/" + logFilePath) | ||||
| } | ||||
|
|
||||
| // Returns the first line in the log file that contains the given substring. | ||||
| protected def captureLogOutput(f: () => Unit): String = { | ||||
| // Return the newly added log contents in the log file after executing the function `f` | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Obviously, the using of the function is: |
||||
| private def captureLogOutput(f: () => Unit): String = { | ||||
| val content = if (logFile.exists()) { | ||||
| Files.readString(logFile.toPath) | ||||
| } else { | ||||
|
|
@@ -52,35 +57,33 @@ abstract class LoggingSuiteBase extends AnyFunSuite // scalastyle:ignore funsuit | |||
|
|
||||
| def msgWithMDCAndException: LogEntry = log"Error in executor ${MDC(EXECUTOR_ID, "1")}." | ||||
|
|
||||
| def expectedPatternForBasicMsg(level: Level): String | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using 'Level' instead of 'String' may be more reasonable |
||||
|
|
||||
| def msgWithConcat: LogEntry = log"Min Size: ${MDC(MIN_SIZE, "2")}, " + | ||||
| log"Max Size: ${MDC(MAX_SIZE, "4")}. " + | ||||
| log"Please double check." | ||||
|
|
||||
| def expectedPatternForMsgWithMDC(level: Level): String | ||||
|
|
||||
| def expectedPatternForBasicMsg(level: String): String | ||||
|
|
||||
| def expectedPatternForMsgWithMDC(level: String): String | ||||
|
|
||||
| def expectedPatternForMsgWithMDCAndException(level: String): String | ||||
| def expectedPatternForMsgWithMDCAndException(level: Level): String | ||||
|
|
||||
| def verifyMsgWithConcat(level: String, logOutput: String): Unit | ||||
| def verifyMsgWithConcat(level: Level, logOutput: String): Unit | ||||
|
|
||||
| test("Basic logging") { | ||||
| val msg = "This is a log message" | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
spark/common/utils/src/test/scala/org/apache/spark/util/StructuredLoggingSuite.scala Line 49 in 11d76c9
|
||||
| Seq( | ||||
| ("ERROR", () => logError(msg)), | ||||
| ("WARN", () => logWarning(msg)), | ||||
| ("INFO", () => logInfo(msg))).foreach { case (level, logFunc) => | ||||
| (Level.ERROR, () => logError(basicMsg)), | ||||
| (Level.WARN, () => logWarning(basicMsg)), | ||||
| (Level.INFO, () => logInfo(basicMsg))).foreach { case (level, logFunc) => | ||||
| val logOutput = captureLogOutput(logFunc) | ||||
| assert(expectedPatternForBasicMsg(level).r.matches(logOutput)) | ||||
| } | ||||
| } | ||||
|
|
||||
| test("Logging with MDC") { | ||||
| Seq( | ||||
| ("ERROR", () => logError(msgWithMDC)), | ||||
| ("WARN", () => logWarning(msgWithMDC)), | ||||
| ("INFO", () => logInfo(msgWithMDC))).foreach { | ||||
| (Level.ERROR, () => logError(msgWithMDC)), | ||||
| (Level.WARN, () => logWarning(msgWithMDC)), | ||||
| (Level.INFO, () => logInfo(msgWithMDC))).foreach { | ||||
| case (level, logFunc) => | ||||
| val logOutput = captureLogOutput(logFunc) | ||||
| assert(expectedPatternForMsgWithMDC(level).r.matches(logOutput)) | ||||
|
|
@@ -90,9 +93,9 @@ abstract class LoggingSuiteBase extends AnyFunSuite // scalastyle:ignore funsuit | |||
| test("Logging with MDC and Exception") { | ||||
| val exception = new RuntimeException("OOM") | ||||
| Seq( | ||||
| ("ERROR", () => logError(msgWithMDCAndException, exception)), | ||||
| ("WARN", () => logWarning(msgWithMDCAndException, exception)), | ||||
| ("INFO", () => logInfo(msgWithMDCAndException, exception))).foreach { | ||||
| (Level.ERROR, () => logError(msgWithMDCAndException, exception)), | ||||
| (Level.WARN, () => logWarning(msgWithMDCAndException, exception)), | ||||
| (Level.INFO, () => logInfo(msgWithMDCAndException, exception))).foreach { | ||||
| case (level, logFunc) => | ||||
| val logOutput = captureLogOutput(logFunc) | ||||
| assert(expectedPatternForMsgWithMDCAndException(level).r.findFirstIn(logOutput).isDefined) | ||||
|
|
@@ -101,9 +104,9 @@ abstract class LoggingSuiteBase extends AnyFunSuite // scalastyle:ignore funsuit | |||
|
|
||||
| test("Logging with concat") { | ||||
| Seq( | ||||
| ("ERROR", () => logError(msgWithConcat)), | ||||
| ("WARN", () => logWarning(msgWithConcat)), | ||||
| ("INFO", () => logInfo(msgWithConcat))).foreach { | ||||
| (Level.ERROR, () => logError(msgWithConcat)), | ||||
| (Level.WARN, () => logWarning(msgWithConcat)), | ||||
| (Level.INFO, () => logInfo(msgWithConcat))).foreach { | ||||
| case (level, logFunc) => | ||||
| val logOutput = captureLogOutput(logFunc) | ||||
| verifyMsgWithConcat(level, logOutput) | ||||
|
|
@@ -112,32 +115,87 @@ abstract class LoggingSuiteBase extends AnyFunSuite // scalastyle:ignore funsuit | |||
| } | ||||
|
|
||||
| class StructuredLoggingSuite extends LoggingSuiteBase { | ||||
| private val className = this.getClass.getName.stripSuffix("$") | ||||
| override def className: String = classOf[StructuredLoggingSuite].getName | ||||
| override def logFilePath: String = "target/structured.log" | ||||
|
|
||||
| override def expectedPatternForBasicMsg(level: String): String = | ||||
| s"""\\{"ts":"[^"]+","level":"$level","msg":"This is a log message","logger":"$className"}\n""" | ||||
|
|
||||
| override def expectedPatternForMsgWithMDC(level: String): String = | ||||
| // scalastyle:off line.size.limit | ||||
| s"""\\{"ts":"[^"]+","level":"$level","msg":"Lost executor 1.","context":\\{"executor_id":"1"},"logger":"$className"}\n""" | ||||
| // scalastyle:on | ||||
| private val jsonMapper = new ObjectMapper().registerModule(DefaultScalaModule) | ||||
| private def compactAndToRegexPattern(json: String): String = { | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In testing, this method |
||||
| jsonMapper.readTree(json).toString. | ||||
| replace("<timestamp>", """[^"]+"""). | ||||
| replace(""""<stacktrace>"""", """.*"""). | ||||
| replace("{", """\{""") + "\n" | ||||
| } | ||||
|
|
||||
| override def expectedPatternForMsgWithMDCAndException(level: String): String = | ||||
| // scalastyle:off line.size.limit | ||||
| s"""\\{"ts":"[^"]+","level":"$level","msg":"Error in executor 1.","context":\\{"executor_id":"1"},"exception":\\{"class":"java.lang.RuntimeException","msg":"OOM","stacktrace":.*},"logger":"$className"}\n""" | ||||
| // scalastyle:on | ||||
| override def expectedPatternForBasicMsg(level: Level): String = { | ||||
| compactAndToRegexPattern( | ||||
| s""" | ||||
| { | ||||
| "ts": "<timestamp>", | ||||
| "level": "$level", | ||||
| "msg": "This is a log message", | ||||
| "logger": "$className" | ||||
| }""") | ||||
| } | ||||
|
|
||||
| override def verifyMsgWithConcat(level: String, logOutput: String): Unit = { | ||||
| // scalastyle:off line.size.limit | ||||
| val pattern1 = | ||||
| s"""\\{"ts":"[^"]+","level":"$level","msg":"Min Size: 2, Max Size: 4. Please double check.","context":\\{"min_size":"2","max_size": "4"},"logger":"$className"}\n""" | ||||
| override def expectedPatternForMsgWithMDC(level: Level): String = { | ||||
| compactAndToRegexPattern( | ||||
| s""" | ||||
| { | ||||
| "ts": "<timestamp>", | ||||
| "level": "$level", | ||||
| "msg": "Lost executor 1.", | ||||
| "context": { | ||||
| "executor_id": "1" | ||||
| }, | ||||
| "logger": "$className" | ||||
| }""") | ||||
| } | ||||
|
|
||||
| val pattern2 = | ||||
| s"""\\{"ts":"[^"]+","level":"$level","msg":"Min Size: 2, Max Size: 4. Please double check.","context":\\{"max_size":"4","min_size":"2"},"logger":"$className"}\n""" | ||||
| override def expectedPatternForMsgWithMDCAndException(level: Level): String = { | ||||
| compactAndToRegexPattern( | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is |
||||
| s""" | ||||
| { | ||||
| "ts": "<timestamp>", | ||||
| "level": "$level", | ||||
| "msg": "Error in executor 1.", | ||||
| "context": { | ||||
| "executor_id": "1" | ||||
| }, | ||||
| "exception": { | ||||
| "class": "java.lang.RuntimeException", | ||||
| "msg": "OOM", | ||||
| "stacktrace": "<stacktrace>" | ||||
| }, | ||||
| "logger": "$className" | ||||
| }""") | ||||
| } | ||||
|
|
||||
| override def verifyMsgWithConcat(level: Level, logOutput: String): Unit = { | ||||
| val pattern1 = compactAndToRegexPattern( | ||||
| s""" | ||||
| { | ||||
| "ts": "<timestamp>", | ||||
| "level": "$level", | ||||
| "msg": "Min Size: 2, Max Size: 4. Please double check.", | ||||
| "context": { | ||||
| "min_size": "2", | ||||
| "max_size": "4" | ||||
| }, | ||||
| "logger": "$className" | ||||
| }""") | ||||
|
|
||||
| val pattern2 = compactAndToRegexPattern( | ||||
| s""" | ||||
| { | ||||
| "ts": "<timestamp>", | ||||
| "level": "$level", | ||||
| "msg": "Min Size: 2, Max Size: 4. Please double check.", | ||||
| "context": { | ||||
| "max_size": "4", | ||||
| "min_size": "2" | ||||
| }, | ||||
| "logger": "$className" | ||||
| }""") | ||||
| assert(pattern1.r.matches(logOutput) || pattern2.r.matches(logOutput)) | ||||
| // scalastyle:on | ||||
| } | ||||
|
|
||||
| } | ||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make full use of the variable
classNameto eliminate hard coding textPatternLoggingSuite