Search in sources :

Example 1 with LogEventAdapter

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;
}
Also used : Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LocationInfo(org.apache.log4j.spi.LocationInfo) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEventAdapter(org.apache.log4j.bridge.LogEventAdapter) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with LogEventAdapter

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;
    }
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) LogEvent(org.apache.logging.log4j.core.LogEvent) MapMessage(org.apache.logging.log4j.message.MapMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) LocationInfo(org.apache.log4j.spi.LocationInfo) LogEventAdapter(org.apache.log4j.bridge.LogEventAdapter) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) SortedArrayStringMap(org.apache.logging.log4j.util.SortedArrayStringMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 LogEventAdapter (org.apache.log4j.bridge.LogEventAdapter)2 LocationInfo (org.apache.log4j.spi.LocationInfo)2 LogEvent (org.apache.logging.log4j.core.LogEvent)2 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 SortedArrayStringMap (org.apache.logging.log4j.util.SortedArrayStringMap)2 MapMessage (org.apache.logging.log4j.message.MapMessage)1 Message (org.apache.logging.log4j.message.Message)1