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

Add getDefaultTag() so a Tree can have its own custom tag generation logic #208

Open
wants to merge 5 commits into
base: trunk
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
18 changes: 12 additions & 6 deletions timber/src/main/java/timber/log/Timber.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,20 @@ String getTag() {
String tag = explicitTag.get();
if (tag != null) {
explicitTag.remove();
} else {
tag = getDefaultTag();
}
return tag;
}

/**
* If no tag is explicitly set, then the tree will fall back to this tag.
* @return The default tag for this tree
*/
@Nullable protected String getDefaultTag() {
return null;
}

/** Log a verbose message with optional format args. */
public void v(String message, Object... args) {
prepareLog(Log.VERBOSE, null, message, args);
Expand Down Expand Up @@ -587,7 +597,7 @@ protected abstract void log(int priority, @Nullable String tag, @NotNull String
public static class DebugTree extends Tree {
private static final int MAX_LOG_LENGTH = 4000;
private static final int MAX_TAG_LENGTH = 23;
private static final int CALL_STACK_INDEX = 5;
private static final int CALL_STACK_INDEX = 6;
private static final Pattern ANONYMOUS_CLASS = Pattern.compile("(\\$\\d+)+$");

/**
Expand All @@ -612,11 +622,7 @@ protected String createStackElementTag(@NotNull StackTraceElement element) {
return tag.substring(0, MAX_TAG_LENGTH);
}

@Override final String getTag() {
String tag = super.getTag();
if (tag != null) {
return tag;
}
@Override protected String getDefaultTag() {

// DO NOT switch this to Thread.getCurrentThread().getStackTrace(). The test will pass
// because Robolectric runs them on the JVM but on Android the elements are different.
Expand Down
48 changes: 46 additions & 2 deletions timber/src/test/java/timber/log/TimberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,55 @@ protected String formatMessage(@NotNull String message, @NotNull Object[] args)
.hasDebugMessage("TimberTest", "Test formatting: Test message logged. 100");
}


@Test public void logsWithCustomTag() {
Timber.plant(new Timber.DebugTree() {
@Override
protected String getDefaultTag() {
return "CUSTOMTAG";
}
});

Timber.d("Test with custom tag");
assertLog().hasDebugMessage("CUSTOMTAG", "Test with custom tag");

}

@Test public void logsWithCustomTagOverridden() {
Timber.plant(new Timber.DebugTree() {
@Override protected String getDefaultTag() {
return "CUSTOMTAG";
}
});

Timber.tag("NewTag").d("Tag manually set");

assertLog().hasDebugMessage("NewTag", "Tag manually set");
}

@Test public void logsWithMultipleTreesMultipleTags() {
Timber.plant(new Timber.DebugTree() {
@Override protected String getDefaultTag() {
return "CUSTOMTAG";
}
});

Timber.plant(new Timber.DebugTree() {
@Override protected String getDefaultTag() {
return "DIFFERENTTAG";
}
});

Timber.d("multiple tags");

assertLog().hasDebugMessage("CUSTOMTAG", "multiple tags")
.hasDebugMessage("DIFFERENTTAG", "multiple tags");
}

@Test public void nullArgumentObjectArray() {
Timber.plant(new Timber.DebugTree());
Timber.v("Test", (Object[]) null);
assertLog()
.hasVerboseMessage("TimberTest", "Test")
assertLog().hasVerboseMessage("TimberTest", "Test")
.hasNoMoreMessages();
}

Expand Down