Skip to content
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

fix: Verbose logging introduced by LogCaptor #1849

Closed
wants to merge 3 commits into from
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
1 change: 0 additions & 1 deletion exposed-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ dependencies {
testCompileOnly("com.impossibl.pgjdbc-ng", "pgjdbc-ng", Versions.postgreNG)
compileOnly("com.h2database", "h2", Versions.h2)
testCompileOnly("org.xerial", "sqlite-jdbc", Versions.sqlLite3)
testImplementation("io.github.hakky54:logcaptor:2.9.0")
}

tasks.withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jetbrains.exposed.sql.tests

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.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(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) {
if (event.level == level) { // add events of the specified level only
log.add(event.toImmutable())
}
}

fun getLog(): List<LogEvent> {
return log
}

companion object {
@PluginFactory
@JvmStatic
fun createAppender(
layout: Layout<out Serializable>,
level: Level
): TestAppender {
return TestAppender(layout, level)
}
}
}
2 changes: 1 addition & 1 deletion exposed-tests/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Configuration status="info" packages="org.jetbrains.exposed.sql.tests">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ABSOLUTE} %5p %t %c{1}:%M:%L - %m%n"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.jetbrains.exposed.sql.tests.shared.dml

import nl.altindag.log.LogCaptor
import org.apache.logging.log4j.Level
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 @@ -193,7 +200,7 @@ class JoinTests : DatabaseTestsBase() {

@Test
fun testNoWarningsOnLeftJoinRegression() {
val logCaptor = LogCaptor.forName(exposedLogger.name)
val testAppender = addAppender(Level.WARN)

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

// Assert no logging took place
assertTrue(logCaptor.warnLogs.isEmpty())
assertTrue(logCaptor.errorLogs.isEmpty())
// 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)
}
}
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)
}
}
Loading