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

Added ability to set tags line in separate attribute of log4j properties... #1

Open
wants to merge 4 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.project
logs
/pom.xml.versionsBackup
/bin
1 change: 0 additions & 1 deletion .settings/org.maven.ide.eclipse.prefs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#Fri Apr 29 11:43:59 NZST 2011
activeProfiles=
eclipse.preferences.version=1
fullBuildGoals=process-test-resources
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
<version>2.3.0</version>
</dependency>
</dependencies>
</project>
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage is relatively straight forward.
class="com.spidertracks.loggly.LogglyAppender">
<param name="dirName" value="A working directory to store the HSQL queue buffer" />
<param name="logglyUrl" value="Your loggly url goes here" />
<param name="logglyTags" value="Your loggly tags goes here" /> <!-- Optional value -->
<param name="proxyHost" value="A dns name or an ip address"/> <!-- Optional value -->
<param name="proxyPort" value="The port number for the proxy"/> <!-- Optional value -->
<!-- The maximum number of messages to upload in a single http POST -->
Expand All @@ -23,7 +24,8 @@ or

log4j.appender.loggly=com.spidertracks.loggly.LogglyAppender
log4j.appender.loggly.dirName=logs/
log4j.appender.loggly.logglyUrl=https://logs.loggly.com/inputs/xxxxx-xxxx
log4j.appender.loggly.logglyUrl=https://logs-01.loggly.com/inputs/xxxxx-xxxx
log4j.appender.loggly.logglyTags=myApp1,newYork,middleware
log4j.appender.loggly.proxyHost=example.com
log4j.appender.loggly.proxyPort=8080
log4j.appender.loggly.batchSize=50
Expand All @@ -44,4 +46,5 @@ This supports guaranteed delivery. If the logger cannot contact Loggly,
Excepts when the request http status codes i 400 Bad Request - then the messages is dumped.
ex. a too large message.


# Authentic source
http://www.loggly.com/docs/api-sending-data/
3 changes: 0 additions & 3 deletions src/main/java/com/spidertracks/loggly/EmbeddedDb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package com.spidertracks.loggly;

import java.sql.*;
Expand Down
50 changes: 36 additions & 14 deletions src/main/java/com/spidertracks/loggly/LogglyAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import org.apache.log4j.spi.LoggingEvent;

/**
* Currently uses an asynchronous blocking queue to write messages. Messages are written to files with sequential identifiers, these sequential files are then read by the reader thread. When a file is
* fully consumed, it is removed.
* Currently uses an asynchronous blocking queue to write messages.
* Messages are written to files with sequential identifiers, these sequential files are then read by the reader thread.
* When a file is fully consumed, it is removed.
*
* @author Todd Nine
*/
Expand All @@ -21,11 +22,11 @@ public class LogglyAppender extends AppenderSkeleton {

private EmbeddedDb db;

// private LogglyMessageQueue messageQ;

private String dirName;

private String logglyUrl;

private String logglyTags;

private int batchSize = 50;

Expand Down Expand Up @@ -63,11 +64,15 @@ protected void append(LoggingEvent event) {
/**
* We always only produce to the current file. So there's no need for locking
*/

assert this.layout != null : "Cannot log, there is no layout configured.";

String output = this.layout.format(event);

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
if (event.getThrowableInformation() != null && event.getThrowableInformation().getThrowable() != null) {
event.getThrowableInformation().getThrowable().printStackTrace(pw);
output += sw.toString();
}
synchronized (waitLock) {
db.writeEntry(output, System.nanoTime());
waitLock.notify();
Expand Down Expand Up @@ -111,9 +116,9 @@ private enum ThreadState { START, RUNNING, STOP_REQUESTED, STOPPED };
private class HttpPost implements Runnable {

/**
* Loggly max message size as stated by http://www.loggly.com/blog/2011/09/logging-out-of-your-java-code/
* Loggly max message size as stated by https://loggly.desk.com/customer/portal/questions/3431783-maximum-rest-api-request-size
*/
private static final int LOGGLY_MAX_MESSAGE_SIZE = 32 * 1024;
private static final int LOGGLY_MAX_MESSAGE_SIZE = 1024 * 1024;

// State variables needs to be volatile, otherwise it can be cached local to the thread and stop() will never work
volatile ThreadState curState = ThreadState.START;
Expand Down Expand Up @@ -176,7 +181,7 @@ public void run() {
}
}
} catch (IOException e) {
errorHandler.error(String.format("Unable to send data to loggly at URL %s", logglyUrl), e, 2);
errorHandler.error(String.format("Unable to send data to loggly at URL %s", getLogglyPreparedURL()), e, 2);
}
}

Expand Down Expand Up @@ -240,9 +245,8 @@ public boolean waitUntilDbInitialized() {
* @param message
* @throws IOException
*/

private int sendData(List<Entry> messages) throws IOException {
URL url = new URL(logglyUrl);
URL url = new URL(getLogglyPreparedURL());
Proxy proxy = Proxy.NO_PROXY;
if (proxyHost != null) {
SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
Expand Down Expand Up @@ -283,19 +287,28 @@ private int sendData(List<Entry> messages) throws IOException {
while ((value = in.read()) != -1) {
response.append((char) value);
}
errorHandler.error(String.format("Unable to send data to loggly at URL %s Response %s (Logging error, this will not functioning of the main program)", logglyUrl,
errorHandler.error(String.format("Unable to send data to loggly at URL %s Response %s (Logging error, this will not functioning of the main program)", getLogglyPreparedURL(),
response));
} catch (IOException ee) {
errorHandler.error(String.format("Unable to send data to loggly at URL %s (Logging error, this will not functioning of the main program)", logglyUrl), e, 2);
errorHandler.error(String.format("Unable to send data to loggly at URL %s (Logging error, this will not functioning of the main program)", getLogglyPreparedURL()), e, 2);
}
}
return respCode;
}

/**
* @return URL to loggly with tags appended if exist.
*/
private String getLogglyPreparedURL() {
if (logglyTags != null && !logglyTags.isEmpty()) {
return logglyUrl + "/tag/" + logglyTags;
}
return logglyUrl;
}

/**
* Stop this thread sending data and write the last read position
*/

public void stop() {
LogLog.debug("Loggly: Stopping background thread");
requestedState = ThreadState.STOPPED;
Expand Down Expand Up @@ -362,4 +375,13 @@ public void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}

/**
* Tags, which Loggly will apply as meta data to your event.
*
* @param logglyTags
*/
public void setLogglyTags(String logglyTags) {
this.logglyTags = logglyTags;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
* $Id$
*/
package com.spidertracks.loggly;

import java.util.Properties;
Expand Down Expand Up @@ -53,7 +50,7 @@ public static void configureLoggingToConsole(String apiKey) {

loggingProperties.setProperty("log4j.appender.loggly","com.spidertracks.loggly.LogglyAppender");
loggingProperties.setProperty("log4j.appender.loggly.dirName","logs/");
loggingProperties.setProperty("log4j.appender.loggly.logglyUrl","https://logs.loggly.com/inputs/" + apiKey);
loggingProperties.setProperty("log4j.appender.loggly.logglyUrl","https://logs-01.loggly.com/inputs/" + apiKey);
loggingProperties.setProperty("log4j.appender.loggly.batchSize","50");
loggingProperties.setProperty("log4j.appender.loggly.layout","org.apache.log4j.EnhancedPatternLayout");
loggingProperties.setProperty("log4j.appender.loggly.layout.ConversionPattern","%d{ISO8601}{GMT}Z %5p [%t] %m%n");
Expand Down