Skip to content

Commit dbf44d0

Browse files
Add threadTagPrefix support.
1 parent d79c5f0 commit dbf44d0

5 files changed

Lines changed: 33 additions & 12 deletions

File tree

app/src/main/java/com/tonytangandroid/wood/sample/App.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.tonytangandroid.wood.sample;
22

3+
import static com.tonytangandroid.wood.sample.HomeActivity.logInBackground;
4+
35
import android.app.Application;
46

57
public class App extends Application {
@@ -8,5 +10,6 @@ public class App extends Application {
810
public void onCreate() {
911
super.onCreate();
1012
WoodIntegrationUtil.initWood(this);
13+
logInBackground();
1114
}
1215
}

app/src/main/java/com/tonytangandroid/wood/sample/HomeActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ private void generateTimberLog() {
6060
logError();
6161
}
6262

63+
public static void logInBackground() {
64+
new Thread(() -> Timber.i("This is an INFO message triggered in background thread.")).start();
65+
}
66+
6367
private void logError() {
6468
try {
6569
String shortSrc = "";

app/src/main/java/com/tonytangandroid/wood/sample/WoodIntegrationUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object WoodIntegrationUtil {
1010
@JvmStatic
1111
fun initWood(application: Application) {
1212
Timber.plant(
13-
WoodTree(application)
13+
WoodTree(application,"tony")
1414
.retainDataFor(WoodTree.Period.FOREVER)
1515
.logLevel(Log.VERBOSE)
1616
.autoScroll(false)

wood-no-op/src/main/java/com/tonytangandroid/wood/WoodTree.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class WoodTree extends Timber.Tree {
1010

1111
public WoodTree(Context context) {}
1212

13+
public WoodTree(Context context, String threadTagPrefix) {}
14+
1315
public WoodTree showNotification(boolean sticky) {
1416
return this;
1517
}

wood/src/main/java/com/tonytangandroid/wood/WoodTree.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.StringWriter;
1010
import java.util.ArrayList;
1111
import java.util.List;
12+
import java.util.Locale;
1213
import java.util.concurrent.Executor;
1314
import timber.log.Timber;
1415

@@ -20,6 +21,7 @@ public class WoodTree extends Timber.DebugTree {
2021
@NonNull private final Context context;
2122
@NonNull private final WoodDatabase woodDatabase;
2223
private final Executor executor;
24+
private final String threadTagPrefix;
2325
@Nullable private NotificationHelper notificationHelper;
2426
@NonNull private RetentionManager retentionManager;
2527
private int maxContentLength = 250000;
@@ -30,11 +32,20 @@ public class WoodTree extends Timber.DebugTree {
3032

3133
/** @param context The current Context. */
3234
public WoodTree(@NonNull Context context) {
33-
executor = new JobExecutor();
35+
this(context, "");
36+
}
37+
38+
/**
39+
* @param context context
40+
* @param threadTagPrefix the extra prefix added on the logged message.
41+
*/
42+
public WoodTree(@NonNull Context context, String threadTagPrefix) {
43+
this.threadTagPrefix = threadTagPrefix;
44+
this.executor = new JobExecutor();
3445
this.context = context.getApplicationContext();
35-
woodDatabase = WoodDatabase.getInstance(context);
36-
retentionManager = new RetentionManager(this.context, DEFAULT_RETENTION);
37-
sharedPreferences = context.getSharedPreferences(PREF_WOOD_CONFIG, Context.MODE_PRIVATE);
46+
this.woodDatabase = WoodDatabase.getInstance(context);
47+
this.retentionManager = new RetentionManager(this.context, DEFAULT_RETENTION);
48+
this.sharedPreferences = context.getSharedPreferences(PREF_WOOD_CONFIG, Context.MODE_PRIVATE);
3849
}
3950

4051
public static boolean autoScroll(Context context) {
@@ -117,16 +128,16 @@ public WoodTree maxLength(int max) {
117128
protected void log(
118129
final int priority, final String tag, final @NonNull String message, final Throwable t) {
119130
if (shouldBeLogged(priority, tag)) {
120-
executor.execute(
121-
new Runnable() {
122-
@Override
123-
public void run() {
124-
doLog(priority, tag, message, t);
125-
}
126-
});
131+
String assembledMessage = formatThreadTag(message, this.threadTagPrefix);
132+
executor.execute(() -> doLog(priority, tag, assembledMessage, t));
127133
}
128134
}
129135

136+
private static String formatThreadTag(String message, String threadTagPrefix) {
137+
return String.format(
138+
Locale.US, "[%s#%s]:%s", threadTagPrefix, Thread.currentThread().getName(), message);
139+
}
140+
130141
private boolean shouldBeLogged(int priority, String tag) {
131142
if (priority < logLevel) {
132143
return false;
@@ -188,6 +199,7 @@ public enum Period {
188199

189200
/** From https://stackoverflow.com/a/1149712/4068957 */
190201
static class ErrorUtil {
202+
191203
public static String asString(Throwable throwable) {
192204
StringWriter sw = new StringWriter();
193205
PrintWriter pw = new PrintWriter(sw);

0 commit comments

Comments
 (0)