Fix line endings and add PSForever copyright

This commit is contained in:
Chord 2016-05-01 21:46:18 -04:00
parent e299abb03a
commit bc87042c0f
2 changed files with 129 additions and 127 deletions

View file

@ -1,65 +1,66 @@
// Taken from http://code.hootsuite.com/logging-contextual-info-in-an-asynchronous-scala-application/ // Copyright (c) 2016 PSForever.net to present
package akka.actor // Taken from http://code.hootsuite.com/logging-contextual-info-in-an-asynchronous-scala-application/
package akka.actor
import akka.util.Timeout
import org.slf4j.MDC import akka.util.Timeout
import org.slf4j.MDC
import scala.concurrent.Future
import scala.concurrent.Future
trait MDCContextAware extends Actor with ActorLogging {
import MDCContextAware._ trait MDCContextAware extends Actor with ActorLogging {
import MDCContextAware._
// This is why this needs to be in package akka.actor
override protected[akka] def aroundReceive(receive: Actor.Receive, msg: Any): Unit = { // This is why this needs to be in package akka.actor
val orig = MDC.getCopyOfContextMap override protected[akka] def aroundReceive(receive: Actor.Receive, msg: Any): Unit = {
try { val orig = MDC.getCopyOfContextMap
msg match { try {
case MdcMsg(mdc, origMsg) => msg match {
if (mdc != null) case MdcMsg(mdc, origMsg) =>
MDC.setContextMap(mdc) if (mdc != null)
else MDC.setContextMap(mdc)
MDC.clear() else
super.aroundReceive(receive, origMsg) MDC.clear()
case _ => super.aroundReceive(receive, origMsg)
super.aroundReceive(receive, msg) case _ =>
} super.aroundReceive(receive, msg)
} finally { }
if (orig != null) } finally {
MDC.setContextMap(orig) if (orig != null)
else MDC.setContextMap(orig)
MDC.clear() else
} MDC.clear()
} }
} }
}
object MDCContextAware {
private case class MdcMsg(mdc: java.util.Map[String, String], msg: Any) object MDCContextAware {
private case class MdcMsg(mdc: java.util.Map[String, String], msg: Any)
object Implicits {
object Implicits {
/**
* Add two new methods that allow MDC info to be passed to MDCContextAware actors. /**
* * Add two new methods that allow MDC info to be passed to MDCContextAware actors.
* Do NOT use these methods to send to actors that are not MDCContextAware. *
*/ * Do NOT use these methods to send to actors that are not MDCContextAware.
implicit class ContextLocalAwareActorRef(val ref: ActorRef) extends AnyVal { */
implicit class ContextLocalAwareActorRef(val ref: ActorRef) extends AnyVal {
import akka.pattern.ask
import akka.pattern.ask
/**
* Send a message to an actor that is MDCContextAware - it will propagate /**
* the current MDC values. Note: we MUST capture the ActorContext in order for senders * Send a message to an actor that is MDCContextAware - it will propagate
* to be correct! This was a bug from the original author. * the current MDC values. Note: we MUST capture the ActorContext in order for senders
*/ * to be correct! This was a bug from the original author.
def !>(msg: Any)(implicit context: ActorContext) : Unit = */
ref.tell(MdcMsg(MDC.getCopyOfContextMap, msg), context.self) def !>(msg: Any)(implicit context: ActorContext) : Unit =
ref.tell(MdcMsg(MDC.getCopyOfContextMap, msg), context.self)
/**
* "Ask" an actor that is MDCContextAware for something - it will propagate /**
* the current MDC values * "Ask" an actor that is MDCContextAware for something - it will propagate
*/ * the current MDC values
def ?>(msg: Any)(implicit context: ActorContext, timeout: Timeout): Future[Any] = */
ref.ask(MdcMsg(MDC.getCopyOfContextMap, msg), context.self) def ?>(msg: Any)(implicit context: ActorContext, timeout: Timeout): Future[Any] =
} ref.ask(MdcMsg(MDC.getCopyOfContextMap, msg), context.self)
} }
}
} }

