Search in sources :

Example 1 with DefaultThreadContextStack

use of org.apache.logging.log4j.spi.DefaultThreadContextStack in project logging-log4j2 by apache.

the class ThreadContext method init.

/**
 * <em>Consider private, used for testing.</em>
 */
static void init() {
    ThreadContextMapFactory.init();
    contextMap = null;
    final PropertiesUtil managerProps = PropertiesUtil.getProperties();
    final boolean disableAll = managerProps.getBooleanProperty(DISABLE_ALL);
    useStack = !(managerProps.getBooleanProperty(DISABLE_STACK) || disableAll);
    final boolean useMap = !(managerProps.getBooleanProperty(DISABLE_MAP) || disableAll);
    contextStack = new DefaultThreadContextStack(useStack);
    if (!useMap) {
        contextMap = new NoOpThreadContextMap();
    } else {
        contextMap = ThreadContextMapFactory.createThreadContextMap();
    }
    if (contextMap instanceof ReadOnlyThreadContextMap) {
        readOnlyContextMap = (ReadOnlyThreadContextMap) contextMap;
    } else {
        readOnlyContextMap = null;
    }
}
Also used : DefaultThreadContextStack(org.apache.logging.log4j.spi.DefaultThreadContextStack) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) NoOpThreadContextMap(org.apache.logging.log4j.spi.NoOpThreadContextMap) ReadOnlyThreadContextMap(org.apache.logging.log4j.spi.ReadOnlyThreadContextMap)

Example 2 with DefaultThreadContextStack

use of org.apache.logging.log4j.spi.DefaultThreadContextStack in project logging-log4j2 by apache.

the class ContextStackJsonAttributeConverter method convertToEntityAttribute.

@Override
public ThreadContext.ContextStack convertToEntityAttribute(final String s) {
    if (Strings.isEmpty(s)) {
        return null;
    }
    List<String> list;
    try {
        list = ContextMapJsonAttributeConverter.OBJECT_MAPPER.readValue(s, new TypeReference<List<String>>() {
        });
    } catch (final IOException e) {
        throw new PersistenceException("Failed to convert JSON string to list for stack.", e);
    }
    final DefaultThreadContextStack result = new DefaultThreadContextStack(true);
    result.addAll(list);
    return result;
}
Also used : DefaultThreadContextStack(org.apache.logging.log4j.spi.DefaultThreadContextStack) PersistenceException(javax.persistence.PersistenceException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Example 3 with DefaultThreadContextStack

use of org.apache.logging.log4j.spi.DefaultThreadContextStack in project logging-log4j2 by apache.

the class ContextStackJsonAttributeConverter method convertToEntityAttribute.

@Override
public ThreadContext.ContextStack convertToEntityAttribute(final String s) {
    if (Strings.isEmpty(s)) {
        return null;
    }
    List<String> list;
    try {
        list = ContextMapJsonAttributeConverter.OBJECT_MAPPER.readValue(s, new TypeReference<List<String>>() {
        });
    } catch (final IOException e) {
        throw new PersistenceException("Failed to convert JSON string to list for stack.", e);
    }
    final DefaultThreadContextStack result = new DefaultThreadContextStack(true);
    result.addAll(list);
    return result;
}
Also used : DefaultThreadContextStack(org.apache.logging.log4j.spi.DefaultThreadContextStack) PersistenceException(javax.persistence.PersistenceException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Example 4 with DefaultThreadContextStack

use of org.apache.logging.log4j.spi.DefaultThreadContextStack in project logging-log4j2 by apache.

the class LogEventFixtures method createLogEvent.

/**
     * @return a log event that uses all the bells and whistles, features, nooks and crannies
     */
static Log4jLogEvent createLogEvent() {
    final Marker cMarker = MarkerManager.getMarker("Marker1");
    final Marker pMarker1 = MarkerManager.getMarker("ParentMarker1");
    final Marker pMarker2 = MarkerManager.getMarker("ParentMarker2");
    final Marker gfMarker = MarkerManager.getMarker("GrandFatherMarker");
    final Marker gmMarker = MarkerManager.getMarker("GrandMotherMarker");
    cMarker.addParents(pMarker1);
    cMarker.addParents(pMarker2);
    pMarker1.addParents(gmMarker);
    pMarker1.addParents(gfMarker);
    final Exception sourceHelper = new Exception();
    sourceHelper.fillInStackTrace();
    final Exception cause = new NullPointerException("testNPEx");
    sourceHelper.fillInStackTrace();
    final StackTraceElement source = sourceHelper.getStackTrace()[0];
    final IOException ioException = new IOException("testIOEx", cause);
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1"));
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2"));
    final ThrowableProxy throwableProxy = new ThrowableProxy(ioException);
    final Map<String, String> contextMap = new HashMap<>();
    contextMap.put("MDC.A", "A_Value");
    contextMap.put("MDC.B", "B_Value");
    final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true);
    contextStack.clear();
    contextStack.push("stack_msg1");
    contextStack.add("stack_msg2");
    final Log4jLogEvent expected = //
    Log4jLogEvent.newBuilder().setLoggerName(//
    "a.B").setMarker(//
    cMarker).setLoggerFqcn(//
    "f.q.c.n").setLevel(//
    Level.DEBUG).setMessage(//
    new SimpleMessage("Msg")).setThrown(//
    ioException).setThrownProxy(//
    throwableProxy).setContextMap(//
    contextMap).setContextStack(//
    contextStack).setThreadName(//
    "MyThreadName").setSource(//
    source).setTimeMillis(1).build();
    // validate event?
    return expected;
}
Also used : HashMap(java.util.HashMap) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) IOException(java.io.IOException) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) IOException(java.io.IOException) DefaultThreadContextStack(org.apache.logging.log4j.spi.DefaultThreadContextStack) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent)

