From 846945a61b3ca5b87a87213222490d5863ff1c9f Mon Sep 17 00:00:00 2001 From: psquickitjayant Date: Wed, 12 Aug 2015 19:28:15 +0530 Subject: [PATCH 1/2] Updated to Timber.Tree 1. Timber.HollowTree was deprecated 2. Added support for Appname --- .../tony19/timber/loggly/LogglyTree.java | 96 +++++++++++++++---- 1 file changed, 77 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java b/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java index 0cfae35..77381b9 100644 --- a/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java +++ b/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java @@ -24,13 +24,17 @@ /** * A Timber tree that posts * log messages to Loggly - * * @author tony19@gmail.com */ -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 { @@ -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() { + + } + + @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 } @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); + } /** @@ -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); } /** @@ -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); + } /** @@ -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); + } /** @@ -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("\"", "\\\"")); } /** @@ -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("\"", "\\\"")); } /** @@ -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) { loggly.log(toJson(level, message, t, args), handler); } @@ -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); } @@ -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); } } From d66b6c1d85d88aefdaaa218b4fb54dbec0db9db3 Mon Sep 17 00:00:00 2001 From: psquickitjayant Date: Mon, 31 Aug 2015 19:24:02 +0530 Subject: [PATCH 2/2] Updated code for tags --- .../tony19/timber/loggly/LogglyTree.java | 68 +++++++++---------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java b/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java index 77381b9..d3cf1a9 100644 --- a/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java +++ b/src/main/java/com/github/tony19/timber/loggly/LogglyTree.java @@ -30,11 +30,8 @@ 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; + private String appName=""; + /** Log severity level */ private enum Level { @@ -52,6 +49,7 @@ 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 @@ -68,7 +66,8 @@ public void failure(String error) { } public LogglyTree(String token, String tag) { - loggly = new LogglyClient(token, tag); + loggly = new LogglyClient(token); + tag(tag); // Setup an async callback // TODO: handle failed messages with N retries handler = new LogglyClient.Callback() {@Override @@ -85,7 +84,8 @@ public void failure(String error) { } public LogglyTree(String token, String tag, String appName) { - loggly = new LogglyClient(token, tag); + loggly = new LogglyClient(token); + tag(tag); this.appName = appName; // Setup an async callback // TODO: handle failed messages with N retries @@ -108,7 +108,7 @@ public void failure(String error) { */ @Override public void d(String message, Object... args) { - log(DEBUG, message, args); + log(Level.DEBUG, message, args); } /** @@ -119,7 +119,7 @@ public void d(String message, Object... args) { */ @Override public void d(Throwable t, String message, Object... args) { - log(DEBUG, message, t, args); + log(Level.DEBUG, message, t, args); } /** @@ -129,7 +129,7 @@ public void d(Throwable t, String message, Object... args) { */ @Override public void i(String message, Object... args) { - log(INFO, message, args); + log(Level.INFO, message, args); } /** @@ -140,7 +140,7 @@ public void i(String message, Object... args) { */ @Override public void i(Throwable t, String message, Object... args) { - log(INFO, message, t, args); + log(Level.INFO, message, t, args); } /** @@ -150,7 +150,7 @@ public void i(Throwable t, String message, Object... args) { */ @Override public void e(String message, Object... args) { - log(ERROR, message, args); + log(Level.ERROR, message, args); } @@ -162,7 +162,7 @@ public void e(String message, Object... args) { */ @Override public void e(Throwable t, String message, Object... args) { - log(ERROR, message, t, args); + log(Level.ERROR, message, t, args); } /** @@ -172,7 +172,7 @@ public void e(Throwable t, String message, Object... args) { */ @Override public void w(String message, Object... args) { - log(WARN, message, args); + log(Level.WARN, message, args); } @@ -184,8 +184,7 @@ public void w(String message, Object... args) { */ @Override public void w(Throwable t, String message, Object... args) { - log(WARN, message, t, args); - + log(Level.WARN, message, t, args); } /** @@ -198,12 +197,6 @@ public void w(Throwable t, String message, Object... args) { * */ - 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\",\"appName\" : \"%3$s\"}", level, @@ -236,12 +229,6 @@ private String toJson(Level level, String message, Throwable t, Object... args) 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("\"", "\\\"")); - } /** * Asynchronously sends a log event to Loggly @@ -250,7 +237,7 @@ private String toJson(int level, String message, Throwable t, Object... args) { * @param t throwable * @param args message formatting arguments */ - private void log(int level, String message, Throwable t, Object... args) { + private void log(Level level, String message, Throwable t, Object... args) { loggly.log(toJson(level, message, t, args), handler); } @@ -260,20 +247,27 @@ private void log(int level, String message, Throwable t, Object... args) { * @param message message to be logged * @param args message formatting arguments */ - private void log(int level, String message, Object... args) { + private void log(Level level, String message, Object... args) { loggly.log(toJson(level, message, args), handler); } /** - * 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) { - // TODO Auto-generated method stub - loggly.log(toJson(level, tag, message, t), handler); + 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); } }