Java Code Examples for org.apache.logging.log4j.ThreadContext#getImmutableContext()

The following examples show how to use org.apache.logging.log4j.ThreadContext#getImmutableContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestContextHeaderInterceptor.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body,
                                    ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
    Map<String, String> map = ThreadContext.getImmutableContext();
    HttpHeaders headers = httpRequest.getHeaders();
    for (Map.Entry<String, String> entry : map.entrySet()) {
        RequestContextMapping mapping = mappings.getMapping(entry.getKey());
        if (mapping != null && !mapping.isLocal()) {
            String key = mappings.getHeaderPrefix() + mapping.getFieldName();
            if (!headers.containsKey(key)) {
                headers.add(key, entry.getValue());
            }
        }
    }
    return clientHttpRequestExecution.execute(httpRequest, body);
}
 
Example 2
Source File: CloudBusImpl3.java    From zstack with Apache License 2.0 6 votes vote down vote up
private void evalThreadContextToMessage(Message msg) {
    Map<String, String> ctx = ThreadContext.getImmutableContext();
    if (ctx != null) {
        msg.putHeaderEntry(THREAD_CONTEXT, new HashMap<>(ctx));
    }

    List<String> list = ThreadContext.getImmutableStack().asList();
    if (list != null && !list.isEmpty()) {
        msg.putHeaderEntry(THREAD_CONTEXT_STACK, new ArrayList<>(list));
    }

    Map<Object, Object> tctx = TaskContext.getTaskContext();
    if (tctx != null) {
        msg.putHeaderEntry(TASK_CONTEXT, tctx);
    }
}
 
Example 3
Source File: ThreadContextUtilityClass.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public static void perfTest() throws Exception {
    ThreadContext.clearMap();
    final Timer complete = new Timer("ThreadContextTest");
    complete.start();
    ThreadContext.put("Var1", "value 1");
    ThreadContext.put("Var2", "value 2");
    ThreadContext.put("Var3", "value 3");
    ThreadContext.put("Var4", "value 4");
    ThreadContext.put("Var5", "value 5");
    ThreadContext.put("Var6", "value 6");
    ThreadContext.put("Var7", "value 7");
    ThreadContext.put("Var8", "value 8");
    ThreadContext.put("Var9", "value 9");
    ThreadContext.put("Var10", "value 10");
    final int loopCount = 1000000;
    final Timer timer = new Timer("ThreadContextCopy", loopCount);
    timer.start();
    for (int i = 0; i < loopCount; ++i) {
        final Map<String, String> map = ThreadContext.getImmutableContext();
        assertNotNull(map);
    }
    timer.stop();
    complete.stop();
    System.out.println(timer.toString());
    System.out.println(complete.toString());
}
 
Example 4
Source File: CloudBusImpl2.java    From zstack with Apache License 2.0 5 votes vote down vote up
private void evalThreadContextToMessage(Message msg) {
    Map<String, String> ctx = ThreadContext.getImmutableContext();
    if (ctx != null) {
        msg.putHeaderEntry(CloudBus.HEADER_TASK_CONTEXT, ctx);
    }

    List<String> taskStack = ThreadContext.getImmutableStack().asList();
    if (taskStack != null && !taskStack.isEmpty()) {
        msg.putHeaderEntry(CloudBus.HEADER_TASK_STACK, taskStack);
    }
}
 
Example 5
Source File: ThreadContextBenchmark.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
static Map<String, String> createMap(final List<Property> properties) {
    final Map<String, String> contextMap = ThreadContext.getImmutableContext();
    if (properties == null || properties.isEmpty()) {
        return contextMap; // may be ThreadContext.EMPTY_MAP but not null
    }
    final Map<String, String> map = new HashMap<>(contextMap);

    for (final Property prop : properties) {
        if (!map.containsKey(prop.getName())) {
            map.put(prop.getName(), prop.getValue());
        }
    }
    return Collections.unmodifiableMap(map);
}
 
Example 6
Source File: ThreadContextDataInjector.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public ReadOnlyStringMap rawContextData() {
    final ReadOnlyThreadContextMap map = ThreadContext.getThreadContextMap();
    if (map instanceof ReadOnlyStringMap) {
        return (ReadOnlyStringMap) map;
    }
    // note: default ThreadContextMap is null
    final Map<String, String> copy = ThreadContext.getImmutableContext();
    return copy.isEmpty() ? ContextDataFactory.emptyFrozenContextData() : new JdkMapAdapterStringMap(copy);
}
 
Example 7
Source File: SimpleLogger.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public void logMessage(final String fqcn, final Level mgsLevel, final Marker marker, final Message msg,
        final Throwable throwable) {
    final StringBuilder sb = new StringBuilder();
    // Append date-time if so configured
    if (showDateTime) {
        final Date now = new Date();
        String dateText;
        synchronized (dateFormatter) {
            dateText = dateFormatter.format(now);
        }
        sb.append(dateText);
        sb.append(SPACE);
    }

    sb.append(mgsLevel.toString());
    sb.append(SPACE);
    if (Strings.isNotEmpty(logName)) {
        sb.append(logName);
        sb.append(SPACE);
    }
    sb.append(msg.getFormattedMessage());
    if (showContextMap) {
        final Map<String, String> mdc = ThreadContext.getImmutableContext();
        if (mdc.size() > 0) {
            sb.append(SPACE);
            sb.append(mdc.toString());
            sb.append(SPACE);
        }
    }
    final Object[] params = msg.getParameters();
    Throwable t;
    if (throwable == null && params != null && params.length > 0
            && params[params.length - 1] instanceof Throwable) {
        t = (Throwable) params[params.length - 1];
    } else {
        t = throwable;
    }
    stream.println(sb.toString());
    if (t != null) {
        stream.print(SPACE);
        t.printStackTrace(stream);
    }
}
 
Example 8
Source File: ThreadContextUtilityClass.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public static void testGetImmutableContextReturnsImmutableMapIfNonEmpty() {
    ThreadContext.clearMap();
    ThreadContext.put("key", "val");
    final Map<String, String> immutable = ThreadContext.getImmutableContext();
    immutable.put("otherkey", "otherval");
}
 
Example 9
Source File: ThreadContextUtilityClass.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
public static void testGetImmutableContextReturnsImmutableMapIfEmpty() {
    ThreadContext.clearMap();
    final Map<String, String> immutable = ThreadContext.getImmutableContext();
    immutable.put("otherkey", "otherval");
}
 
Example 10
Source File: ThreadContextDataProvider.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> supplyContextData() {
    return ThreadContext.getImmutableContext();
}