Luché por un tiempo para encontrar la funcionalidad de SiftingAppender en log4j (no pudimos cambiar a logback debido a algunas dependencias), y terminé con una solución programática que funciona bastante bien, usando un MDC y agregando loggers en tiempo de ejecución:
// this can be any thread-specific string
String processID = request.getProcessID();
Logger logger = Logger.getRootLogger();
// append a new file logger if no logger exists for this tag
if(logger.getAppender(processID) == null){
try{
String pattern = "%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n";
String logfile = "log/"+processID+".log";
FileAppender fileAppender = new FileAppender(
new PatternLayout(pattern), logfile, true);
fileAppender.setName(processID);
// add a filter so we can ignore any logs from other threads
fileAppender.addFilter(new ProcessIDFilter(processID));
logger.addAppender(fileAppender);
}catch(Exception e){
throw new RuntimeException(e);
}
}
// tag all child threads with this process-id so we can separate out log output
MDC.put("process-id", processID);
//whatever you want to do in the thread
LOG.info("This message will only end up in "+processID+".log!");
MDC.remove("process-id");
El filtro anexa anteriormente simplemente comprueba para un ID de proceso específico:
public class RunIdFilter extends Filter {
private final String runId;
public RunIdFilter(String runId) {
this.runId = runId;
}
@Override
public int decide(LoggingEvent event) {
Object mdc = event.getMDC("run-id");
if (runId.equals(mdc)) {
return Filter.ACCEPT;
}
return Filter.DENY;
}
}
Hope esto ayuda un poco.
Pero el uso de Logback significaría que todas las instrucciones de registro deben cambiarse ¿verdad? ' –
Consulte el enlace de API heredadas en http://slf4j.org/legacy.html – Ceki