mirror of
https://github.com/ACINQ/eclair.git
synced 2025-03-12 19:01:39 +01:00
(Minor) Fix capturing logs in tests (#3011)
The lack of properly initialized `MDCContext` made `prepareForDeferredProcessing()` crash in `MyCapturingAppender`.
This commit is contained in:
parent
00de49f750
commit
22bc4a7d16
3 changed files with 13 additions and 11 deletions
|
@ -19,7 +19,7 @@ class MyCapturingAppender extends AppenderBase[ILoggingEvent] {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flush buffered logging events to the output appenders
|
* Flush buffered logging events to the output appenders
|
||||||
* Also clears the buffer..
|
* Also clears the buffer.
|
||||||
*/
|
*/
|
||||||
def flush(): Unit = synchronized {
|
def flush(): Unit = synchronized {
|
||||||
val deferredTestLogger = this.getContext.asInstanceOf[LoggerContext].getLogger("MyCapturingAppenderDelegate")
|
val deferredTestLogger = this.getContext.asInstanceOf[LoggerContext].getLogger("MyCapturingAppenderDelegate")
|
||||||
|
|
|
@ -2,7 +2,7 @@ package fr.acinq.eclair.testutils
|
||||||
|
|
||||||
import ch.qos.logback.classic.LoggerContext
|
import ch.qos.logback.classic.LoggerContext
|
||||||
import ch.qos.logback.classic.selector.ContextSelector
|
import ch.qos.logback.classic.selector.ContextSelector
|
||||||
import ch.qos.logback.classic.util.ContextInitializer
|
import ch.qos.logback.classic.util.{ContextInitializer, LogbackMDCAdapter}
|
||||||
|
|
||||||
import java.util
|
import java.util
|
||||||
import scala.jdk.CollectionConverters.SeqHasAsJava
|
import scala.jdk.CollectionConverters.SeqHasAsJava
|
||||||
|
@ -32,10 +32,11 @@ class MyContextSelector extends ContextSelector {
|
||||||
val context = contexts.getOrElse(name, {
|
val context = contexts.getOrElse(name, {
|
||||||
val context = new LoggerContext()
|
val context = new LoggerContext()
|
||||||
context.setName(name)
|
context.setName(name)
|
||||||
|
context.setMDCAdapter(new LogbackMDCAdapter())
|
||||||
new ContextInitializer(context).autoConfig()
|
new ContextInitializer(context).autoConfig()
|
||||||
|
contexts = contexts + (name -> context)
|
||||||
context
|
context
|
||||||
})
|
})
|
||||||
contexts = contexts + (name -> context)
|
|
||||||
context
|
context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ class MySlf4jLogger extends Actor with SLF4JLogging with RequiresMessageQueue[Lo
|
||||||
private val contextName = context.system.settings.config.getString("akka.logging-context")
|
private val contextName = context.system.settings.config.getString("akka.logging-context")
|
||||||
|
|
||||||
val loggerFactory: LoggerContext = MyContextSelector.Singleton.getLoggerContext(contextName)
|
val loggerFactory: LoggerContext = MyContextSelector.Singleton.getLoggerContext(contextName)
|
||||||
|
val mdc = loggerFactory.getMDCAdapter
|
||||||
|
|
||||||
val mdcThreadAttributeName = "sourceThread"
|
val mdcThreadAttributeName = "sourceThread"
|
||||||
val mdcActorSystemAttributeName = "sourceActorSystem"
|
val mdcActorSystemAttributeName = "sourceActorSystem"
|
||||||
|
@ -101,21 +102,21 @@ class MySlf4jLogger extends Actor with SLF4JLogging with RequiresMessageQueue[Lo
|
||||||
case m: LogEventWithMarker if m.marker ne null =>
|
case m: LogEventWithMarker if m.marker ne null =>
|
||||||
val properties = m.marker.properties
|
val properties = m.marker.properties
|
||||||
if (properties.nonEmpty) {
|
if (properties.nonEmpty) {
|
||||||
properties.foreach { case (k, v) => MDC.put(k, String.valueOf(v)) }
|
properties.foreach { case (k, v) => mdc.put(k, String.valueOf(v)) }
|
||||||
}
|
}
|
||||||
case _ =>
|
case _ =>
|
||||||
}
|
}
|
||||||
|
|
||||||
MDC.put(mdcAkkaSourceAttributeName, logSource)
|
mdc.put(mdcAkkaSourceAttributeName, logSource)
|
||||||
MDC.put(mdcThreadAttributeName, logEvent.thread.getName)
|
mdc.put(mdcThreadAttributeName, logEvent.thread.getName)
|
||||||
MDC.put(mdcAkkaTimestamp, formatTimestamp(logEvent.timestamp))
|
mdc.put(mdcAkkaTimestamp, formatTimestamp(logEvent.timestamp))
|
||||||
MDC.put(mdcActorSystemAttributeName, context.system.name)
|
mdc.put(mdcActorSystemAttributeName, context.system.name)
|
||||||
MDC.put(mdcAkkaAddressAttributeName, akkaAddress)
|
mdc.put(mdcAkkaAddressAttributeName, akkaAddress)
|
||||||
logEvent.mdc.foreach { case (k, v) => MDC.put(k, String.valueOf(v)) }
|
logEvent.mdc.foreach { case (k, v) => mdc.put(k, String.valueOf(v)) }
|
||||||
|
|
||||||
try logStatement
|
try logStatement
|
||||||
finally {
|
finally {
|
||||||
MDC.clear()
|
mdc.clear()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue