Search in sources :

Example 1 with GelfMessageLevel

use of org.graylog2.gelfclient.GelfMessageLevel in project graylog2-server by Graylog2.

the class GelfOutput method toGELFMessage.

protected GelfMessage toGELFMessage(final Message message) {
    final DateTime timestamp;
    final Object fieldTimeStamp = message.getField(Message.FIELD_TIMESTAMP);
    if (fieldTimeStamp instanceof DateTime) {
        timestamp = (DateTime) fieldTimeStamp;
    } else {
        timestamp = Tools.nowUTC();
    }
    final GelfMessageLevel messageLevel = extractLevel(message.getField(Message.FIELD_LEVEL));
    final String fullMessage = (String) message.getField(Message.FIELD_FULL_MESSAGE);
    final String facility = (String) message.getField("facility");
    final String forwarder = GelfOutput.class.getCanonicalName();
    final GelfMessageBuilder builder = new GelfMessageBuilder(message.getMessage(), message.getSource()).timestamp(timestamp.getMillis() / 1000.0d).additionalField("_forwarder", forwarder).additionalFields(message.getFields());
    if (messageLevel != null) {
        builder.level(messageLevel);
    }
    if (fullMessage != null) {
        builder.fullMessage(fullMessage);
    }
    if (facility != null) {
        builder.additionalField("_facility", facility);
    }
    return builder.build();
}
Also used : GelfMessageLevel(org.graylog2.gelfclient.GelfMessageLevel) GelfMessageBuilder(org.graylog2.gelfclient.GelfMessageBuilder) DateTime(org.joda.time.DateTime)

Example 2 with GelfMessageLevel

use of org.graylog2.gelfclient.GelfMessageLevel in project graylog2-server by Graylog2.

the class GelfOutput method extractLevel.

@Nullable
private GelfMessageLevel extractLevel(Object rawLevel) {
    GelfMessageLevel level;
    if (rawLevel instanceof Number) {
        final int numericLevel = ((Number) rawLevel).intValue();
        level = extractLevel(numericLevel);
    } else if (rawLevel instanceof String) {
        Integer numericLevel;
        try {
            numericLevel = Integer.parseInt((String) rawLevel);
        } catch (NumberFormatException e) {
            LOG.debug("Invalid message level " + rawLevel, e);
            numericLevel = null;
        }
        if (numericLevel == null) {
            level = null;
        } else {
            level = extractLevel(numericLevel);
        }
    } else {
        LOG.debug("Invalid message level {}", rawLevel);
        level = null;
    }
    return level;
}
Also used : GelfMessageLevel(org.graylog2.gelfclient.GelfMessageLevel) Nullable(javax.annotation.Nullable)

Aggregations

GelfMessageLevel (org.graylog2.gelfclient.GelfMessageLevel)2 Nullable (javax.annotation.Nullable)1 GelfMessageBuilder (org.graylog2.gelfclient.GelfMessageBuilder)1 DateTime (org.joda.time.DateTime)1