+act #3671 Implementing support for MDC values defined by the application. Fixes #3671.

* Added LogEvent subclasses with new field for transporting the MDC custom values.
* Slf4jLogger now takes MDC values from new LogEvent field, puts all in MDC before appending the log, and removes all after.
* New trait DiagnosticLoggingAdapter was introduced, which extends LoggingAdapter and adds MDC support with methods to get, set and clear MDC values.
* New factory method added to Logging for getting loggers with MDC support.
* BusLogging was changed to create new LogEvents including the MDC values.
* Actors can mixin with DiagnosticActorLogging which defines a diagnostic logger "log", has a hook to override for defining MDC values per message, and overrides aroundReceive for setting and clearing MDC around receive execution.
* Proper documentation was added for Scala and Java under the Logging/Slf4j section.
This commit is contained in:
Gaston M. Tonietti 2013-10-18 01:10:10 -03:00
parent 05f402c236
commit 9e9d5541b4
12 changed files with 645 additions and 18 deletions

View file

@ -22,6 +22,13 @@ import org.junit.Test;
import akka.testkit.JavaTestKit;
import scala.Option;
//#imports-mdc
import akka.event.Logging;
import akka.event.DiagnosticLoggingAdapter;
import java.util.HashMap;
import java.util.Map;
//#imports-mdc
//#imports-deadletter
import akka.actor.Props;
import akka.actor.ActorRef;
@ -40,6 +47,14 @@ public class LoggingDocTest {
JavaTestKit.shutdownActorSystem(system);
}
@Test
public void useLoggingActorWithMDC() {
ActorSystem system = ActorSystem.create("MyDiagnosticSystem");
ActorRef mdcActor = system.actorOf(Props.create(MdcActor.class, this));
mdcActor.tell("some request", ActorRef.noSender());
JavaTestKit.shutdownActorSystem(system);
}
@Test
public void subscribeToDeadLetters() {
//#deadletters
@ -86,6 +101,27 @@ public class LoggingDocTest {
//#my-actor
//#mdc-actor
class MdcActor extends UntypedActor {
final DiagnosticLoggingAdapter log = Logging.getLogger(this);
public void onReceive(Object message) {
Map<String, Object> mdc;
mdc = new HashMap<String, Object>();
mdc.put("requestId", 1234);
mdc.put("visitorId", 5678);
log.setMDC(mdc);
log.info("Starting new request");
log.clearMDC();
}
}
//#mdc-actor
//#my-event-listener
class MyEventListener extends UntypedActor {
public void onReceive(Object message) {