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 1 commit
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
96 changes: 77 additions & 19 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,17 @@
/**
* 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;
private String[] LEVEL = {
"DEBUG", "INFO", "WARN", "ERROR"
};
private final int DEBUG = 0, INFO = 1, WARN = 2, ERROR = 3;

/** Log severity level */
private enum Level {
Expand All @@ -48,10 +52,44 @@ private enum Level {
public LogglyTree(String token) {
loggly = new LogglyClient(token);

// Setup an async callback
// 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);
// 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);
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 All @@ -70,7 +108,7 @@ public void failure(String error) {
*/
@Override
public void d(String message, Object... args) {
log(Level.DEBUG, message, args);
log(DEBUG, message, args);
}

/**
Expand All @@ -81,7 +119,7 @@ public void d(String message, Object... args) {
*/
@Override
public void d(Throwable t, String message, Object... args) {
log(Level.DEBUG, message, t, args);
log(DEBUG, message, t, args);
}

/**
Expand All @@ -91,7 +129,7 @@ public void d(Throwable t, String message, Object... args) {
*/
@Override
public void i(String message, Object... args) {
log(Level.INFO, message, args);
log(INFO, message, args);
}

/**
Expand All @@ -102,7 +140,7 @@ public void i(String message, Object... args) {
*/
@Override
public void i(Throwable t, String message, Object... args) {
log(Level.INFO, message, t, args);
log(INFO, message, t, args);
}

/**
Expand All @@ -112,7 +150,8 @@ public void i(Throwable t, String message, Object... args) {
*/
@Override
public void e(String message, Object... args) {
log(Level.ERROR, message, args);
log(ERROR, message, args);

}

/**
Expand All @@ -123,7 +162,7 @@ public void e(String message, Object... args) {
*/
@Override
public void e(Throwable t, String message, Object... args) {
log(Level.ERROR, message, t, args);
log(ERROR, message, t, args);
}

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

}

/**
Expand All @@ -144,7 +184,8 @@ public void w(String message, Object... args) {
*/
@Override
public void w(Throwable t, String message, Object... args) {
log(Level.WARN, message, t, args);
log(WARN, message, t, args);

}

/**
Expand All @@ -153,11 +194,20 @@ 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(int level, String message, Object... args) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\",\"appName\" : \"%3$s\"}",
LEVEL[level],
String.format(message, args).replace("\"", "\\\""), String.format(appName).replace("\"", "\\\""));
}

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,10 +230,17 @@ 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("\"", "\\\""));
}

private String toJson(int level, String message, Throwable t, Object... args) {
return String.format("{\"level\": \"%1$s\", \"message\": \"%2$s\", \"exception\": \"%3$s\",\"appName\" : \"%4$s\"}",
LEVEL[level],
String.format(message, args).replace("\"", "\\\""),
formatThrowable(t), String.format(appName).replace("\"", "\\\""));
}

/**
Expand All @@ -193,7 +250,7 @@ private String toJson(Level level, String message, Throwable t, Object... args)
* @param t throwable
* @param args message formatting arguments
*/
private void log(Level level, String message, Throwable t, Object... args) {
private void log(int level, String message, Throwable t, Object... args) {

Choose a reason for hiding this comment

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

@psquickitjayant why change from Level to int?

Copy link
Author

Choose a reason for hiding this comment

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

@mostlyjason the new library Timber.Tree takes int as parameter

loggly.log(toJson(level, message, t, args), handler);
}

Expand All @@ -203,7 +260,7 @@ private void log(Level level, String message, Throwable t, Object... args) {
* @param message message to be logged
* @param args message formatting arguments
*/
private void log(Level level, String message, Object... args) {
private void log(int level, String message, Object... args) {
loggly.log(toJson(level, message, args), handler);
}

Expand All @@ -215,7 +272,8 @@ private void log(Level level, String message, Object... args) {
* to clear tags
*/
@Override
public final void tag(String tag) {
loggly.setTags(tag);
protected void log(int level, String tag, String message, Throwable t) {
// TODO Auto-generated method stub
loggly.log(toJson(level, tag, message, t), handler);
}
}