Example 5 with DefaultThreadContextStack

use of org.apache.logging.log4j.spi.DefaultThreadContextStack in project logging-log4j2 by apache.

the class LogEventFixtures method createLogEvent.

/**
 * @return a log event that uses all the bells and whistles, features, nooks and crannies
 */
public static Log4jLogEvent createLogEvent() {
    final Marker cMarker = MarkerManager.getMarker("Marker1");
    final Marker pMarker1 = MarkerManager.getMarker("ParentMarker1");
    final Marker pMarker2 = MarkerManager.getMarker("ParentMarker2");
    final Marker gfMarker = MarkerManager.getMarker("GrandFatherMarker");
    final Marker gmMarker = MarkerManager.getMarker("GrandMotherMarker");
    cMarker.addParents(pMarker1);
    cMarker.addParents(pMarker2);
    pMarker1.addParents(gmMarker);
    pMarker1.addParents(gfMarker);
    final Exception sourceHelper = new Exception();
    sourceHelper.fillInStackTrace();
    final Exception cause = new NullPointerException("testNPEx");
    sourceHelper.fillInStackTrace();
    final StackTraceElement source = sourceHelper.getStackTrace()[0];
    final IOException ioException = new IOException("testIOEx", cause);
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 1"));
    ioException.addSuppressed(new IndexOutOfBoundsException("I am suppressed exception 2"));
    final ThrowableProxy throwableProxy = new ThrowableProxy(ioException);
    final StringMap contextData = ContextDataFactory.createContextData();
    contextData.putValue("MDC.A", "A_Value");
    contextData.putValue("MDC.B", "B_Value");
    final DefaultThreadContextStack contextStack = new DefaultThreadContextStack(true);
    contextStack.clear();
    contextStack.push("stack_msg1");
    contextStack.add("stack_msg2");
    final Log4jLogEvent expected = // 
    Log4jLogEvent.newBuilder().setLoggerName(// 
    "a.B").setMarker(// 
    cMarker).setLoggerFqcn(// 
    "f.q.c.n").setLevel(// 
    Level.DEBUG).setMessage(// 
    new SimpleMessage("Msg")).setThrown(// 
    ioException).setThrownProxy(// 
    throwableProxy).setContextData(// 
    contextData).setContextStack(// 
    contextStack).setThreadName(// 
    "MyThreadName").setSource(// 
    source).setTimeMillis(1).build();
    // validate event?
    return expected;
}
Also used : StringMap(org.apache.logging.log4j.util.StringMap) DefaultThreadContextStack(org.apache.logging.log4j.spi.DefaultThreadContextStack) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Marker(org.apache.logging.log4j.Marker) IOException(java.io.IOException) ThrowableProxy(org.apache.logging.log4j.core.impl.ThrowableProxy) IOException(java.io.IOException)

Aggregations

DefaultThreadContextStack (org.apache.logging.log4j.spi.DefaultThreadContextStack)5 IOException (java.io.IOException)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 PersistenceException (javax.persistence.PersistenceException)2 Marker (org.apache.logging.log4j.Marker)2 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)2 ThrowableProxy (org.apache.logging.log4j.core.impl.ThrowableProxy)2 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)2 HashMap (java.util.HashMap)1 NoOpThreadContextMap (org.apache.logging.log4j.spi.NoOpThreadContextMap)1 ReadOnlyThreadContextMap (org.apache.logging.log4j.spi.ReadOnlyThreadContextMap)1 PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)1 StringMap (org.apache.logging.log4j.util.StringMap)1