1
0
mirror of https://github.com/ACINQ/eclair.git synced 2025-01-19 05:33:59 +01:00

Use logback's MDC to display channelId in logs (#142)

This commit is contained in:
Pierre-Marie Padiou 2017-08-25 16:30:00 +02:00 committed by GitHub
parent 4cd081c11c
commit edab8b004a
4 changed files with 33 additions and 4 deletions

View File

@ -0,0 +1,23 @@
package fr.acinq.eclair
import akka.actor.{Actor, FSM}
import akka.event.{DiagnosticLoggingAdapter, LoggingAdapter}
/**
* A version of [[akka.actor.DiagnosticActorLogging]] compatible with an FSM
* See https://groups.google.com/forum/#!topic/akka-user/0CxR8CImr4Q
*/
trait FSMDiagnosticActorLogging[S, D] extends FSM[S, D] {
import akka.event.Logging._
val diagLog: DiagnosticLoggingAdapter = akka.event.Logging(this)
def mdc(currentMessage: Any): MDC = emptyMDC
override def log: LoggingAdapter = diagLog
override def aroundReceive(receive: Actor.Receive, msg: Any): Unit = try {
diagLog.mdc(mdc(msg))
super.aroundReceive(receive, msg)
} finally {
diagLog.clearMDC()
}
}

View File

@ -1,6 +1,7 @@
package fr.acinq.eclair.channel
import akka.actor.{ActorRef, FSM, LoggingFSM, OneForOneStrategy, Props, Status, SupervisorStrategy}
import akka.actor.{ActorRef, DiagnosticActorLogging, FSM, LoggingFSM, OneForOneStrategy, Props, Status, SupervisorStrategy}
import akka.event.Logging.MDC
import fr.acinq.bitcoin.Crypto.{PrivateKey, PublicKey}
import fr.acinq.bitcoin._
import fr.acinq.eclair._
@ -25,7 +26,7 @@ object Channel {
def props(nodeParams: NodeParams, remoteNodeId: PublicKey, blockchain: ActorRef, router: ActorRef, relayer: ActorRef) = Props(new Channel(nodeParams, remoteNodeId, blockchain, router, relayer))
}
class Channel(val nodeParams: NodeParams, remoteNodeId: PublicKey, blockchain: ActorRef, router: ActorRef, relayer: ActorRef)(implicit ec: ExecutionContext = ExecutionContext.Implicits.global) extends LoggingFSM[State, Data] {
class Channel(val nodeParams: NodeParams, remoteNodeId: PublicKey, blockchain: ActorRef, router: ActorRef, relayer: ActorRef)(implicit ec: ExecutionContext = ExecutionContext.Implicits.global) extends LoggingFSM[State, Data] with FSMDiagnosticActorLogging[State, Data] {
val forwarder = context.actorOf(Props(new Forwarder(nodeParams)), "forwarder")
@ -1426,6 +1427,11 @@ class Channel(val nodeParams: NodeParams, remoteNodeId: PublicKey, blockchain: A
// }
}
override def mdc(currentMessage: Any): MDC = {
val id = Helpers.getChannelId(stateData)
Map("channelId" -> id)
}
// we let the peer decide what to do
override val supervisorStrategy = OneForOneStrategy(loggingEnabled = true) { case _ => SupervisorStrategy.Escalate }

View File

@ -31,7 +31,7 @@
<target>System.out</target>
<withJansi>false</withJansi>
<encoder>
<pattern>%yellow(${HOSTNAME} %d) %highlight(%-5level) %logger{36} %X{akkaSource} - %blue(%msg) %ex{12}%n
<pattern>%yellow(${HOSTNAME} %d) %highlight(%-5level) %logger{36} channelId=%X{channelId} - %blue(%msg) %ex{12}%n
</pattern>
</encoder>
</appender>

View File

@ -15,7 +15,7 @@ object LogSetup {
def logTo(datadir: File) = {
val lc = LoggerFactory.getILoggerFactory().asInstanceOf[LoggerContext]
val ple = new PatternLayoutEncoder()
ple.setPattern("%d %-5level %logger{36} %X{akkaSource} - %msg%ex{24}%n")
ple.setPattern("%d %-5level %logger{36} %X{akkaSource} %X{channelId} - %msg%ex{24}%n")
ple.setContext(lc)
ple.start()
val fileAppender = new FileAppender[ILoggingEvent]()