Skip to content

Commit

Permalink
fix: Verbose logging introduced by LogCaptor
Browse files Browse the repository at this point in the history
Add TestAppender programmatically for one test rather than enable it for all tests.
  • Loading branch information
joc-a committed Aug 31, 2023
1 parent 1fac540 commit 62d284c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package org.jetbrains.exposed.sql.tests

import org.apache.logging.log4j.core.Filter
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.core.Layout
import org.apache.logging.log4j.core.LogEvent
import org.apache.logging.log4j.core.appender.AbstractAppender
import org.apache.logging.log4j.core.config.Property
import org.apache.logging.log4j.core.config.plugins.Plugin
import org.apache.logging.log4j.core.config.plugins.PluginAttribute
import org.apache.logging.log4j.core.config.plugins.PluginElement
import org.apache.logging.log4j.core.config.plugins.PluginFactory
import java.io.Serializable

/**
* Appends log events of a specified level to a list using a layout.
*/
@Plugin(name = "TestAppender", category = "Core", elementType = "appender", printObject = false)
class TestAppender
private constructor(name: String, filter: Filter?, layout: Layout<out Serializable>) :
AbstractAppender(name, filter, layout, true, Property.EMPTY_ARRAY) {
private constructor(layout: Layout<out Serializable>, val level: Level) : AbstractAppender(
"TestAppender",
null,
layout,
true,
Property.EMPTY_ARRAY
) {
private val log: MutableList<LogEvent> = mutableListOf()

override fun append(event: LogEvent) {
log.add(event.toImmutable())
if (event.level == level) { // add events of the specified level only
log.add(event.toImmutable())
}
}

fun getLog(): List<LogEvent> {
Expand All @@ -29,11 +37,10 @@ private constructor(name: String, filter: Filter?, layout: Layout<out Serializab
@PluginFactory
@JvmStatic
fun createAppender(
@PluginAttribute("name") name: String,
@PluginElement("Layout") layout: Layout<out Serializable>,
@PluginElement("Filter") filter: Filter?
layout: Layout<out Serializable>,
level: Level
): TestAppender {
return TestAppender(name, filter, layout)
return TestAppender(layout, level)
}
}
}
5 changes: 0 additions & 5 deletions exposed-tests/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
<Async name="ASYNC" includeLocation="true">
<AppenderRef ref="CONSOLE"/>
</Async>
<TestAppender name="TestAppender">
<PatternLayout pattern="%d{ABSOLUTE} %5p %t %c{1}:%M:%L - %m%n"/>
</TestAppender>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ASYNC"/>
<AppenderRef ref="TestAppender"/>
</Root>
<Logger name="Exposed" level="info" additivity="false">
<AppenderRef ref="ASYNC"/>
<AppenderRef ref="TestAppender"/>
</Logger>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package org.jetbrains.exposed.sql.tests.shared.dml

import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.core.Appender
import org.apache.logging.log4j.core.Filter
import org.apache.logging.log4j.core.LoggerContext
import org.apache.logging.log4j.core.config.Configuration
import org.apache.logging.log4j.core.layout.PatternLayout
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestAppender
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.expectException
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class JoinTests : DatabaseTestsBase() {
Expand Down Expand Up @@ -196,9 +200,7 @@ class JoinTests : DatabaseTestsBase() {

@Test
fun testNoWarningsOnLeftJoinRegression() {
val loggerContext = LogManager.getContext(false) as LoggerContext
val appenders = loggerContext.configuration.appenders
val testAppender = appenders["TestAppender"] as TestAppender
val testAppender = addAppender(Level.WARN)

val MainTable = object : Table("maintable") {
val id = integer("idCol")
Expand All @@ -217,13 +219,45 @@ class JoinTests : DatabaseTestsBase() {
.single()
.getOrNull(JoinTable.data)

// Assert no logging took place
println("Printing ${testAppender.getLog().size} logevents")
testAppender.getLog().forEachIndexed { index, logEvent ->
println("LogEvent $index: level ${logEvent.level}, message ${logEvent.message}, $logEvent")
// Assert no logging took place whose source is the ResultRow.getInternal method because that is where the
// warning comes from
assertFalse(testAppender.getLog().any { it.source.methodName == "getInternal" })
}

removeAppender(testAppender)
}

private fun addAppender(level: Level): TestAppender {
val context = LoggerContext.getContext(false)
val config = context.configuration
val layout = PatternLayout.createDefaultLayout(config)
val testAppender = TestAppender.createAppender(layout, level)
testAppender.start()
config.addAppender(testAppender)
updateLoggers(testAppender, config, level)
return testAppender
}

private fun updateLoggers(appender: Appender, config: Configuration, level: Level, remove: Boolean = false) {
val filter: Filter? = null
for (loggerConfig in config.loggers.values) {
if (remove) {
loggerConfig.removeAppender(appender.name)
} else {
loggerConfig.addAppender(appender, level, filter)
}
println("End of logevents")
assertTrue(testAppender.getLog().none { it.level == Level.WARN })
}
if (remove) {
config.rootLogger.removeAppender(appender.name)
} else {
config.rootLogger.addAppender(appender, level, filter)
}
}

private fun removeAppender(testAppender: TestAppender) {
val context = LoggerContext.getContext(false)
val config = context.configuration
testAppender.stop()
updateLoggers(testAppender, config, testAppender.level, remove = true)
}
}

0 comments on commit 62d284c

Please sign in to comment.