I'm working on writing a class that works as a wrapper for a Logback Logger. Specifically it is to facilitate Audit logging. It should be an independent library that people should be able to import and use. The problem I've having has to do with the minimal amounts of documentation there is for declaring LogBack programatically. I am basing the general declaration on another stack overflow question (Setting Logback Appender path programmatically).
The current definition I have is below. Currently I am having trouble getting the Marker filter work to. It seems like the marker filter, as I've declared it, doesn't do anything. Ultimately I want to make sure that no other information aside from Audit logging makes it into the Audit Log files. That is why I have it Deny on mismatch. From my testing it appears that it is just ignored.
Any information or direction in regards to programmatically created Logback files would be much appreciated. Also is there anything I should worry about when a user uses my class to do audit logging then makes a Logback.xml to do their own separate logging? Thank you
import ch.qos.logback.classic.Level;
import ch.qos.logback.core.spi.FilterReply;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.filter.EvaluatorFilter;
import ch.qos.logback.classic.boolex.OnMarkerEvaluator;
public class AuditLogger {
private static Logger logbackLogger;
final static Marker AUDIT = MarkerFactory.getMarker("AUDIT");
static {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
RollingFileAppender rfAppender = new RollingFileAppender();
rfAppender.setContext(loggerContext);
rfAppender.setFile("auditLogFile.currentDay.log");
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setContext(loggerContext);
// rolling policies need to know their parent
// it's one of the rare cases, where a sub-component knows about its parent
rollingPolicy.setParent(rfAppender);
rollingPolicy.setFileNamePattern("auditLogFile.%d{yyyy-MM-dd}.log");
rollingPolicy.start();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("[%d{ISO8601}] %5marker - %msg%n");
encoder.start();
rfAppender.setEncoder(encoder);
rfAppender.setRollingPolicy(rollingPolicy);
rfAppender.start();
EvaluatorFilter evalFilter= new EvaluatorFilter();
OnMarkerEvaluator markerEval= new OnMarkerEvaluator();
markerEval.addMarker("AUDIT");
evalFilter.setEvaluator(markerEval);
evalFilter.setOnMatch(FilterReply.ACCEPT);
evalFilter.setOnMismatch(FilterReply.DENY);
rfAppender.addFilter(evalFilter);
logbackLogger = loggerContext.getLogger("AUDIT_LOGGER");
logbackLogger.addAppender(rfAppender);
logbackLogger.setLevel(Level.DEBUG);
}
public AuditLogger(){}
public void log(String msg){
logbackLogger.debug(AUDIT, msg);
}
}