当前位置: 首页>>代码示例>>Java>>正文


Java ThreadContext.ContextStack方法代码示例

本文整理汇总了Java中org.apache.logging.log4j.ThreadContext.ContextStack方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadContext.ContextStack方法的具体用法?Java ThreadContext.ContextStack怎么用?Java ThreadContext.ContextStack使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.logging.log4j.ThreadContext的用法示例。


在下文中一共展示了ThreadContext.ContextStack方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: resolve

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public JsonNode resolve(TemplateResolverContext context, LogEvent logEvent, String key) {
    ThreadContext.ContextStack contextStack = logEvent.getContextStack();
    if (contextStack.getDepth() == 0) {
        return null;
    }
    Pattern itemPattern = context.getNdcPattern();
    ArrayNode contextStackNode = context.getObjectMapper().createArrayNode();
    for (String contextStackItem : contextStack.asList()) {
        boolean matches = itemPattern == null || itemPattern.matcher(contextStackItem).matches();
        if (matches) {
            contextStackNode.add(contextStackItem);
        }
    }
    return contextStackNode;
}
 
开发者ID:vy,项目名称:log4j2-logstash-layout,代码行数:17,代码来源:ContextStackResolver.java

示例2: testConvert02

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void testConvert02() {
    final ThreadContext.ContextStack stack = new MutableThreadContextStack(
            Arrays.asList("key1", "value2", "my3"));

    final String converted = this.converter.convertToDatabaseColumn(stack);

    assertNotNull("The converted value should not be null.", converted);

    final ThreadContext.ContextStack reversed = this.converter
            .convertToEntityAttribute(converted);

    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", stack.asList(),
            reversed.asList());
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:17,代码来源:ContextStackJsonAttributeConverterTest.java

示例3: Log4jLogEvent

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
/**
 * Constructor.
 * @param loggerName The name of the Logger.
 * @param marker The Marker or null.
 * @param fqcn The fully qualified class name of the caller.
 * @param level The logging Level.
 * @param message The Message.
 * @param t A ThrowableProxy or null.
 * @param mdc The mapped diagnostic context.
 * @param ndc the nested diagnostic context.
 * @param threadName The name of the thread.
 * @param location The locations of the caller.
 * @param timestamp The timestamp of the event.
 */
