Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Support GELF 1.1 spec #35
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Nov 27, 2014
1 parent 034f0a8 commit e2dd1cf
Show file tree
Hide file tree
Showing 32 changed files with 265 additions and 97 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ logstash-gelf
[![Build Status](https://api.travis-ci.org/mp911de/logstash-gelf.svg)](https://travis-ci.org/mp911de/logstash-gelf) [![Coverage Status](https://img.shields.io/coveralls/mp911de/logstash-gelf.svg)](https://coveralls.io/r/mp911de/logstash-gelf) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/biz.paluch.logging/logstash-gelf/badge.svg)](https://maven-badges.herokuapp.com/maven-central/biz.paluch.logging/logstash-gelf)


Provides logging to logstash using the Graylog Extended Logging Format (GELF) for using with:
Provides logging to logstash using the Graylog Extended Logging Format ([GELF](http://www.graylog2.org/resources/gelf/specification)) for using with:

* [Java Util Logging](#jul)
* [log4j 1.2.x](#log4j)
* [log4j 2.x](#log4j2)
* [JBoss AS7/Wildfly (JBoss AS8) (mix of Java Util Logging with log4j MDC)](#jbossas7)
* [Logback](#logback)

See also http://logging.paluch.biz/ for further documentation.
See also http://logging.paluch.biz/ or http://www.graylog2.org/resources/gelf/specification for further documentation.


Including it in your project
Expand Down Expand Up @@ -50,6 +50,7 @@ handlers = biz.paluch.logging.gelf.jul.GelfLogHandler, java.util.logging.Console

biz.paluch.logging.gelf.jul.GelfLogHandler.host=udp:localhost
biz.paluch.logging.gelf.jul.GelfLogHandler.port=12201
biz.paluch.logging.gelf.jul.GelfLogHandler.version=1.1
biz.paluch.logging.gelf.jul.GelfLogHandler.facility=java-test
biz.paluch.logging.gelf.jul.GelfLogHandler.extractStackTrace=true
biz.paluch.logging.gelf.jul.GelfLogHandler.filterStackTrace=true
Expand All @@ -72,6 +73,7 @@ log4j.appender.gelf=biz.paluch.logging.gelf.log4j.GelfLogAppender
log4j.appender.gelf.Threshold=INFO
log4j.appender.gelf.Host=udp:localhost
log4j.appender.gelf.Port=12201
log4j.appender.gelf.Version=1.1
log4j.appender.gelf.Facility=java-test
log4j.appender.gelf.ExtractStackTrace=true
log4j.appender.gelf.FilterStackTrace=true
Expand All @@ -95,6 +97,7 @@ XML:
<param name="Threshold" value="INFO" />
<param name="Host" value="udp:localhost" />
<param name="Port" value="12201" />
<param name="Version" value="1.1" />
<param name="Facility" value="java-test" />
<param name="ExtractStackTrace" value="true" />
<param name="FilterStackTrace" value="true" />
Expand Down Expand Up @@ -171,7 +174,7 @@ XML:
```xml
<Configuration>
<Appenders>
<Gelf name="gelf" graylogHost="udp:localhost" graylogPort="12201" extractStackTrace="true"
<Gelf name="gelf" host="udp:localhost" port="12201" version="1.1" extractStackTrace="true"
filterStackTrace="true" mdcProfiling="true" includeFullMdc="true" maximumMessageSize="8192"
originHost="%host{fqdn}">
<Field name="timestamp" pattern="%d{dd MMM yyyy HH:mm:ss,SSS}" />
Expand Down Expand Up @@ -210,6 +213,7 @@ standalone.xml
<properties>
<property name="host" value="udp:localhost" />
<property name="port" value="12201" />
<property name="version" value="1.1" />
<property name="facility" value="java-test" />
<property name="extractStackTrace" value="true" />
<property name="filterStackTrace" value="true" />
Expand All @@ -226,6 +230,17 @@ standalone.xml
<property name="includeFullMdc" value="true" />
</properties>
</custom-handler>

...

<root-logger>
<level name="INFO"/>
<handlers>
<handler name="FILE"/>
<handler name="CONSOLE"/>
<handler name="GelfLogger"/>
</handlers>
</root-logger>
```

<a name="logback"/>
Expand All @@ -243,6 +258,7 @@ logback.xml Example:
<appender name="gelf" class="biz.paluch.logging.gelf.logback.GelfLogbackAppender">
<host>udp:localhost</host>
<port>12201</port>
<version>1.1</version>
<facility>java-test</facility>
<extractStackTrace>true</extractStackTrace>
<filterStackTrace>true</filterStackTrace>
Expand Down Expand Up @@ -294,6 +310,7 @@ Following settings can be used:
* `port` (since version 1.2.0, Optional): Port, default 12201
* `graylogHost` (until version 1.1.0, Mandatory): Hostname/IP-Address of the Logstash Host
* `graylogPort` (until version 1.1.0, Optional): Port, default 12201
* `version` (Optional): GELF Version 1.0 or 1.1, default 1.0
* `originHost` (Optional): Originating Hostname, default FQDN Hostname
* `extractStackTrace` (Optional): Post Stack-Trace to StackTrace field, default false
* `filterStackTrace` (Optional): Perform Stack-Trace filtering (true/false), default false
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/biz/paluch/logging/gelf/GelfMessageAssembler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package biz.paluch.logging.gelf;

import static biz.paluch.logging.gelf.GelfMessageBuilder.newInstance;
import biz.paluch.logging.RuntimeContainer;
import biz.paluch.logging.StackTraceFilter;
import biz.paluch.logging.gelf.intern.GelfMessage;
import biz.paluch.logging.gelf.intern.HostAndPortProvider;
import static biz.paluch.logging.gelf.GelfMessageBuilder.*;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -14,18 +10,26 @@
import java.util.Date;
import java.util.List;

import biz.paluch.logging.RuntimeContainer;
import biz.paluch.logging.StackTraceFilter;
import biz.paluch.logging.gelf.intern.GelfMessage;
import biz.paluch.logging.gelf.intern.HostAndPortProvider;

/**
* @author <a href="mailto:[email protected]">Mark Paluch</a>
* @since 26.09.13 15:05
*/
public class GelfMessageAssembler implements HostAndPortProvider {

private static final int MAX_SHORT_MESSAGE_LENGTH = 250;
private static final int MAX_PORT_NUMBER = 65535;
private static final int MAX_MESSAGE_SIZE = Integer.MAX_VALUE;

public static final String FIELD_MESSAGE_PARAM = "MessageParam";
public static final String FIELD_STACK_TRACE = "StackTrace";

private String host;
private String version = GelfMessage.GELF_VERSION;
private String originHost;
private int port;
private String facility;
Expand Down Expand Up @@ -53,7 +57,7 @@ public void initialize(PropertyProvider propertyProvider) {
port = propertyProvider.getProperty(PropertyProvider.PROPERTY_GRAYLOG_PORT);
}

if (port != null) {
if (port != null && !"".equals(port)) {
this.port = Integer.parseInt(port);
}

Expand All @@ -63,6 +67,11 @@ public void initialize(PropertyProvider propertyProvider) {

setupStaticFields(propertyProvider);
facility = propertyProvider.getProperty(PropertyProvider.PROPERTY_FACILITY);
String version = propertyProvider.getProperty(PropertyProvider.PROPERTY_VERSION);

if (version != null && !"".equals(version)) {
this.version = version;
}

String messageSize = propertyProvider.getProperty(PropertyProvider.PROPERTY_MAX_MESSAGE_SIZE);
if (messageSize != null) {
Expand All @@ -88,6 +97,7 @@ public GelfMessage createGelfMessage(LogEvent logEvent) {

builder.withShortMessage(shortMessage).withFullMessage(message).withJavaTimestamp(logEvent.getLogTimestamp());
builder.withLevel(logEvent.getSyslogLevel());
builder.withVersion(getVersion());

for (MessageField field : fields) {
Values values = getValues(logEvent, field);
Expand Down Expand Up @@ -215,6 +225,9 @@ public int getPort() {
}

public void setPort(int port) {
if (port > MAX_PORT_NUMBER || port < 1) {
throw new IllegalArgumentException("Invalid port number: " + port + ", supported range: 1-" + MAX_PORT_NUMBER);
}
this.port = port;
}

Expand Down Expand Up @@ -255,7 +268,26 @@ public int getMaximumMessageSize() {
}

public void setMaximumMessageSize(int maximumMessageSize) {

if (maximumMessageSize > MAX_MESSAGE_SIZE || maximumMessageSize < 1) {
throw new IllegalArgumentException("Invalid maximum message size: " + maximumMessageSize + ", supported range: 1-"
+ MAX_MESSAGE_SIZE);
}

this.maximumMessageSize = maximumMessageSize;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {

if (!GelfMessage.GELF_VERSION_1_0.equals(version) && !GelfMessage.GELF_VERSION_1_1.equals(version)) {
throw new IllegalArgumentException("Invalid GELF version: " + version + ", supported range: "
+ GelfMessage.GELF_VERSION_1_0 + ", " + GelfMessage.GELF_VERSION_1_1);
}

this.version = version;
}
}
5 changes: 3 additions & 2 deletions src/main/java/biz/paluch/logging/gelf/GelfMessageBuilder.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package biz.paluch.logging.gelf;

import biz.paluch.logging.gelf.intern.GelfMessage;

import java.util.HashMap;
import java.util.Map;

import biz.paluch.logging.gelf.intern.GelfMessage;

/**
* Builder to create a GelfMessage.
*
Expand Down Expand Up @@ -161,6 +161,7 @@ public GelfMessage build() {
gelfMessage.setHost(host);
gelfMessage.setJavaTimestamp(javaTimestamp);
gelfMessage.setFacility(facility);
gelfMessage.setFacility(facility);

return gelfMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface PropertyProvider {

String PROPERTY_FILTER = "filter";
String PROPERTY_LEVEL = "level";
String PROPERTY_VERSION = "version";

String getProperty(String propertyName);
}
36 changes: 30 additions & 6 deletions src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package biz.paluch.logging.gelf.intern;

import org.json.simple.JSONValue;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -11,6 +9,8 @@
import java.util.Map;
import java.util.zip.GZIPOutputStream;

import org.json.simple.JSONValue;

/**
* (c) https://github.com/t0xa/gelfj
*/
Expand All @@ -24,9 +24,14 @@ public class GelfMessage {
public static final String FIELD_FACILITY = "facility";
public static final String ID_NAME = "id";

public static final String GELF_VERSION = "1.0";
public static final String GELF_VERSION_1_0 = "1.0";
public static final String GELF_VERSION_1_1 = "1.1";

public static final String GELF_VERSION = GELF_VERSION_1_0;

public static final String DEFAULT_FACILITY = "logstash-gelf";
public static final int DEFAULT_MESSAGE_SIZE = 8192;
public static final int DEFAUL_LEVEL = 7;

private static final byte[] GELF_CHUNKED_ID = new byte[] { 0x1e, 0x0f };
private static final BigDecimal TIME_DIVISOR = new BigDecimal(1000);
Expand Down Expand Up @@ -69,11 +74,26 @@ public String toJson(String additionalFieldPrefix) {
}

if (getJavaTimestamp() != 0) {
map.put(FIELD_TIMESTAMP, getTimestamp());
if (GELF_VERSION_1_1.equals(version)) {
map.put(FIELD_TIMESTAMP, getTimestampAsBigDecimal().doubleValue());
} else {
map.put(FIELD_TIMESTAMP, getTimestamp());
}
}

if (!isEmpty(getLevel())) {
map.put(FIELD_LEVEL, getLevel());
if (GELF_VERSION_1_1.equals(version)) {
int level;
try {
level = Integer.parseInt(getLevel());
} catch (NumberFormatException ex) {
// fallback on the default value
level = DEFAUL_LEVEL;
}
map.put(FIELD_LEVEL, level);
} else {
map.put(FIELD_LEVEL, getLevel());
}
}

if (!isEmpty(getFacility())) {
Expand Down Expand Up @@ -219,8 +239,12 @@ public void setFullMessage(String fullMessage) {
this.fullMessage = fullMessage;
}

public BigDecimal getTimestampAsBigDecimal() {
return new BigDecimal(javaTimestamp).divide(TIME_DIVISOR);
}

public String getTimestamp() {
return new BigDecimal(javaTimestamp).divide(TIME_DIVISOR).toPlainString();
return getTimestampAsBigDecimal().toPlainString();
}

public Long getJavaTimestamp() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/biz/paluch/logging/gelf/jul/GelfLogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* </ul>
* </li>
* <li>port (Optional): Port, default 12201</li>
* <li>version (Optional): GELF Version 1.0 or 1.1, default 1.0</li>
* <li>originHost (Optional): Originating Hostname, default FQDN Hostname</li>
* <li>extractStackTrace (Optional): Post Stack-Trace to StackTrace field, default false</li>
* <li>filterStackTrace (Optional): Perform Stack-Trace filtering (true/false), default false</li>
Expand Down Expand Up @@ -230,4 +231,12 @@ public int getMaximumMessageSize() {
public void setMaximumMessageSize(int maximumMessageSize) {
gelfMessageAssembler.setMaximumMessageSize(maximumMessageSize);
}

public String getVersion() {
return gelfMessageAssembler.getVersion();
}

public void setVersion(String version) {
gelfMessageAssembler.setVersion(version);
}
}
11 changes: 4 additions & 7 deletions src/main/java/biz/paluch/logging/gelf/jul/JulLogEvent.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package biz.paluch.logging.gelf.jul;

import biz.paluch.logging.gelf.GelfUtil;
import biz.paluch.logging.gelf.LogEvent;
import biz.paluch.logging.gelf.LogMessageField;
import biz.paluch.logging.gelf.MessageField;
import biz.paluch.logging.gelf.Values;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
Expand All @@ -18,6 +12,9 @@
import java.util.logging.Level;
import java.util.logging.LogRecord;

import biz.paluch.logging.gelf.*;
import biz.paluch.logging.gelf.intern.GelfMessage;

/**
* @author <a href="mailto:[email protected]">Mark Paluch</a>
* @since 26.09.13 15:22
Expand Down Expand Up @@ -130,7 +127,7 @@ private int levelToSyslogLevel(final Level level) {
} else if (level == Level.INFO) {
syslogLevel = 6;
} else {
syslogLevel = 7;
syslogLevel = GelfMessage.DEFAUL_LEVEL;
}
return syslogLevel;
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/biz/paluch/logging/gelf/log4j/GelfLogAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import static biz.paluch.logging.gelf.LogMessageField.NamedLogField.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import biz.paluch.logging.RuntimeContainer;
import biz.paluch.logging.gelf.*;
import biz.paluch.logging.gelf.intern.*;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

/**
* Logging-Handler for GELF (Graylog Extended Logging Format). This Java-Util-Logging Handler creates GELF Messages and posts
Expand All @@ -26,6 +23,7 @@
* </ul>
* </li>
* <li>port (Optional): Port, default 12201</li>
* <li>version (Optional): GELF Version 1.0 or 1.1, default 1.0</li>
* <li>originHost (Optional): Originating Hostname, default FQDN Hostname</li>
* <li>extractStackTrace (Optional): Post Stack-Trace to StackTrace field, default false</li>
* <li>filterStackTrace (Optional): Perform Stack-Trace filtering (true/false), default false</li>
Expand Down Expand Up @@ -248,4 +246,12 @@ public void setIncludeFullMdc(boolean includeFullMdc) {
gelfMessageAssembler.setIncludeFullMdc(includeFullMdc);
}

public String getVersion() {
return gelfMessageAssembler.getVersion();
}

public void setVersion(String version) {
gelfMessageAssembler.setVersion(version);
}

}
Loading

0 comments on commit e2dd1cf

Please sign in to comment.