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

Updated to Timber.Tree #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
74 changes: 63 additions & 11 deletions src/main/java/com/github/tony19/timber/loggly/LogglyTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
/**
* A <a href="https://github.com/JakeWharton/timber">Timber</a> tree that posts
* log messages to <a href="http://loggly.com">Loggly</a>
*
* @author [email protected]
*/
public class LogglyTree extends Timber.HollowTree implements Timber.TaggedTree {
public class LogglyTree extends Timber.Tree {

private final LogglyClient loggly;
private LogglyClient.Callback handler;
private String appName="";


/** Log severity level */
private enum Level {
Expand All @@ -52,6 +53,43 @@ public LogglyTree(String token) {
// TODO: handle failed messages with N retries
handler = new LogglyClient.Callback() {
@Override
public void success() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


}

@Override
public void failure(String error) {
System.err.println("LogglyTree failed: " + error);

}
};
}

public LogglyTree(String token, String tag) {
loggly = new LogglyClient(token);
tag(tag);
// Setup an async callback
// TODO: handle failed messages with N retries
handler = new LogglyClient.Callback() {@Override
public void success() {
// XXX: Handle success
}

@Override
public void failure(String error) {
System.err.println("LogglyTree failed: " + error);
}
};

}

public LogglyTree(String token, String tag, String appName) {
loggly = new LogglyClient(token);
tag(tag);
this.appName = appName;
// Setup an async callback
// TODO: handle failed messages with N retries
handler = new LogglyClient.Callback() {@Override
public void success() {
// XXX: Handle success
}
Expand Down Expand Up @@ -113,6 +151,7 @@ public void i(Throwable t, String message, Object... args) {
@Override
public void e(String message, Object... args) {
log(Level.ERROR, message, args);

}

/**
Expand All @@ -134,6 +173,7 @@ public void e(Throwable t, String message, Object... args) {
@Override
public void w(String message, Object... args) {
log(Level.WARN, message, args);

}

/**
Expand All @@ -153,11 +193,14 @@ public void w(Throwable t, String message, Object... args) {
* @param message message to be logged
* @param args message formatting arguments
* @return JSON string
*
*
*/

private String toJson(Level level, String message, Object... args) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\"}",
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\",\"appName\" : \"%3$s\"}",
level,
String.format(message, args).replace("\"", "\\\""));
String.format(message, args).replace("\"", "\\\""), String.format(appName).replace("\"", "\\\""));
}

/**
Expand All @@ -180,12 +223,13 @@ private String formatThrowable(Throwable t) {
* @return JSON string
*/
private String toJson(Level level, String message, Throwable t, Object... args) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\", \"exception\": \"%3$s\"}",
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\", \"exception\": \"%3$s\",\"appName\" : \"%4$s\"}",
level,
String.format(message, args).replace("\"", "\\\""),
formatThrowable(t));
formatThrowable(t), String.format(appName).replace("\"", "\\\""));
}


/**
* Asynchronously sends a log event to Loggly
* @param level log severity level
Expand All @@ -208,13 +252,21 @@ private void log(Level level, String message, Object... args) {
}

/**
* Sets the Loggly tag for all logs going forward. This differs from
* the API of {@code Timber.TaggedTree} in that it's not a one-shot
* tag.
* @param tag desired tag or CSV of multiple tags; use empty string
* to clear tags
* Sets the Loggly log for all logs going forward.
* @param level log severity level
* @param message message to be logged
* @param t throwable
*/
@Override
protected void log(int level, String tag, String message, Throwable t) {
loggly.log(toJson(Level.values()[level], tag, message, t), handler);
}

/**
* Sets the Loggly tag for all logs going forward.
* @param tag desired tag or CSV of multiple tags; use empty string
* to clear tags
*/
public final void tag(String tag) {
loggly.setTags(tag);
}
Expand Down