Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #110: Changed GelfHandler's close to comply with Handler #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<connection>scm:git:[email protected]:t0xa/gelfj.git</connection>
<developerConnection>scm:git:[email protected]:t0xa/gelfj.git</developerConnection>
<url>[email protected]:t0xa/gelfj.git</url>
<tag>v1.1.15</tag>
<tag>HEAD</tag>
</scm>

<groupId>org.graylog2</groupId>
<artifactId>gelfj</artifactId>
<version>1.1.15</version>
<version>1.1.16-SNAPSHOT</version>
<packaging>jar</packaging>
<name>gelfj</name>
<description>GELF implementation in Java and log4j appender without any dependencies.</description>
Expand Down
83 changes: 47 additions & 36 deletions src/main/java/org/graylog2/logging/GelfHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class GelfHandler
private GelfSender gelfSender;
private boolean extractStacktrace;
private Map<String, String> fields;
private volatile boolean closed;

public GelfHandler() {
final LogManager manager = LogManager.getLogManager();
Expand Down Expand Up @@ -115,42 +116,15 @@ private String getLocalHostName() {
}

@Override
public synchronized void publish(final LogRecord record) {
if (!isLoggable(record)) {
return;
public void publish(final LogRecord record) {
if (!closed && isLoggable(record)) {
send(record);
}
}

private synchronized void send(LogRecord record) {
if (null == gelfSender) {
if (graylogHost == null && amqpURI == null) {
reportError("Graylog2 hostname and amqp uri are empty!", null, ErrorManager.WRITE_FAILURE);
} else if (graylogHost != null && amqpURI != null) {
reportError("Graylog2 hostname and amqp uri are both informed!", null, ErrorManager.WRITE_FAILURE);
} else {
try {
if (graylogHost.startsWith("tcp:")) {
String tcpGraylogHost = graylogHost.substring(4, graylogHost.length());
gelfSender = new GelfTCPSender(tcpGraylogHost, graylogPort);
} else if (graylogHost.startsWith("udp:")) {
String udpGraylogHost = graylogHost.substring(4, graylogHost.length());
gelfSender = new GelfUDPSender(udpGraylogHost, graylogPort);
} else if (amqpURI != null) {
gelfSender = new GelfAMQPSender(amqpURI, amqpExchangeName, amqpRoutingKey, amqpMaxRetries);
} else {
gelfSender = new GelfUDPSender(graylogHost, graylogPort);
}
} catch (UnknownHostException e) {
reportError("Unknown Graylog2 hostname:" + graylogHost, e, ErrorManager.WRITE_FAILURE);
} catch (SocketException e) {
reportError("Socket exception", e, ErrorManager.WRITE_FAILURE);
} catch (IOException e) {
reportError("IO exception", e, ErrorManager.WRITE_FAILURE);
} catch (URISyntaxException e) {
reportError("AMQP uri exception", e, ErrorManager.WRITE_FAILURE);
} catch (NoSuchAlgorithmException e) {
reportError("AMQP algorithm exception", e, ErrorManager.WRITE_FAILURE);
} catch (KeyManagementException e) {
reportError("AMQP key exception", e, ErrorManager.WRITE_FAILURE);
}
}
gelfSender = createSender();
}
if (null == gelfSender) {
reportError("Could not send GELF message", null, ErrorManager.WRITE_FAILURE);
Expand All @@ -160,14 +134,51 @@ public synchronized void publish(final LogRecord record) {
reportError("Error during sending GELF message. Error code: " + gelfSenderResult.getCode() + ".", gelfSenderResult.getException(), ErrorManager.WRITE_FAILURE);
}
}
}

private GelfSender createSender() {
GelfSender gelfSender = null;
if (graylogHost == null && amqpURI == null) {
reportError("Graylog2 hostname and amqp uri are empty!", null, ErrorManager.WRITE_FAILURE);
} else if (graylogHost != null && amqpURI != null) {
reportError("Graylog2 hostname and amqp uri are both informed!", null, ErrorManager.WRITE_FAILURE);
} else {
try {
if (graylogHost.startsWith("tcp:")) {
String tcpGraylogHost = graylogHost.substring(4, graylogHost.length());
gelfSender = new GelfTCPSender(tcpGraylogHost, graylogPort);
} else if (graylogHost.startsWith("udp:")) {
String udpGraylogHost = graylogHost.substring(4, graylogHost.length());
gelfSender = new GelfUDPSender(udpGraylogHost, graylogPort);
} else if (amqpURI != null) {
gelfSender = new GelfAMQPSender(amqpURI, amqpExchangeName, amqpRoutingKey, amqpMaxRetries);
} else {
gelfSender = new GelfUDPSender(graylogHost, graylogPort);
}
} catch (UnknownHostException e) {
reportError("Unknown Graylog2 hostname:" + graylogHost, e, ErrorManager.WRITE_FAILURE);
} catch (SocketException e) {
reportError("Socket exception", e, ErrorManager.WRITE_FAILURE);
} catch (IOException e) {
reportError("IO exception", e, ErrorManager.WRITE_FAILURE);
} catch (URISyntaxException e) {
reportError("AMQP uri exception", e, ErrorManager.WRITE_FAILURE);
} catch (NoSuchAlgorithmException e) {
reportError("AMQP algorithm exception", e, ErrorManager.WRITE_FAILURE);
} catch (KeyManagementException e) {
reportError("AMQP key exception", e, ErrorManager.WRITE_FAILURE);
}
}
return gelfSender;
}

@Override
public void close() {
@Override
public synchronized void close() {
if (null != gelfSender) {
gelfSender.close();
gelfSender = null;
}
closed = true;
}

private GelfMessage makeMessage(final LogRecord record) {
Expand Down