本文整理汇总了Java中org.apache.logging.log4j.core.net.Severity类的典型用法代码示例。如果您正苦于以下问题:Java Severity类的具体用法?Java Severity怎么用?Java Severity使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Severity类属于org.apache.logging.log4j.core.net包,在下文中一共展示了Severity类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decodeWithValidRawMessagePayloadReturnsValidMessage
import org.apache.logging.log4j.core.net.Severity; //导入依赖的package包/类
@Test
public void decodeWithValidRawMessagePayloadReturnsValidMessage() throws Exception {
final DateTime timestamp = new DateTime(2016, 9, 20, 0, 0, DateTimeZone.UTC);
final LogEvent logEvent = createLogEvent(timestamp);
final byte[] payload;
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream outputStream = new ObjectOutputStream(baos)) {
outputStream.writeObject(logEvent);
outputStream.flush();
payload = baos.toByteArray();
}
assertThat(payload).isNotNull();
final RawMessage rawMessage = new RawMessage(payload);
final Message message = codec.decode(rawMessage);
assertThat(message).isNotNull();
assertThat(message.getMessage()).isEqualTo("Test");
assertThat(message.getSource()).isEqualTo("example.org");
assertThat(message.getTimestamp()).isEqualTo(timestamp);
assertThat(message.getFields())
.containsKey("_id")
.containsEntry("cluster_id", "cluster-id")
.containsEntry("node_id", "node-id")
.containsEntry("level", Severity.DEBUG.getCode())
.containsEntry("log4j_level", "TRACE")
.containsEntry("log4j_level_int", Level.TRACE.intLevel())
.containsEntry("marker", "TestMarker")
.containsEntry("logger_name", "org.example.Test")
.containsEntry("context_foobar", "quux")
.containsEntry("context_stack", ImmutableList.of("one", "two"))
.containsEntry("exception_class", "java.lang.Throwable")
.containsEntry("exception_message", "Test");
assertThat((String) message.getField("exception_stack_trace")).startsWith("java.lang.Throwable: Test");
}
示例2: processLogEvent
import org.apache.logging.log4j.core.net.Severity; //导入依赖的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;
}
示例3: append
import org.apache.logging.log4j.core.net.Severity; //导入依赖的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);
}
}
示例4: formatLevel
import org.apache.logging.log4j.core.net.Severity; //导入依赖的package包/类
/**
* http://en.wikipedia.org/wiki/Syslog#Severity_levels
*/
private int formatLevel(final Level level) {
return Severity.getSeverity(level).getCode();
}