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
from
Open
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Updated code for tags

  • Loading branch information
varshneyjayant committed Aug 31, 2015
commit d66b6c1d85d88aefdaaa218b4fb54dbec0db9db3
@@ -30,11 +30,8 @@

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 @@
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);
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.