View file

@ -1,64 +1,65 @@
// Taken from http://code.hootsuite.com/logging-contextual-info-in-an-asynchronous-scala-application/ // Copyright (c) 2016 PSForever.net to present
// Taken from http://code.hootsuite.com/logging-contextual-info-in-an-asynchronous-scala-application/
import org.slf4j.MDC
import org.slf4j.MDC
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext
trait MDCPropagatingExecutionContext extends ExecutionContext {
// name the self-type "self" so we can refer to it inside the nested class trait MDCPropagatingExecutionContext extends ExecutionContext {
self => // name the self-type "self" so we can refer to it inside the nested class
self =>
override def prepare(): ExecutionContext = new ExecutionContext {
// Save the call-site MDC state override def prepare(): ExecutionContext = new ExecutionContext {
val context = MDC.getCopyOfContextMap // Save the call-site MDC state
val context = MDC.getCopyOfContextMap
def execute(r: Runnable): Unit = self.execute(new Runnable {
def run(): Unit = { def execute(r: Runnable): Unit = self.execute(new Runnable {
// Save the existing execution-site MDC state def run(): Unit = {
val oldContext = MDC.getCopyOfContextMap // Save the existing execution-site MDC state
try { val oldContext = MDC.getCopyOfContextMap
// Set the call-site MDC state into the execution-site MDC try {
if (context != null ) // Set the call-site MDC state into the execution-site MDC
MDC.setContextMap(context) if (context != null )
else MDC.setContextMap(context)
MDC.clear() else
MDC.clear()
r.run()
} finally { r.run()
// Restore the existing execution-site MDC state } finally {
if (oldContext != null) // Restore the existing execution-site MDC state
MDC.setContextMap(oldContext) if (oldContext != null)
else MDC.setContextMap(oldContext)
MDC.clear() else
} MDC.clear()
} }
}) }
})
def reportFailure(t: Throwable): Unit = self.reportFailure(t)
} def reportFailure(t: Throwable): Unit = self.reportFailure(t)
} }
}
object MDCPropagatingExecutionContext {
object Implicits { object MDCPropagatingExecutionContext {
// Convenience wrapper around the Scala global ExecutionContext so you can just do: object Implicits {
// import MDCPropagatingExecutionContext.Implicits.global // Convenience wrapper around the Scala global ExecutionContext so you can just do:
implicit lazy val global = MDCPropagatingExecutionContextWrapper(ExecutionContext.Implicits.global) // import MDCPropagatingExecutionContext.Implicits.global
} implicit lazy val global = MDCPropagatingExecutionContextWrapper(ExecutionContext.Implicits.global)
} }
}
/**
* Wrapper around an existing ExecutionContext that makes it propagate MDC information. /**
*/ * Wrapper around an existing ExecutionContext that makes it propagate MDC information.
class MDCPropagatingExecutionContextWrapper(wrapped: ExecutionContext) */
extends ExecutionContext with MDCPropagatingExecutionContext { class MDCPropagatingExecutionContextWrapper(wrapped: ExecutionContext)
extends ExecutionContext with MDCPropagatingExecutionContext {
override def execute(r: Runnable): Unit = wrapped.execute(r)
override def execute(r: Runnable): Unit = wrapped.execute(r)
override def reportFailure(t: Throwable): Unit = wrapped.reportFailure(t)
} override def reportFailure(t: Throwable): Unit = wrapped.reportFailure(t)
}
object MDCPropagatingExecutionContextWrapper {
def apply(wrapped: ExecutionContext): MDCPropagatingExecutionContextWrapper = { object MDCPropagatingExecutionContextWrapper {
new MDCPropagatingExecutionContextWrapper(wrapped) def apply(wrapped: ExecutionContext): MDCPropagatingExecutionContextWrapper = {
} new MDCPropagatingExecutionContextWrapper(wrapped)
}
} }