Hi Peter
You could use the following Utility class to forward log message from one Logger to another one. E.e. from the Hibernate Logger to the Runtime Log (ivy.log)
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Priority;
import org.apache.log4j.spi.LoggingEvent;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.log.Logger;
public class LogUtil
{
private static class ForwardAppender extends AppenderSkeleton
{
public static final String NAME = ForwardAppender.class.getSimpleName();
private Logger to;
@SuppressWarnings("deprecation")
public ForwardAppender(Logger to) {
this.to = to;
setName(NAME);
}
@Override
public void close() {
}
@Override
public boolean requiresLayout() {
return false;
}
@Override
protected void append(LoggingEvent event) {
if (event.getThrowableInformation() != null)
{
to.log(event.getLevel(), event.getMessage(), event.getThrowableInformation().getThrowable());
}
else
{
to.log(event.getLevel(), event.getMessage());
}
}
}
public static void forwardLoggerToLogger(Logger from, Logger to)
{
if (from.getAppender(ForwardAppender.NAME)==null)
{
from.addAppender(new ForwardAppender(to));
}
}
public static void main(String[] args)
{
// Example
Logger hibernate = Logger.getLogger("org.hibernate");
Logger runtimeLog = Ivy.log();
forwardLoggerToLogger(hibernate, runtimeLog);
}
}
answered
02.10.2018 at 09:38
Reto Weiss ♦♦
4.9k●20●28●57
accept rate:
74%