private Log4jLogEvent(final String loggerName, final Marker marker, final String fqcn, final Level level,
                     final Message message, final ThrowableProxy t,
                     final Map<String, String> mdc, final ThreadContext.ContextStack ndc, final String threadName,
                     final StackTraceElement location, final long timestamp) {
    name = loggerName;
    this.marker = marker;
    this.fqcnOfLogger = fqcn;
    this.level = level;
    this.message = message;
    this.throwable = t;
    this.mdc = mdc;
    this.ndc = ndc;
    this.timestamp = message instanceof TimestampMessage ? ((TimestampMessage) message).getTimestamp() : timestamp;
    this.threadName = threadName;
    this.location = location;
    if (message != null && message instanceof LoggerNameAwareMessage) {
        ((LoggerNameAwareMessage) message).setLoggerName(name);
    }
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:34,代码来源:Log4jLogEvent.java

示例4: createLogEvent

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
private static LogEvent createLogEvent() {
    final Marker marker = null;
    final String fqcn = "com.mycom.myproject.mypackage.MyClass";
    final org.apache.logging.log4j.Level level = org.apache.logging.log4j.Level.DEBUG;
    final Message message = new SimpleMessage(MESSAGE);
    final Throwable t = null;
    final StringMap mdc = null;
    final ThreadContext.ContextStack ndc = null;
    final String threadName = null;
    final StackTraceElement location = null;
    final long timestamp = 12345678;

    return Log4jLogEvent.newBuilder() //
            .setLoggerName("name(ignored)") //
            .setMarker(marker) //
            .setLoggerFqcn(fqcn) //
            .setLevel(level) //
            .setMessage(message) //
            .setThrown(t) //
            .setContextData(mdc) //
            .setContextStack(ndc) //
            .setThreadName(threadName) //
            .setSource(location) //
            .setTimeMillis(timestamp) //
            .build();
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:27,代码来源:GelfLayoutBenchmark.java

示例5: testConvert01

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void testConvert01() {
    final ThreadContext.ContextStack stack = new MutableThreadContextStack(
            Arrays.asList("value1", "another2"));

    final String converted = this.converter.convertToDatabaseColumn(stack);

    assertNotNull("The converted value should not be null.", converted);

    final ThreadContext.ContextStack reversed = this.converter
            .convertToEntityAttribute(converted);

    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", stack.asList(),
            reversed.asList());
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:17,代码来源:ContextStackJsonAttributeConverterTest.java

示例6: Log4jLogEvent

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
private Log4jLogEvent(final String loggerName, final Marker marker, final String loggerFQCN, final Level level,
                      final Message message, final Throwable thrown, final ThrowableProxy thrownProxy,
                      final StringMap contextData, final ThreadContext.ContextStack contextStack, final long threadId,
                      final String threadName, final int threadPriority, final StackTraceElement source,
                      final long nanoTime) {
    this.loggerName = loggerName;
    this.marker = marker;
    this.loggerFqcn = loggerFQCN;
    this.level = level == null ? Level.OFF : level; // LOG4J2-462, LOG4J2-465
    this.message = message;
    this.thrown = thrown;
    this.thrownProxy = thrownProxy;
    this.contextData = contextData == null ? ContextDataFactory.createContextData() : contextData;
    this.contextStack = contextStack == null ? ThreadContext.EMPTY_STACK : contextStack;
    this.threadId = threadId;
    this.threadName = threadName;
    this.threadPriority = threadPriority;
    this.source = source;
    if (message instanceof LoggerNameAwareMessage) {
        ((LoggerNameAwareMessage) message).setLoggerName(loggerName);
    }
    this.nanoTime = nanoTime;
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:24,代码来源:Log4jLogEvent.java

示例7: testConvert02

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void testConvert02() {
    ThreadContext.clearStack();
    final ThreadContext.ContextStack stack = new MutableThreadContextStack(
            Arrays.asList("key1", "value2", "my3"));

    final String converted = this.converter.convertToDatabaseColumn(stack);

    assertNotNull("The converted value should not be null.", converted);

    final ThreadContext.ContextStack reversed = this.converter
            .convertToEntityAttribute(converted);

    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", stack.asList(),
            reversed.asList());
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:18,代码来源:ContextStackJsonAttributeConverterTest.java

示例8: createLogEvent

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
private static LogEvent createLogEvent() {
    final Marker marker = null;
    final String fqcn = "com.mycom.myproject.mypackage.MyClass";
    final Level level = Level.DEBUG;
    final Message message = new SimpleMessage(MESSAGE);
    final Throwable t = null;
    final StringMap mdc = null;
    final ThreadContext.ContextStack ndc = null;
    final String threadName = "THREAD";
    final StackTraceElement location = null;
    final long timestamp = System.currentTimeMillis();

    return Log4jLogEvent.newBuilder() //
            .setLoggerName("name(ignored)") //
            .setMarker(marker) //
            .setLoggerFqcn(fqcn) //
            .setLevel(level) //
            .setMessage(message) //
            .setThrown(t) //
            .setContextData(mdc) //
            .setContextStack(ndc) //
            .setThreadName(threadName) //
            .setSource(location) //
            .setTimeMillis(timestamp) //
            .build();
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:27,代码来源:Log4j2AppenderComparisonBenchmark.java

示例9: convertToDatabaseColumn

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public String convertToDatabaseColumn(final ThreadContext.ContextStack contextStack) {
    if (contextStack == null) {
        return null;
    }

    try {
        return ContextMapJsonAttributeConverter.OBJECT_MAPPER.writeValueAsString(contextStack.asList());
    } catch (final IOException e) {
        throw new PersistenceException("Failed to convert stack list to JSON string.", e);
    }
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:13,代码来源:ContextStackJsonAttributeConverter.java

示例10: convertToDatabaseColumn

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public String convertToDatabaseColumn(final ThreadContext.ContextStack contextStack) {
    if (contextStack == null) {
        return null;
    }

    final StringBuilder builder = new StringBuilder();
    for (final String value : contextStack.asList()) {
        if (builder.length() > 0) {
            builder.append('\n');
        }
        builder.append(value);
    }
    return builder.toString();
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:16,代码来源:ContextStackAttributeConverter.java

示例11: testConvertToDatabaseColumn02

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Test
public void testConvertToDatabaseColumn02() {
    final ThreadContext.ContextStack stack = new MutableThreadContextStack(
            Arrays.asList("key1", "value2", "my3"));

    assertEquals("The converted value is not correct.",
            "key1\nvalue2\nmy3",
            this.converter.convertToDatabaseColumn(stack));
}
 
开发者ID:OuZhencong,项目名称:log4j2,代码行数:10,代码来源:ContextStackAttributeConverterTest.java

示例12: processLogEvent

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
private Message processLogEvent(LogEvent logEvent) {
    final String formattedMessage = logEvent.getMessage().getFormattedMessage();
    final DateTime timestamp = new DateTime(logEvent.getTimeMillis(), DateTimeZone.UTC);
    final Message message = new Message(formattedMessage, hostname, timestamp);

    final Level level = logEvent.getLevel();
    message.addField(Message.FIELD_LEVEL, Severity.getSeverity(level).getCode());
    message.addField("log4j_level", level.name());
    message.addField("log4j_level_int", level.intLevel());

    message.addField("node_id", nodeId);
    if (clusterId != null) {
        message.addField("cluster_id", clusterId);
    }
    message.addField("logger_name", logEvent.getLoggerName());
    message.addField("thread_id", logEvent.getThreadId());
    message.addField("thread_name", logEvent.getThreadName());
    message.addField("thread_priority", logEvent.getThreadPriority());
    message.addField("timestamp_nanos", logEvent.getNanoTime());

    final Marker marker = logEvent.getMarker();
    if (marker != null) {
        message.addField("marker", marker.getName());
    }

    if (includeThreadContext) {
        logEvent.getContextData().forEach((k, v ) -> message.addField("context_" + k, v));

        // Guard against https://issues.apache.org/jira/browse/LOG4J2-1530
        final ThreadContext.ContextStack contextStack = logEvent.getContextStack();
        if (contextStack != null) {
            final List<String> contextStackItems = contextStack.asList();
            if (contextStackItems != null && !contextStackItems.isEmpty()) {
                message.addField("context_stack", contextStackItems);
            }
        }
    }

    if (includeSource) {
        final StackTraceElement source = logEvent.getSource();
        if (source != null) {
            message.addField("source_file_name", source.getFileName());
            message.addField("source_method_name", source.getMethodName());
            message.addField("source_class_name", source.getClassName());
            message.addField("source_line_number", source.getLineNumber());
        }
    }

    final ThrowableProxy throwableProxy = logEvent.getThrownProxy();
    if (includeStackTrace && throwableProxy != null) {
        final String stackTrace;
        if (includeExceptionCause) {
            stackTrace = throwableProxy.getExtendedStackTraceAsString("");
        } else {
            stackTrace = throwableProxy.getCauseStackTraceAsString("");
        }

        message.addField("exception_class", throwableProxy.getName());
        message.addField("exception_message", throwableProxy.getMessage());
        message.addField("exception_stack_trace", stackTrace);
    }

    return message;
}
 
开发者ID:graylog-labs,项目名称:graylog-plugin-internal-logs,代码行数:65,代码来源:SerializedLogEventCodec.java

示例13: append

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
public void append(LogEvent event) {
    final Layout<? extends Serializable> layout = getLayout();
    final String formattedMessage;
    if (layout == null) {
        formattedMessage = event.getMessage().getFormattedMessage();
    } else {
        formattedMessage = new String(layout.toByteArray(event), StandardCharsets.UTF_8);
    }

    final GelfMessageBuilder builder = new GelfMessageBuilder(formattedMessage, hostName)
            .timestamp(event.getTimeMillis() / 1000d)
            .level(GelfMessageLevel.fromNumericLevel(Severity.getSeverity(event.getLevel()).getCode()))
            .additionalField("loggerName", event.getLoggerName())
            .additionalField("threadName", event.getThreadName());

    final Marker marker = event.getMarker();
    if (marker != null) {
        builder.additionalField("marker", marker.getName());
    }

    if (includeThreadContext) {
        for (Map.Entry<String, String> entry : event.getContextMap().entrySet()) {
            builder.additionalField(entry.getKey(), entry.getValue());
        }

        // Guard against https://issues.apache.org/jira/browse/LOG4J2-1530
        final ThreadContext.ContextStack contextStack = event.getContextStack();
        if (contextStack != null) {
            final List<String> contextStackItems = contextStack.asList();
            if (contextStackItems != null && !contextStackItems.isEmpty()) {
                builder.additionalField("contextStack", contextStackItems.toString());
            }
        }
    }

    if (includeSource) {
        final StackTraceElement source = event.getSource();
        if (source != null) {
            builder.additionalField("sourceFileName", source.getFileName());
            builder.additionalField("sourceMethodName", source.getMethodName());
            builder.additionalField("sourceClassName", source.getClassName());
            builder.additionalField("sourceLineNumber", source.getLineNumber());
        }
    }

    @SuppressWarnings("all")
    final Throwable thrown = event.getThrown();
    if (includeStackTrace && thrown != null) {
        String stackTrace;
        if (includeExceptionCause) {
            final StringWriter stringWriter = new StringWriter();
            final PrintWriter printWriter = new PrintWriter(stringWriter);
            thrown.printStackTrace(printWriter);
            stackTrace = stringWriter.toString();
        } else {
            stackTrace = getSimpleStacktraceAsString(thrown);
        }

        builder.additionalField("exceptionClass", thrown.getClass().getCanonicalName());
        builder.additionalField("exceptionMessage", thrown.getMessage());
        builder.additionalField("exceptionStackTrace", stackTrace);

        builder.fullMessage(formattedMessage);
    }

    if (!additionalFields.isEmpty()) {
        builder.additionalFields(additionalFields);
    }

    final GelfMessage gelfMessage = builder.build();
    try {
        final boolean sent = client.trySend(gelfMessage);
        if (!sent) {
            LOG.debug("Couldn't send message: {}", gelfMessage);
        }
    } catch (Exception e) {
        throw new AppenderLoggingException("failed to write log event to GELF server: " + e.getMessage(), e);
    }
}
 
开发者ID:graylog-labs,项目名称:log4j2-gelf,代码行数:81,代码来源:GelfAppender.java

示例14: setFields

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
private void setFields(final LogEvent event, final NoSqlObject<W> entity) {
    entity.set("level", event.getLevel());
    entity.set("loggerName", event.getLoggerName());
    entity.set("message", event.getMessage() == null ? null : event.getMessage().getFormattedMessage());

    final StackTraceElement source = event.getSource();
    if (source == null) {
        entity.set("source", (Object) null);
    } else {
        entity.set("source", this.convertStackTraceElement(source));
    }

    final Marker marker = event.getMarker();
    if (marker == null) {
        entity.set("marker", (Object) null);
    } else {
        entity.set("marker", buildMarkerEntity(marker));
    }

    entity.set("threadId", event.getThreadId());
    entity.set("threadName", event.getThreadName());
    entity.set("threadPriority", event.getThreadPriority());
    entity.set("millis", event.getTimeMillis());
    entity.set("date", new java.util.Date(event.getTimeMillis()));

    @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
    Throwable thrown = event.getThrown();
    if (thrown == null) {
        entity.set("thrown", (Object) null);
    } else {
        final NoSqlObject<W> originalExceptionEntity = this.connection.createObject();
        NoSqlObject<W> exceptionEntity = originalExceptionEntity;
        exceptionEntity.set("type", thrown.getClass().getName());
        exceptionEntity.set("message", thrown.getMessage());
        exceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
        while (thrown.getCause() != null) {
            thrown = thrown.getCause();
            final NoSqlObject<W> causingExceptionEntity = this.connection.createObject();
            causingExceptionEntity.set("type", thrown.getClass().getName());
            causingExceptionEntity.set("message", thrown.getMessage());
            causingExceptionEntity.set("stackTrace", this.convertStackTrace(thrown.getStackTrace()));
            exceptionEntity.set("cause", causingExceptionEntity);
            exceptionEntity = causingExceptionEntity;
        }

        entity.set("thrown", originalExceptionEntity);
    }

    final ReadOnlyStringMap contextMap = event.getContextData();
    if (contextMap == null) {
        entity.set("contextMap", (Object) null);
    } else {
        final NoSqlObject<W> contextMapEntity = this.connection.createObject();
        contextMap.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(final String key, final String val) {
                contextMapEntity.set(key, val);
            }
        });
        entity.set("contextMap", contextMapEntity);
    }

    final ThreadContext.ContextStack contextStack = event.getContextStack();
    if (contextStack == null) {
        entity.set("contextStack", (Object) null);
    } else {
        entity.set("contextStack", contextStack.asList().toArray());
    }
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:70,代码来源:NoSqlDatabaseManager.java

示例15: getContextStack

import org.apache.logging.log4j.ThreadContext; //导入方法依赖的package包/类
@Override
@Transient
public ThreadContext.ContextStack getContextStack() {
    return this.getWrappedEvent().getContextStack();
}
 
开发者ID:apache,项目名称:logging-log4j2,代码行数:6,代码来源:TestBaseEntity.java


注:本文中的org.apache.logging.log4j.ThreadContext.ContextStack方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。