use of org.apache.log4j.bridge.LogEventAdapter in project logging-log4j2 by apache.
the class PropertyRewritePolicy method rewrite.
/**
* {@inheritDoc}
*/
public LoggingEvent rewrite(final LoggingEvent source) {
if (!properties.isEmpty()) {
Map<String, String> rewriteProps = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
for (Map.Entry<String, String> entry : properties.entrySet()) {
if (!rewriteProps.containsKey(entry.getKey())) {
rewriteProps.put(entry.getKey(), entry.getValue());
}
}
LogEvent event;
if (source instanceof LogEventAdapter) {
event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setContextData(new SortedArrayStringMap(rewriteProps)).build();
} else {
LocationInfo info = source.getLocationInformation();
StackTraceElement element = new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(), Integer.parseInt(info.getLineNumber()));
Thread thread = getThread(source.getThreadName());
long threadId = thread != null ? thread.getId() : 0;
int threadPriority = thread != null ? thread.getPriority() : 0;
event = Log4jLogEvent.newBuilder().setContextData(new SortedArrayStringMap(rewriteProps)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(new SimpleMessage(source.getRenderedMessage())).setSource(element).setLoggerName(source.getLoggerName()).setThreadName(source.getThreadName()).setThreadId(threadId).setThreadPriority(threadPriority).setThrown(source.getThrowableInformation().getThrowable()).setTimeMillis(source.getTimeStamp()).setNanoTime(0).setThrownProxy(null).build();
}
return new LogEventAdapter(event);
}
return source;
}
use of org.apache.log4j.bridge.LogEventAdapter in project logging-log4j2 by apache.
the class MapRewritePolicy method rewrite.
/**
* {@inheritDoc}
*/
public LoggingEvent rewrite(final LoggingEvent source) {
Object msg = source.getMessage();
if (msg instanceof MapMessage || msg instanceof Map) {
Map<String, String> props = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
@SuppressWarnings("unchecked") Map<String, Object> eventProps = msg instanceof Map ? (Map) msg : ((MapMessage) msg).getData();
//
// if the map sent in the logging request
// has "message" entry, use that as the message body
// otherwise, use the entire map.
//
Message newMessage = null;
Object newMsg = eventProps.get("message");
if (newMsg != null) {
newMessage = new SimpleMessage(newMsg.toString());
for (Map.Entry<String, Object> entry : eventProps.entrySet()) {
if (!("message".equals(entry.getKey()))) {
props.put(entry.getKey(), entry.getValue().toString());
}
}
} else {
return source;
}
LogEvent event;
if (source instanceof LogEventAdapter) {
event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setMessage(newMessage).setContextData(new SortedArrayStringMap(props)).build();
} else {
LocationInfo info = source.getLocationInformation();
StackTraceElement element = new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(), Integer.parseInt(info.getLineNumber()));
Thread thread = getThread(source.getThreadName());
long threadId = thread != null ? thread.getId() : 0;
int threadPriority = thread != null ? thread.getPriority() : 0;
event = Log4jLogEvent.newBuilder().setContextData(new SortedArrayStringMap(props)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(newMessage).setSource(element).setLoggerName(source.getLoggerName()).setThreadName(source.getThreadName()).setThreadId(threadId).setThreadPriority(threadPriority).setThrown(source.getThrowableInformation().getThrowable()).setTimeMillis(source.getTimeStamp()).setNanoTime(0).setThrownProxy(null).build();
}
return new LogEventAdapter(event);
} else {
return source;
}
}
Aggregations