Java 类org.apache.logging.log4j.core.net.Severity 实例源码

项目:graylog-plugin-internal-logs    文件:SerializedLogEventCodecTest.java   
@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");
}
项目:graylog-plugin-internal-logs    文件:SerializedLogEventCodec.java   
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;
}
项目:log4j2-gelf    文件:GelfAppender.java   
@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);
    }
}
项目:logging-log4j2    文件:GelfLayout.java   
/**
 * http://en.wikipedia.org/wiki/Syslog#Severity_levels
 */
private int formatLevel(final Level level) {
    return Severity.getSeverity(level).getCode();
}