All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.log.GelfJsonAppender Maven / Gradle / Ivy

Go to download

GELF implementation in Java and log4j appender without any dependencies.

The newest version!
package org.graylog2.log;

import java.util.Map;

import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;
import org.graylog2.message.GelfMessage;
import org.graylog2.message.GelfMessageBuilderException;
import org.graylog2.sender.GelfSender;
import org.graylog2.sender.GelfSenderException;
import org.json.simple.JSONValue;

/**
 * A GelfAppender which will parse the given JSON message into additional fields
 * in GELF
 *
 * @author Anton Yakimov
 * @author Jochen Schalanda
 * @author the-james-burton
 */
public class GelfJsonAppender extends GelfAppender {
	@Override
	protected void append(final LoggingEvent event) {
		GelfSender sender = getGelfSender();
		if (sender == null) {
			errorHandler.error("Could not send GELF message. Gelf Sender is not initialised and equals null");
		} else {
			try {
				GelfMessage gelfMessage = getMessageFactory().makeMessage(layout, event, this);
				@SuppressWarnings("unchecked")
				Map fields = (Map) JSONValue.parse(event.getMessage().toString());
				if (fields != null) {
					for (String key : fields.keySet()) {
						gelfMessage.addField(key, fields.get(key));
					}
				}
				sender.sendMessage(gelfMessage);
			} catch (GelfMessageBuilderException exception) {
				errorHandler.error("Error building GELF message", exception, ErrorCode.WRITE_FAILURE);
			} catch (GelfSenderException exception) {
				errorHandler.error("Error during sending GELF message. Error code: " + exception.getErrorCode() + ".",
						exception.getCause(), ErrorCode.WRITE_FAILURE);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy