-
Notifications
You must be signed in to change notification settings - Fork 115
Home
It's very simple GELF implementation in pure Java with the log4j appender. It supports chunked messages which allows you to send large log messages (stacktraces, environment variables, additional fields, etc.) to Graylog2 Server.
Grab latest JAR from the downloads section and drop it into your classpath. You will also need com.googlecode.json-simple, json-simple and log4j.
To send a GELF message:
GelfMessage message = new GelfMessage("Short message", "Long message", new Date(), "1");
message.setHost("origin-host");
GelfSender gelfSender = new GelfSender("localhost");
if (message.isValid()) {
gelfSender.sendMessage(message);
}
To send a GELF message with additional fields:
GelfMessage message = new GelfMessage("Short message", "Long message", new Date(), "1");
message.setHost("origin-host");
message.addField("id", "LOLCAT").addField("_id", "typos in my closet");
GelfSender gelfSender = new GelfSender("localhost");
if (message.isValid()) {
gelfSender.sendMessage(message);
}
GELF log for4j appender will use log message as a short message and a stacktrace (if exception available) as a long message.
To use GELF Facility as appender in Log4J (XML format):
<appender name="graylog" class="org.graylog2.log.GelfAppender">
<param name="graylogHost" value="192.168.0.201"/>
<param name="originHost" value="my.machine.example.com"/>
<param name="extractStacktrace" value="true"/>
<param name="addExtendedInformation" value="true"/>
<param name="facility" value="gelf-java"/>
<param name="Threshold" value="INFO"/>
<param name="additionalFields" value="{'environment': 'DEV', 'application': 'MyAPP'}"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d |%t|%c{2}| %-5p - %m%n"/>
</layout>
</appender>
and then add it as a one of appenders:
<root>
<priority value="INFO"/>
<appender-ref ref="graylog"/>
</root>
Or, in the log4j.properties format:
# Define the graylog2 destination
log4j.appender.graylog2=org.graylog2.log.GelfAppender
log4j.appender.graylog2.graylogHost=graylog2.example.com
log4j.appender.graylog2.originHost=my.machine.example.com
log4j.appender.graylog2.facility=gelf-java
log4j.appender.graylog2.layout=org.apache.log4j.PatternLayout
log4j.appender.graylog2.layout.ConversionPattern=%d |%t|%c{2}| %-5p - %m%n
log4j.appender.graylog2.additionalFields={'environment': 'DEV', 'application': 'MyAPP'}
# Send all INFO logs to graylog2
log4j.rootLogger=INFO, graylog2
The Graylog Extended Log Format (GELF) avoids the shortcomings of classic plain syslog:
- Limited to length of 1024 byte
- Not much space for payloads like backtraces
- Unstructured. You can only build a long message string and define priority, severity etc.
You can get more information here: http://www.graylog2.org/about/gelf