diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/FSMDiagnosticActorLogging.scala b/eclair-core/src/main/scala/fr/acinq/eclair/FSMDiagnosticActorLogging.scala new file mode 100644 index 000000000..76e21550d --- /dev/null +++ b/eclair-core/src/main/scala/fr/acinq/eclair/FSMDiagnosticActorLogging.scala @@ -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() + } +} diff --git a/eclair-core/src/main/scala/fr/acinq/eclair/channel/Channel.scala b/eclair-core/src/main/scala/fr/acinq/eclair/channel/Channel.scala index c3e4c5b2e..ede867dde 100644 --- a/eclair-core/src/main/scala/fr/acinq/eclair/channel/Channel.scala +++ b/eclair-core/src/main/scala/fr/acinq/eclair/channel/Channel.scala @@ -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 } diff --git a/eclair-node/src/main/resources/logback_colors.xml b/eclair-node/src/main/resources/logback_colors.xml index 6d8be1019..de29a3a28 100644 --- a/eclair-node/src/main/resources/logback_colors.xml +++ b/eclair-node/src/main/resources/logback_colors.xml @@ -31,7 +31,7 @@ System.out false - %yellow(${HOSTNAME} %d) %highlight(%-5level) %logger{36} %X{akkaSource} - %blue(%msg) %ex{12}%n + %yellow(${HOSTNAME} %d) %highlight(%-5level) %logger{36} channelId=%X{channelId} - %blue(%msg) %ex{12}%n diff --git a/eclair-node/src/main/scala/fr/acinq/eclair/LogSetup.scala b/eclair-node/src/main/scala/fr/acinq/eclair/LogSetup.scala index 084e878b2..8a8ed6dab 100644 --- a/eclair-node/src/main/scala/fr/acinq/eclair/LogSetup.scala +++ b/eclair-node/src/main/scala/fr/acinq/eclair/LogSetup.scala @@ -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]()