From dac3d2fc8ec6b09e45e5aeba6ff4c80c67a02032 Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Tue, 13 Aug 2024 22:37:29 +0200 Subject: [PATCH 01/12] Add custom logger --- .../library/BacktraceClientMetricsTest.java | 4 +- .../BacktraceClientSummedEventTest.java | 4 +- .../BacktraceClientUniqueEventTest.java | 4 +- .../library/metrics/BacktraceMetricsTest.java | 4 +- .../library/watchdog/BacktraceAnrTest.java | 6 +- .../library/logger/BacktraceLogLogger.java | 91 +++++++++++++++++++ .../library/logger/BacktraceLogger.java | 43 ++------- .../backtraceio/library/logger/Logger.java | 15 +++ 8 files changed, 124 insertions(+), 47 deletions(-) create mode 100644 backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java create mode 100644 backtrace-library/src/main/java/backtraceio/library/logger/Logger.java diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java index 4edd73f2..d16039eb 100644 --- a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java +++ b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java @@ -25,6 +25,7 @@ import backtraceio.library.common.BacktraceSerializeHelper; import backtraceio.library.events.EventsOnServerResponseEventListener; import backtraceio.library.events.EventsRequestHandler; +import backtraceio.library.logger.BacktraceLogLogger; import backtraceio.library.logger.BacktraceLogger; import backtraceio.library.logger.LogLevel; import backtraceio.library.models.BacktraceMetricsSettings; @@ -52,13 +53,12 @@ public class BacktraceClientMetricsTest { @Before public void setUp() { + BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG)); context = InstrumentationRegistry.getInstrumentation().getContext(); credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token); BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath()); backtraceClient = new BacktraceClient(context, credentials, database); - - BacktraceLogger.setLevel(LogLevel.DEBUG); } public class MockRequestHandler implements EventsRequestHandler { diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java index ab99b6fb..0d2c1c31 100644 --- a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java +++ b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java @@ -24,6 +24,7 @@ import backtraceio.library.common.BacktraceSerializeHelper; import backtraceio.library.events.EventsOnServerResponseEventListener; import backtraceio.library.events.EventsRequestHandler; +import backtraceio.library.logger.BacktraceLogLogger; import backtraceio.library.logger.BacktraceLogger; import backtraceio.library.logger.LogLevel; import backtraceio.library.models.BacktraceMetricsSettings; @@ -49,13 +50,12 @@ public class BacktraceClientSummedEventTest { @Before public void setUp() { + BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG)); context = InstrumentationRegistry.getInstrumentation().getContext(); credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token); BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath()); backtraceClient = new BacktraceClient(context, credentials, database); - - BacktraceLogger.setLevel(LogLevel.DEBUG); } public class MockRequestHandler implements EventsRequestHandler { diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java index f2606528..497f0d16 100644 --- a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java +++ b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java @@ -27,6 +27,7 @@ import backtraceio.library.common.BacktraceSerializeHelper; import backtraceio.library.events.EventsOnServerResponseEventListener; import backtraceio.library.events.EventsRequestHandler; +import backtraceio.library.logger.BacktraceLogLogger; import backtraceio.library.logger.BacktraceLogger; import backtraceio.library.logger.LogLevel; import backtraceio.library.models.BacktraceMetricsSettings; @@ -56,13 +57,12 @@ public class BacktraceClientUniqueEventTest { @Before public void setUp() { + BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG)); context = InstrumentationRegistry.getInstrumentation().getContext(); credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token); BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath()); backtraceClient = new BacktraceClient(context, credentials, database); - - BacktraceLogger.setLevel(LogLevel.DEBUG); } public class MockRequestHandler implements EventsRequestHandler { diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java index 8f07a2bc..3891dae2 100644 --- a/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java +++ b/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java @@ -20,6 +20,7 @@ import backtraceio.library.BacktraceCredentials; import backtraceio.library.BacktraceDatabase; import backtraceio.library.common.BacktraceTimeHelper; +import backtraceio.library.logger.BacktraceLogLogger; import backtraceio.library.logger.BacktraceLogger; import backtraceio.library.logger.LogLevel; import backtraceio.library.models.BacktraceMetricsSettings; @@ -45,13 +46,12 @@ public class BacktraceMetricsTest { @Before public void setUp() { + BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG)); context = InstrumentationRegistry.getInstrumentation().getContext(); credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token); BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath()); backtraceClient = new BacktraceClient(context, credentials, database); - - BacktraceLogger.setLevel(LogLevel.DEBUG); } @Test diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java index 0bf1f09e..7b47d35a 100644 --- a/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java +++ b/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java @@ -18,6 +18,7 @@ import backtraceio.library.BacktraceClient; import backtraceio.library.BacktraceCredentials; +import backtraceio.library.logger.BacktraceLogLogger; import backtraceio.library.logger.BacktraceLogger; import backtraceio.library.logger.LogLevel; @@ -29,6 +30,7 @@ public class BacktraceAnrTest { @Before public void setUp() { + BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG)); this.context = InstrumentationRegistry.getInstrumentation().getContext(); this.backtraceClient = new BacktraceClient(this.context, credentials); } @@ -38,7 +40,6 @@ public void setUp() { public void checkIfANRIsDetectedCorrectly() { // GIVEN final Waiter waiter = new Waiter(); - BacktraceLogger.setLevel(LogLevel.DEBUG); BacktraceANRWatchdog watchdog = new BacktraceANRWatchdog(this.backtraceClient, 500); watchdog.setOnApplicationNotRespondingEvent(new OnApplicationNotRespondingEvent() { @Override @@ -67,7 +68,6 @@ public void onEvent(BacktraceWatchdogTimeoutException exception) { public void checkIfANRIsDetectedCorrectlyWithBacktraceClient() { // GIVEN final Waiter waiter = new Waiter(); - BacktraceLogger.setLevel(LogLevel.DEBUG); this.backtraceClient.enableAnr(500, new OnApplicationNotRespondingEvent() { @Override public void onEvent(BacktraceWatchdogTimeoutException exception) { @@ -96,7 +96,6 @@ public void checkIfANRIsNotDetected() { // GIVEN final int numberOfIterations = 5; final Waiter waiter = new Waiter(); - BacktraceLogger.setLevel(LogLevel.DEBUG); BacktraceANRWatchdog watchdog = new BacktraceANRWatchdog(this.backtraceClient, 5000); watchdog.setOnApplicationNotRespondingEvent(new OnApplicationNotRespondingEvent() { @Override @@ -128,7 +127,6 @@ public void onEvent(BacktraceWatchdogTimeoutException exception) { public void checkIsDisableWorks() { // GIVEN final Waiter waiter = new Waiter(); - BacktraceLogger.setLevel(LogLevel.DEBUG); backtraceClient.enableAnr(1000, new OnApplicationNotRespondingEvent() { @Override diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java new file mode 100644 index 00000000..1b66f187 --- /dev/null +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java @@ -0,0 +1,91 @@ +package backtraceio.library.logger; + +import android.util.Log; + +/** + * TODO: improve Backtrace Logger implementation class for logging messages from inside library + */ +public class BacktraceLogLogger implements Logger { + + private static final String BASE_TAG = "BacktraceLogger: "; + /** + * Level from which all information is logged + */ + private LogLevel logLevel; + + + public BacktraceLogLogger() { + this(LogLevel.OFF); + } + + public BacktraceLogLogger(LogLevel logLevel) { + this.logLevel = logLevel; + } + /** + * set logging level from which all messages should be logged to the console + * + * @param level login level + */ + public void setLevel(LogLevel level) { + this.logLevel = level; + } + + /** + * @param tag source of logs, usually identifies the class or activity + * @param message text information which should be logged + * @return the number of bytes written + */ + public int d(String tag, String message) { + if (this.logLevel.ordinal() <= LogLevel.DEBUG.ordinal()) { + return Log.d(this.getTag(tag), message); + } + return 0; + } + + /** + * Log messages that suggest something unexpected or rare has happened, which isn't an error. + * + * @param tag source of logs, usually identifies the class or activity + * @param message text information which should be logged + * @return the number of bytes written + */ + public int w(String tag, String message) { + if (this.logLevel.ordinal() <= LogLevel.WARN.ordinal()) { + return Log.w(this.getTag(tag), message); + } + return 0; + } + + /** + * Log messages that suggest error or something that should not happen + * + * @param tag source of logs, usually identifies the class or activity + * @param message text information which should be logged + * @return the number of bytes written + */ + public int e(String tag, String message) { + if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) { + return Log.e(this.getTag(tag), message); + } + return 0; + } + + /** + * Log messages that suggest error or something that should not happen + * + * @param tag source of logs, usually identifies the class or activity + * @param message text information which should be logged + * @param tr an exception to log + * @return the number of bytes written + */ + public int e(String tag, String message, Throwable tr) { + if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) { + return Log.e(this.getTag(tag), message, tr); + } + return 0; + } + + private String getTag(String tag) { + return BacktraceLogLogger.BASE_TAG + tag; + } +} \ No newline at end of file diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java index 401832ed..82afd178 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java @@ -1,25 +1,14 @@ package backtraceio.library.logger; -import android.util.Log; - /** - * Backtrace Logger class for logging messages from inside library + * TODO: improve Backtrace Logger class for logging messages from inside library */ public class BacktraceLogger { - private static final String BASE_TAG = "BacktraceLogger: "; - /** - * Level from which all information is logged - */ - private static LogLevel logLevel = LogLevel.OFF; + private static Logger logger = new BacktraceLogLogger(); - /** - * set logging level from which all messages should be logged to the console - * - * @param level login level - */ - public static void setLevel(LogLevel level) { - BacktraceLogger.logLevel = level; + public static void setLogger(Logger logger) { + BacktraceLogger.logger = logger; } /** @@ -28,10 +17,7 @@ public static void setLevel(LogLevel level) { * @return the number of bytes written */ public static int d(String tag, String message) { - if (BacktraceLogger.logLevel.ordinal() <= LogLevel.DEBUG.ordinal()) { - return Log.d(getTag(tag), message); - } - return 0; + return logger.d(tag, message); } /** @@ -42,10 +28,7 @@ public static int d(String tag, String message) { * @return the number of bytes written */ public static int w(String tag, String message) { - if (BacktraceLogger.logLevel.ordinal() <= LogLevel.WARN.ordinal()) { - return Log.w(getTag(tag), message); - } - return 0; + return logger.w(tag, message); } /** @@ -56,10 +39,7 @@ public static int w(String tag, String message) { * @return the number of bytes written */ public static int e(String tag, String message) { - if (BacktraceLogger.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) { - return Log.e(getTag(tag), message); - } - return 0; + return logger.e(tag, message); } /** @@ -71,13 +51,6 @@ public static int e(String tag, String message) { * @return the number of bytes written */ public static int e(String tag, String message, Throwable tr) { - if (BacktraceLogger.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) { - return Log.e(getTag(tag), message, tr); - } - return 0; - } - - private static String getTag(String tag) { - return BacktraceLogger.BASE_TAG + tag; + return logger.e(tag, message, tr); } } \ No newline at end of file diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java b/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java new file mode 100644 index 00000000..4c02086c --- /dev/null +++ b/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java @@ -0,0 +1,15 @@ +package backtraceio.library.logger; + +/** + * TODO: improve Backtrace Logger class for logging messages from inside library + */ +public interface Logger { + + int d(String tag, String message); + + int w(String tag, String message); + + int e(String tag, String message); + + int e(String tag, String message, Throwable tr); +} \ No newline at end of file From 1cf21c98c8738dcf7a42eca155e172a4cf84587d Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Wed, 14 Aug 2024 21:52:50 +0200 Subject: [PATCH 02/12] Improve docs --- .../library/logger/BacktraceLogLogger.java | 8 ++++++-- .../backtraceio/library/logger/BacktraceLogger.java | 13 ++++++++++++- .../java/backtraceio/library/logger/LogLevel.java | 10 +++++++++- .../java/backtraceio/library/logger/Logger.java | 11 ++++++----- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java index 1b66f187..fefbd13d 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java @@ -3,7 +3,12 @@ import android.util.Log; /** - * TODO: improve Backtrace Logger implementation class for logging messages from inside library + * Backtrace Logger implementation class for logging messages within the Backtrace library. + *
+ * This class leverages the {@link android.util.Log} package to output log messages. + * It provides a standardized way to log information, warnings, errors, and debug messages + * from within the library, making it easier to track and diagnose issues. + *
*/ public class BacktraceLogLogger implements Logger { @@ -13,7 +18,6 @@ public class BacktraceLogLogger implements Logger { */ private LogLevel logLevel; - public BacktraceLogLogger() { this(LogLevel.OFF); } diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java index 82afd178..f0be3316 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java @@ -1,7 +1,18 @@ package backtraceio.library.logger; /** - * TODO: improve Backtrace Logger class for logging messages from inside library + * A global logging utility for the Backtrace library. + *+ * {@code BacktraceLogger} acts as a wrapper around a {@link Logger} implementation, + * providing a centralized logging mechanism throughout the library. By default, it uses + * the {@link BacktraceLogLogger}, which relies on {@code android.util.Log} for logging. + * However, the logger can be replaced with a custom implementation by using the + * {@link #setLogger(Logger)} method. + *
+ *+ * This allows for flexibility in logging strategies, enabling developers to integrate + * their preferred logging framework or customize log handling as needed. + *
*/ public class BacktraceLogger { diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/LogLevel.java b/backtrace-library/src/main/java/backtraceio/library/logger/LogLevel.java index 4fceeb79..0d012e85 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/LogLevel.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/LogLevel.java @@ -1,7 +1,15 @@ package backtraceio.library.logger; /** - * The class is intended to determine the available levels of login messages + * Defines the logging levels for the Backtrace library. + *+ * Controls the severity of log messages: + *
+ * Defines methods for logging at various levels, such as debug, warning, and error. + * Implementing classes will provide the actual logging functionality, ensuring consistent + * message logging across the library. + *
*/ public interface Logger { - int d(String tag, String message); - int w(String tag, String message); - int e(String tag, String message); - int e(String tag, String message, Throwable tr); } \ No newline at end of file From c939f84855c6f1f0272bb3e7ee3e0552cd839489 Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Wed, 14 Aug 2024 22:20:43 +0200 Subject: [PATCH 03/12] Improve doc --- .../src/main/java/backtraceio/library/logger/Logger.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java b/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java index 6a4d044b..2d2e24c5 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java @@ -1,7 +1,7 @@ package backtraceio.library.logger; /** - * Interface for logging messages in the Backtrace library. + * Interface for Backtrace Android library used for logging internal library messages. *
* Defines methods for logging at various levels, such as debug, warning, and error.
* Implementing classes will provide the actual logging functionality, ensuring consistent
From e357246943a08958d626339cd946c89f09b0efaa Mon Sep 17 00:00:00 2001
From: Bartosz Litwiniuk <>
Date: Tue, 20 Aug 2024 22:31:39 +0200
Subject: [PATCH 04/12] Add temp tests
---
backtrace-library/build.gradle | 2 +
.../logger/BacktraceLogLoggerTest.java | 128 ++++++++++++++++++
2 files changed, 130 insertions(+)
create mode 100644 backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java
diff --git a/backtrace-library/build.gradle b/backtrace-library/build.gradle
index 104c51e4..9448c8aa 100644
--- a/backtrace-library/build.gradle
+++ b/backtrace-library/build.gradle
@@ -81,6 +81,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.squareup:tape:1.2.3'
testImplementation 'junit:junit:4.13.2'
+ testImplementation 'org.mockito:mockito-core:5.2.0'
+ testImplementation "org.mockito:mockito-android:2.28.2"
androidTestImplementation 'net.jodah:concurrentunit:0.4.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test:rules:1.5.0'
diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java
new file mode 100644
index 00000000..8b60c577
--- /dev/null
+++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java
@@ -0,0 +1,128 @@
+package backtraceio.library.logger;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.when;
+
+import android.util.Log;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+public class BacktraceLogLoggerTest {
+
+ @Mock
+ private Log mockLog;
+
+ private BacktraceLogLogger logger;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ // We need to mock the Log class's static methods
+ mockStatic(Log.class);
+ }
+
+ @Test
+ public void testDebugLog() {
+ try (MockedStatic
@@ -22,7 +24,7 @@ public BacktraceLogLogger() {
this(LogLevel.OFF);
}
- public BacktraceLogLogger(LogLevel logLevel) {
+ public BacktraceLogLogger(@NotNull LogLevel logLevel) {
this.logLevel = logLevel.ordinal();
}
@@ -31,7 +33,7 @@ public BacktraceLogLogger(LogLevel logLevel) {
*
* @param level login level
*/
- public void setLevel(LogLevel level) {
+ public void setLevel(@NotNull LogLevel level) {
this.logLevel = level.ordinal();
}
diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java
index 920911eb..b6a87642 100644
--- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java
+++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java
@@ -1,5 +1,7 @@
package backtraceio.library.logger;
+import org.jetbrains.annotations.NotNull;
+
/**
* A global logging utility for the Backtrace library.
*
@@ -22,7 +24,7 @@ public static Logger getLogger() {
return logger;
}
- public static void setLogger(Logger logger) {
+ public static void setLogger(@NotNull Logger logger) {
BacktraceLogger.logger = logger;
}
@@ -79,7 +81,7 @@ public static int e(String tag, String message, Throwable tr) {
*
*/
@Deprecated
- public static void setLevel(LogLevel level) {
+ public static void setLevel(@NotNull LogLevel level) {
logger.setLevel(level);
}
}
\ No newline at end of file
From fb65e8cd27c1a6ce72c2bd7fd7883896aeb619ed Mon Sep 17 00:00:00 2001
From: Bartosz Litwiniuk <>
Date: Tue, 3 Sep 2024 22:45:06 +0200
Subject: [PATCH 10/12] Add extra null-check
---
.../java/backtraceio/library/logger/BacktraceLogger.java | 3 +++
.../java/backtraceio/library/logger/BacktraceLoggerTest.java | 5 +++++
2 files changed, 8 insertions(+)
diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java
index b6a87642..0fedfa89 100644
--- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java
+++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java
@@ -25,6 +25,9 @@ public static Logger getLogger() {
}
public static void setLogger(@NotNull Logger logger) {
+ if (logger == null) {
+ throw new IllegalArgumentException("Passed custom logger implementation can`t be null");
+ }
BacktraceLogger.logger = logger;
}
diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java
index eb5490fc..e53b2031 100644
--- a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java
+++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java
@@ -28,4 +28,9 @@ public void testCustomLogger() {
Assert.assertEquals(BacktraceMockLogger.MOCK_VALUE, errorResult);
Assert.assertEquals(BacktraceMockLogger.MOCK_VALUE, errorResult2);
}
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNullCustomLogger() {
+ BacktraceLogger.setLogger(null);
+ }
}
From 2dc10617c4d8c1d2acbe85b4c69d643336c1c149 Mon Sep 17 00:00:00 2001
From: Bartosz Litwiniuk <>
Date: Wed, 4 Sep 2024 23:27:49 +0200
Subject: [PATCH 11/12] Rename logger class, fix setting log level
---
README.md | 6 ++++++
.../library/BacktraceClientMetricsTest.java | 4 ++--
.../library/BacktraceClientSummedEventTest.java | 4 ++--
.../library/BacktraceClientUniqueEventTest.java | 4 ++--
.../library/metrics/BacktraceMetricsTest.java | 4 ++--
.../library/watchdog/BacktraceAnrTest.java | 4 ++--
...eLogLogger.java => BacktraceInternalLogger.java} | 13 +++++++++----
.../backtraceio/library/logger/BacktraceLogger.java | 8 +++++---
.../java/backtraceio/library/logger/Logger.java | 1 -
...erTest.java => BacktraceInternalLoggerTest.java} | 6 +++---
.../library/logger/BacktraceLoggerTest.java | 12 ++++++++++++
.../library/logger/BacktraceMockLogger.java | 2 --
12 files changed, 45 insertions(+), 23 deletions(-)
rename backtrace-library/src/main/java/backtraceio/library/logger/{BacktraceLogLogger.java => BacktraceInternalLogger.java} (91%)
rename backtrace-library/src/test/java/backtraceio/library/logger/{BacktraceLogLoggerTest.java => BacktraceInternalLoggerTest.java} (96%)
diff --git a/README.md b/README.md
index d3096d31..caa30d7d 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,9 @@
+You can replace internal BacktraceLogger with your custom implementation using code below.
+```java
+BacktraceLogger.setLogger(customLoggerInstance);
+```
+Your custom logger implementation has to implement [Logger](https://github.com/backtrace-labs/backtrace-android/blob/master/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java) interface.
+
# Backtrace Integration with Android
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.backtrace-labs.backtrace-android/backtrace-library/badge.svg)](https://search.maven.org/artifact/com.github.backtrace-labs.backtrace-android/backtrace-library)
diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java
index d16039eb..20a8d6c3 100644
--- a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java
+++ b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientMetricsTest.java
@@ -25,7 +25,7 @@
import backtraceio.library.common.BacktraceSerializeHelper;
import backtraceio.library.events.EventsOnServerResponseEventListener;
import backtraceio.library.events.EventsRequestHandler;
-import backtraceio.library.logger.BacktraceLogLogger;
+import backtraceio.library.logger.BacktraceInternalLogger;
import backtraceio.library.logger.BacktraceLogger;
import backtraceio.library.logger.LogLevel;
import backtraceio.library.models.BacktraceMetricsSettings;
@@ -53,7 +53,7 @@ public class BacktraceClientMetricsTest {
@Before
public void setUp() {
- BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG));
+ BacktraceLogger.setLogger(new BacktraceInternalLogger(LogLevel.DEBUG));
context = InstrumentationRegistry.getInstrumentation().getContext();
credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token);
BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath());
diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java
index 0d2c1c31..cab05d7a 100644
--- a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java
+++ b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientSummedEventTest.java
@@ -24,7 +24,7 @@
import backtraceio.library.common.BacktraceSerializeHelper;
import backtraceio.library.events.EventsOnServerResponseEventListener;
import backtraceio.library.events.EventsRequestHandler;
-import backtraceio.library.logger.BacktraceLogLogger;
+import backtraceio.library.logger.BacktraceInternalLogger;
import backtraceio.library.logger.BacktraceLogger;
import backtraceio.library.logger.LogLevel;
import backtraceio.library.models.BacktraceMetricsSettings;
@@ -50,7 +50,7 @@ public class BacktraceClientSummedEventTest {
@Before
public void setUp() {
- BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG));
+ BacktraceLogger.setLogger(new BacktraceInternalLogger(LogLevel.DEBUG));
context = InstrumentationRegistry.getInstrumentation().getContext();
credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token);
BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath());
diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java
index 497f0d16..af44bff8 100644
--- a/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java
+++ b/backtrace-library/src/androidTest/java/backtraceio/library/BacktraceClientUniqueEventTest.java
@@ -27,7 +27,7 @@
import backtraceio.library.common.BacktraceSerializeHelper;
import backtraceio.library.events.EventsOnServerResponseEventListener;
import backtraceio.library.events.EventsRequestHandler;
-import backtraceio.library.logger.BacktraceLogLogger;
+import backtraceio.library.logger.BacktraceInternalLogger;
import backtraceio.library.logger.BacktraceLogger;
import backtraceio.library.logger.LogLevel;
import backtraceio.library.models.BacktraceMetricsSettings;
@@ -57,7 +57,7 @@ public class BacktraceClientUniqueEventTest {
@Before
public void setUp() {
- BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG));
+ BacktraceLogger.setLogger(new BacktraceInternalLogger(LogLevel.DEBUG));
context = InstrumentationRegistry.getInstrumentation().getContext();
credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token);
BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath());
diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java
index 3891dae2..f7e46145 100644
--- a/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java
+++ b/backtrace-library/src/androidTest/java/backtraceio/library/metrics/BacktraceMetricsTest.java
@@ -20,7 +20,7 @@
import backtraceio.library.BacktraceCredentials;
import backtraceio.library.BacktraceDatabase;
import backtraceio.library.common.BacktraceTimeHelper;
-import backtraceio.library.logger.BacktraceLogLogger;
+import backtraceio.library.logger.BacktraceInternalLogger;
import backtraceio.library.logger.BacktraceLogger;
import backtraceio.library.logger.LogLevel;
import backtraceio.library.models.BacktraceMetricsSettings;
@@ -46,7 +46,7 @@ public class BacktraceMetricsTest {
@Before
public void setUp() {
- BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG));
+ BacktraceLogger.setLogger(new BacktraceInternalLogger(LogLevel.DEBUG));
context = InstrumentationRegistry.getInstrumentation().getContext();
credentials = new BacktraceCredentials("https://universe.sp.backtrace.io:6098", token);
BacktraceDatabase database = new BacktraceDatabase(context, context.getFilesDir().getAbsolutePath());
diff --git a/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java b/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java
index 7b47d35a..7cd1ff2c 100644
--- a/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java
+++ b/backtrace-library/src/androidTest/java/backtraceio/library/watchdog/BacktraceAnrTest.java
@@ -18,7 +18,7 @@
import backtraceio.library.BacktraceClient;
import backtraceio.library.BacktraceCredentials;
-import backtraceio.library.logger.BacktraceLogLogger;
+import backtraceio.library.logger.BacktraceInternalLogger;
import backtraceio.library.logger.BacktraceLogger;
import backtraceio.library.logger.LogLevel;
@@ -30,7 +30,7 @@ public class BacktraceAnrTest {
@Before
public void setUp() {
- BacktraceLogger.setLogger(new BacktraceLogLogger(LogLevel.DEBUG));
+ BacktraceLogger.setLogger(new BacktraceInternalLogger(LogLevel.DEBUG));
this.context = InstrumentationRegistry.getInstrumentation().getContext();
this.backtraceClient = new BacktraceClient(this.context, credentials);
}
diff --git a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceInternalLogger.java
similarity index 91%
rename from backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java
rename to backtrace-library/src/main/java/backtraceio/library/logger/BacktraceInternalLogger.java
index 30e35511..7d2ef1af 100644
--- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java
+++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceInternalLogger.java
@@ -12,22 +12,27 @@
* from within the library, making it easier to track and diagnose issues.
*
* {@code BacktraceLogger} acts as a wrapper around a {@link Logger} implementation,
* providing a centralized logging mechanism throughout the library. By default, it uses
- * the {@link BacktraceLogLogger}, which relies on {@code android.util.Log} for logging.
+ * the {@link BacktraceInternalLogger}, which relies on {@code android.util.Log} for logging.
* However, the logger can be replaced with a custom implementation by using the
* {@link #setLogger(Logger)} method.
*