Skip to content

Commit

Permalink
Add threadTagPrefix support.
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyTangAndroid committed Sep 5, 2022
1 parent d79c5f0 commit dbf44d0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/tonytangandroid/wood/sample/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tonytangandroid.wood.sample;

import static com.tonytangandroid.wood.sample.HomeActivity.logInBackground;

import android.app.Application;

public class App extends Application {
Expand All @@ -8,5 +10,6 @@ public class App extends Application {
public void onCreate() {
super.onCreate();
WoodIntegrationUtil.initWood(this);
logInBackground();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ private void generateTimberLog() {
logError();
}

public static void logInBackground() {
new Thread(() -> Timber.i("This is an INFO message triggered in background thread.")).start();
}

private void logError() {
try {
String shortSrc = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object WoodIntegrationUtil {
@JvmStatic
fun initWood(application: Application) {
Timber.plant(
WoodTree(application)
WoodTree(application,"tony")
.retainDataFor(WoodTree.Period.FOREVER)
.logLevel(Log.VERBOSE)
.autoScroll(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class WoodTree extends Timber.Tree {

public WoodTree(Context context) {}

public WoodTree(Context context, String threadTagPrefix) {}

public WoodTree showNotification(boolean sticky) {
return this;
}
Expand Down
34 changes: 23 additions & 11 deletions wood/src/main/java/com/tonytangandroid/wood/WoodTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Executor;
import timber.log.Timber;

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

/** @param context The current Context. */
public WoodTree(@NonNull Context context) {
executor = new JobExecutor();
this(context, "");
}

/**
* @param context context
* @param threadTagPrefix the extra prefix added on the logged message.
*/
public WoodTree(@NonNull Context context, String threadTagPrefix) {
this.threadTagPrefix = threadTagPrefix;
this.executor = new JobExecutor();
this.context = context.getApplicationContext();
woodDatabase = WoodDatabase.getInstance(context);
retentionManager = new RetentionManager(this.context, DEFAULT_RETENTION);
sharedPreferences = context.getSharedPreferences(PREF_WOOD_CONFIG, Context.MODE_PRIVATE);
this.woodDatabase = WoodDatabase.getInstance(context);
this.retentionManager = new RetentionManager(this.context, DEFAULT_RETENTION);
this.sharedPreferences = context.getSharedPreferences(PREF_WOOD_CONFIG, Context.MODE_PRIVATE);
}

public static boolean autoScroll(Context context) {
Expand Down Expand Up @@ -117,16 +128,16 @@ public WoodTree maxLength(int max) {
protected void log(
final int priority, final String tag, final @NonNull String message, final Throwable t) {
if (shouldBeLogged(priority, tag)) {
executor.execute(
new Runnable() {
@Override
public void run() {
doLog(priority, tag, message, t);
}
});
String assembledMessage = formatThreadTag(message, this.threadTagPrefix);
executor.execute(() -> doLog(priority, tag, assembledMessage, t));
}
}

private static String formatThreadTag(String message, String threadTagPrefix) {
return String.format(
Locale.US, "[%s#%s]:%s", threadTagPrefix, Thread.currentThread().getName(), message);
}

private boolean shouldBeLogged(int priority, String tag) {
if (priority < logLevel) {
return false;
Expand Down Expand Up @@ -188,6 +199,7 @@ public enum Period {

/** From https://stackoverflow.com/a/1149712/4068957 */
static class ErrorUtil {

public static String asString(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Expand Down

0 comments on commit dbf44d0

Please sign in to comment.