Skip to content

Commit

Permalink
Split logs in files
Browse files Browse the repository at this point in the history
  • Loading branch information
slinkydeveloper committed Nov 15, 2024
1 parent 973ad40 commit ae40a05
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencyResolutionManagement {
library("restate-sdk-api-kotlin-gen", "dev.restate", "sdk-api-kotlin-gen")
.versionRef("restate")

version("log4j", "2.23.1")
version("log4j", "2.24.1")
library("log4j-api", "org.apache.logging.log4j", "log4j-api").versionRef("log4j")
library("log4j-core", "org.apache.logging.log4j", "log4j-core").versionRef("log4j")
library("log4j-slf4j", "org.apache.logging.log4j", "log4j-slf4j-impl").versionRef("log4j")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class InjectLog4jContextListener(val suiteName: String) : TestExecutionListener

override fun executionStarted(testIdentifier: TestIdentifier) {
if (testIdentifier.isContainer && testIdentifier.source.getOrNull() is ClassSource) {
ThreadContext.put(TEST_CLASS, describeTestIdentifier(suiteName, testPlan!!, testIdentifier))
ThreadContext.put(
TEST_CLASS, classSimpleName((testIdentifier.source.getOrNull() as ClassSource).className))
}
}

Expand Down
49 changes: 43 additions & 6 deletions src/main/kotlin/dev/restate/sdktesting/junit/TestSuite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.github.ajalt.mordant.terminal.Terminal
import dev.restate.sdktesting.infra.BaseRestateDeployerExtension
import dev.restate.sdktesting.infra.getGlobalConfig
import dev.restate.sdktesting.infra.registerGlobalConfig
import dev.restate.sdktesting.junit.InjectLog4jContextListener.Companion.TEST_CLASS
import java.io.PrintWriter
import java.nio.file.Path
import org.apache.logging.log4j.Level
Expand Down Expand Up @@ -130,25 +131,59 @@ class TestSuite(
"pattern",
"%-4r %-5p [%X{test_class}][%t]%notEmpty{[%X{containerHostname}]} %c{1.2.*} - %m%n")

val fileAppender =
val testRunnerFileAppender =
builder
.newAppender("log", "File")
.newAppender("testRunnerLog", "File")
.addAttribute("fileName", reportDir.resolve("testrunner.log").toString())
.add(layout)
val nullAppender = builder.newAppender("nullAppender", "Null")

val routing =
builder
.newAppender("routingAppender", "Routing")
// If you wanna try to figure out what's going on here:
// https://stackoverflow.com/questions/25114526/log4j2-how-to-write-logs-to-separate-files-for-each-user
.addComponent(
builder
.newComponent("Routes")
.addAttribute("pattern", "\${ctx:${TEST_CLASS}}")
.addComponent(
// Route for XML magicians
builder
.newComponent("Route")
.addComponent(
builder
.newAppender("testRunnerLog-\${ctx:${TEST_CLASS}}", "File")
.addAttribute(
"fileName",
"${reportDir}/\${ctx:${TEST_CLASS}}/testRunner.log")
.add(layout)))
.addComponent(
// Default route to noop (still for XML magicians)
builder
.newComponent("Route")
.addAttribute("key", "\${ctx:${TEST_CLASS}}")
.addAttribute("ref", "nullAppender")))

val restateLogger =
builder
.newLogger("dev.restate", Level.DEBUG)
.add(builder.newAppenderRef("log"))
.add(builder.newAppenderRef("testRunnerLog"))
.add(builder.newAppenderRef("routingAppender"))
.addAttribute("additivity", false)

val testContainersLogger =
builder
.newLogger("org.testcontainers", Level.TRACE)
.add(builder.newAppenderRef("log"))
.add(builder.newAppenderRef("testRunnerLog"))
.add(builder.newAppenderRef("routingAppender"))
.addAttribute("additivity", false)

val rootLogger = builder.newRootLogger(Level.WARN).add(builder.newAppenderRef("log"))
val rootLogger =
builder
.newRootLogger(Level.WARN)
.add(builder.newAppenderRef("testRunnerLog"))
.add(builder.newAppenderRef("routingAppender"))

if (printToStdout) {
val consoleAppender = builder.newAppender("stdout", "Console").add(layout)
Expand All @@ -158,7 +193,9 @@ class TestSuite(
restateLogger.add(builder.newAppenderRef("stdout"))
}

builder.add(fileAppender)
builder.add(testRunnerFileAppender)
builder.add(nullAppender)
builder.add(routing)
builder.add(restateLogger)
builder.add(testContainersLogger)
builder.add(rootLogger)
Expand Down

0 comments on commit ae40a05

Please sign in to comment.