Using Mapped Diagnostic Context (MDC) logging, you create a separate log file for each of your applications.
![]() | Note |
---|---|
MDC logging is supported only by log4j and slf4j. |
The basic idea of MDC logging is that you associate each logging message with a particular context (for example, by associating it with a set of key-value pairs). Later on, when it comes to writing the log stream, you can use the context data to sort or filter the logging messages in various ways.
To enable logging per application, you must define an MDC key for each of your applications. At run time, you can then use the application key to sort logging messages and write them into separate files, one for each application key.
To enable logging per application, perform the following steps:
In each of your applications, edit the Java source code to define a unique application key.
If you are using slf4j, add the following static method call to your application:
// Java org.slf4j.MDC.put("app.name","MyFooApp");
If you are using log4j, add the following static method call to your application:
// Java org.apache.log4j.MDC.put("app.name","MyFooApp");
Edit the
etc/org.ops4j.pax.logging.cfg
configuration file and customize the following settings on the sift appender:# Sift appender ... log4j.appender.sift.key=app.name log4j.appender.sift.appender.file=${karaf.data}/log/$\\{app.name\\}.log ...
In the
etc/org.ops4j.pax.logging.cfg
configuration file, add the sift appender to the root logger, as follows:# Root logger log4j.rootLogger=INFO, out, sift, osgi:VmLogAppender