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: + *

*/ public enum LogLevel { /** 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 4c02086c..6a4d044b 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java @@ -1,15 +1,16 @@ package backtraceio.library.logger; /** - * TODO: improve Backtrace Logger class for logging messages from inside library + * Interface for logging messages in the Backtrace library. + *

+ * 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 dummyStatic = Mockito.mockStatic(BacktraceLogLogger.class)) { + dummyStatic.when(BacktraceLogLogger::d) + .thenReturn(1); + // when + System.out.println(SomePublicClass.myPublicStaticFunc()); + //then + dummyStatic.verify( + SomePublicClass::myPublicStaticFunc, times(1)); + } + + logger = new BacktraceLogLogger(LogLevel.DEBUG); + String tag = "TestTag"; + String message = "Debug message"; + + when(Log.d(anyString(), anyString())).thenReturn(10); + + int result = logger.d(tag, message); + + // Verify that Log.d was called + verifyStatic(Log.class); + Log.d(eq("BacktraceLogger: " + tag), eq(message)); + + assertEquals(10, result); + } + + @Test + public void testWarningLog() { + logger = new BacktraceLogLogger(LogLevel.WARN); + String tag = "TestTag"; + String message = "Warning message"; + + when(Log.w(anyString(), anyString())).thenReturn(10); + + int result = logger.w(tag, message); + + // Verify that Log.w was called + verifyStatic(Log.class); + Log.w(eq("BacktraceLogger: " + tag), eq(message)); + + assertEquals(10, result); + } + + @Test + public void testErrorLog() { + logger = new BacktraceLogLogger(LogLevel.ERROR); + String tag = "TestTag"; + String message = "Error message"; + + when(Log.e(anyString(), anyString())).thenReturn(10); + + int result = logger.e(tag, message); + + // Verify that Log.e was called + verifyStatic(Log.class); + Log.e(eq("BacktraceLogger: " + tag), eq(message)); + + assertEquals(10, result); + } + + @Test + public void testErrorLogWithThrowable() { + logger = new BacktraceLogLogger(LogLevel.ERROR); + String tag = "TestTag"; + String message = "Error message"; + Throwable tr = new Throwable("Test Exception"); + + when(Log.e(anyString(), anyString(), any(Throwable.class))).thenReturn(10); + + int result = logger.e(tag, message, tr); + + // Verify that Log.e was called with throwable + verifyStatic(Log.class); + Log.e(eq("BacktraceLogger: " + tag), eq(message), eq(tr)); + + assertEquals(10, result); + } + + @Test + public void testLogLevelFiltering() { + logger = new BacktraceLogLogger(LogLevel.WARN); + String tag = "TestTag"; + String message = "Debug message"; + + int result = logger.d(tag, message); + + // Verify that Log.d was not called due to log level filtering + verifyStatic(Log.class, never()); + Log.d(anyString(), anyString()); + + assertEquals(0, result); + } +} \ No newline at end of file From a21ed1f9cd5c5fec43d3930c103c7d9598d88e78 Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Wed, 21 Aug 2024 18:42:10 +0200 Subject: [PATCH 05/12] Add BacktraceLogLogger tests --- backtrace-library/build.gradle | 4 +- .../logger/BacktraceLogLoggerTest.java | 197 ++++++++++-------- 2 files changed, 107 insertions(+), 94 deletions(-) diff --git a/backtrace-library/build.gradle b/backtrace-library/build.gradle index 9ec23bc6..06ba03ce 100644 --- a/backtrace-library/build.gradle +++ b/backtrace-library/build.gradle @@ -81,8 +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" + testImplementation "org.mockito:mockito-core:5.12.0" + androidTestImplementation "org.mockito:mockito-android:5.12.0" androidTestImplementation 'net.jodah:concurrentunit:0.4.4' androidTestImplementation 'androidx.test.ext:junit:1.2.1' androidTestImplementation 'androidx.test:rules:1.6.1' diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java index 8b60c577..43362227 100644 --- a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java +++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java @@ -1,128 +1,141 @@ 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.AfterClass; +import org.junit.BeforeClass; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; public class BacktraceLogLoggerTest { + private final int LOG_ENABLED = 12345; + private final int LOG_DISABLED = 0; - @Mock - private Log mockLog; + private final String TEST_TAG = "TEST-TAG"; + private final String TEST_MSG = "TEST-MSG"; + + public class LogMockAnswer implements Answer { + @Override + public Integer answer(InvocationOnMock invocation) { return LOG_ENABLED; } + } + @Mock + private static MockedStatic mockLog; private BacktraceLogLogger logger; + @BeforeClass + public static void init() { + mockLog = mockStatic(Log.class); + } + + @AfterClass + public static void close() { + mockLog.close(); + } + @Before public void setUp() { - MockitoAnnotations.initMocks(this); - // We need to mock the Log class's static methods - mockStatic(Log.class); + logger = new BacktraceLogLogger(); + mockLogMethods(); } - @Test - public void testDebugLog() { - try (MockedStatic dummyStatic = Mockito.mockStatic(BacktraceLogLogger.class)) { - dummyStatic.when(BacktraceLogLogger::d) - .thenReturn(1); - // when - System.out.println(SomePublicClass.myPublicStaticFunc()); - //then - dummyStatic.verify( - SomePublicClass::myPublicStaticFunc, times(1)); + private void mockLogMethods() { + try { + mockLog.when(() -> Log.d(Mockito.any(), Mockito.any())).thenAnswer( + new LogMockAnswer() + ); + mockLog.when(() -> Log.w(Mockito.any(), Mockito.anyString())).thenAnswer( + new LogMockAnswer() + ); + mockLog.when(() -> Log.e(Mockito.any(), Mockito.any())).thenAnswer( + new LogMockAnswer() + ); + mockLog.when(() -> Log.e(Mockito.any(), Mockito.any(), Mockito.any())).thenAnswer( + new LogMockAnswer() + ); + } + catch (Exception e) { + Assert.fail(e.toString()); } - - logger = new BacktraceLogLogger(LogLevel.DEBUG); - String tag = "TestTag"; - String message = "Debug message"; - - when(Log.d(anyString(), anyString())).thenReturn(10); - - int result = logger.d(tag, message); - - // Verify that Log.d was called - verifyStatic(Log.class); - Log.d(eq("BacktraceLogger: " + tag), eq(message)); - - assertEquals(10, result); } @Test - public void testWarningLog() { - logger = new BacktraceLogLogger(LogLevel.WARN); - String tag = "TestTag"; - String message = "Warning message"; - - when(Log.w(anyString(), anyString())).thenReturn(10); - - int result = logger.w(tag, message); - - // Verify that Log.w was called - verifyStatic(Log.class); - Log.w(eq("BacktraceLogger: " + tag), eq(message)); - - assertEquals(10, result); + public void testDebugLog() { + // GIVEN + logger.setLevel(LogLevel.DEBUG); + + // WHEN + final int debugResult = logger.d(TEST_TAG, TEST_MSG); + final int warnResult = logger.w(TEST_TAG, TEST_MSG); + final int errorResult = logger.e(TEST_TAG, TEST_MSG); + final int errorResult2 = logger.e(TEST_TAG, TEST_MSG, new Exception()); + + // THEN + Assert.assertEquals(LOG_ENABLED, debugResult); + Assert.assertEquals(LOG_ENABLED, warnResult); + Assert.assertEquals(LOG_ENABLED, errorResult); + Assert.assertEquals(LOG_ENABLED, errorResult2); } @Test - public void testErrorLog() { - logger = new BacktraceLogLogger(LogLevel.ERROR); - String tag = "TestTag"; - String message = "Error message"; - - when(Log.e(anyString(), anyString())).thenReturn(10); - - int result = logger.e(tag, message); - - // Verify that Log.e was called - verifyStatic(Log.class); - Log.e(eq("BacktraceLogger: " + tag), eq(message)); - - assertEquals(10, result); + public void testWarnLog() { + // GIVEN + logger.setLevel(LogLevel.WARN); + + // WHEN + final int debugResult = logger.d(TEST_TAG, TEST_MSG); + final int warnResult = logger.w(TEST_TAG, TEST_MSG); + final int errorResult = logger.e(TEST_TAG, TEST_MSG); + final int errorResult2 = logger.e(TEST_TAG, TEST_MSG, new Exception()); + + // THEN + Assert.assertEquals(LOG_DISABLED, debugResult); + Assert.assertEquals(LOG_ENABLED, warnResult); + Assert.assertEquals(LOG_ENABLED, errorResult); + Assert.assertEquals(LOG_ENABLED, errorResult2); } @Test - public void testErrorLogWithThrowable() { - logger = new BacktraceLogLogger(LogLevel.ERROR); - String tag = "TestTag"; - String message = "Error message"; - Throwable tr = new Throwable("Test Exception"); - - when(Log.e(anyString(), anyString(), any(Throwable.class))).thenReturn(10); - - int result = logger.e(tag, message, tr); - - // Verify that Log.e was called with throwable - verifyStatic(Log.class); - Log.e(eq("BacktraceLogger: " + tag), eq(message), eq(tr)); - - assertEquals(10, result); + public void testErrorLog() { + // GIVEN + logger.setLevel(LogLevel.ERROR); + + // WHEN + final int debugResult = logger.d(TEST_TAG, TEST_MSG); + final int warnResult = logger.w(TEST_TAG, TEST_MSG); + final int errorResult = logger.e(TEST_TAG, TEST_MSG); + final int errorResult2 = logger.e(TEST_TAG, TEST_MSG, new Exception()); + + // THEN + Assert.assertEquals(LOG_DISABLED, debugResult); + Assert.assertEquals(LOG_DISABLED, warnResult); + Assert.assertEquals(LOG_ENABLED, errorResult); + Assert.assertEquals(LOG_ENABLED, errorResult2); } @Test - public void testLogLevelFiltering() { - logger = new BacktraceLogLogger(LogLevel.WARN); - String tag = "TestTag"; - String message = "Debug message"; - - int result = logger.d(tag, message); - - // Verify that Log.d was not called due to log level filtering - verifyStatic(Log.class, never()); - Log.d(anyString(), anyString()); - - assertEquals(0, result); + public void testLogOff() { + // GIVEN + logger.setLevel(LogLevel.OFF); + + // WHEN + final int debugResult = logger.d(TEST_TAG, TEST_MSG); + final int warnResult = logger.w(TEST_TAG, TEST_MSG); + final int errorResult = logger.e(TEST_TAG, TEST_MSG); + final int errorResult2 = logger.e(TEST_TAG, TEST_MSG, new Exception()); + + // THEN + Assert.assertEquals(LOG_DISABLED, debugResult); + Assert.assertEquals(LOG_DISABLED, warnResult); + Assert.assertEquals(LOG_DISABLED, errorResult); + Assert.assertEquals(LOG_DISABLED, errorResult2); } } \ No newline at end of file From 127c23f681566d0efe0f52edf64750a4d8988c0e Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Wed, 21 Aug 2024 18:57:02 +0200 Subject: [PATCH 06/12] Add BacktraceLogger tests --- .../library/logger/BacktraceLoggerTest.java | 31 +++++++++++++++++++ .../library/logger/BacktraceMockLogger.java | 24 ++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java create mode 100644 backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java new file mode 100644 index 00000000..eb5490fc --- /dev/null +++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java @@ -0,0 +1,31 @@ +package backtraceio.library.logger; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class BacktraceLoggerTest { + @Before + public void setUp() { + BacktraceLogger.setLogger(new BacktraceMockLogger()); + } + + @Test + public void testCustomLogger() { + // GIVEN + final String TEST_MSG = "TEST-MSG"; + final String TEST_TAG = "TEST-TAG"; + + // WHEN + final int debugResult = BacktraceLogger.d(TEST_TAG, TEST_MSG); + final int warnResult = BacktraceLogger.w(TEST_TAG, TEST_MSG); + final int errorResult = BacktraceLogger.e(TEST_TAG, TEST_MSG); + final int errorResult2 = BacktraceLogger.e(TEST_TAG, TEST_MSG, new Exception()); + + // THEN + Assert.assertEquals(BacktraceMockLogger.MOCK_VALUE, debugResult); + Assert.assertEquals(BacktraceMockLogger.MOCK_VALUE, warnResult); + Assert.assertEquals(BacktraceMockLogger.MOCK_VALUE, errorResult); + Assert.assertEquals(BacktraceMockLogger.MOCK_VALUE, errorResult2); + } +} diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java new file mode 100644 index 00000000..922d683d --- /dev/null +++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java @@ -0,0 +1,24 @@ +package backtraceio.library.logger; + +public class BacktraceMockLogger implements Logger { + public final static int MOCK_VALUE = 123456789; + @Override + public int d(String tag, String message) { + return MOCK_VALUE; + } + + @Override + public int w(String tag, String message) { + return MOCK_VALUE; + } + + @Override + public int e(String tag, String message) { + return MOCK_VALUE; + } + + @Override + public int e(String tag, String message, Throwable tr) { + return MOCK_VALUE; + } +} From 2ae4e9892505619632be6076dc2605c941d7377b Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Tue, 3 Sep 2024 21:41:50 +0200 Subject: [PATCH 07/12] Fix logLevel type --- .../library/logger/BacktraceLogLogger.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 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 fefbd13d..fb7ac893 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java @@ -16,14 +16,14 @@ public class BacktraceLogLogger implements Logger { /** * Level from which all information is logged */ - private LogLevel logLevel; + private int logLevel; public BacktraceLogLogger() { this(LogLevel.OFF); } public BacktraceLogLogger(LogLevel logLevel) { - this.logLevel = logLevel; + this.logLevel = logLevel.ordinal(); } /** * set logging level from which all messages should be logged to the console @@ -31,7 +31,7 @@ public BacktraceLogLogger(LogLevel logLevel) { * @param level login level */ public void setLevel(LogLevel level) { - this.logLevel = level; + this.logLevel = level.ordinal(); } /** @@ -40,7 +40,7 @@ public void setLevel(LogLevel level) { * @return the number of bytes written */ public int d(String tag, String message) { - if (this.logLevel.ordinal() <= LogLevel.DEBUG.ordinal()) { + if (this.logLevel <= LogLevel.DEBUG.ordinal()) { return Log.d(this.getTag(tag), message); } return 0; @@ -54,7 +54,7 @@ public int d(String tag, String message) { * @return the number of bytes written */ public int w(String tag, String message) { - if (this.logLevel.ordinal() <= LogLevel.WARN.ordinal()) { + if (this.logLevel <= LogLevel.WARN.ordinal()) { return Log.w(this.getTag(tag), message); } return 0; @@ -68,7 +68,7 @@ public int w(String tag, String message) { * @return the number of bytes written */ public int e(String tag, String message) { - if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) { + if (this.logLevel <= LogLevel.ERROR.ordinal()) { return Log.e(this.getTag(tag), message); } return 0; @@ -83,7 +83,7 @@ public int e(String tag, String message) { * @return the number of bytes written */ public int e(String tag, String message, Throwable tr) { - if (this.logLevel.ordinal() <= LogLevel.ERROR.ordinal()) { + if (this.logLevel <= LogLevel.ERROR.ordinal()) { return Log.e(this.getTag(tag), message, tr); } return 0; From 513a7323d49205d068ac0def834bdde3d4f4bbe6 Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Tue, 3 Sep 2024 22:30:55 +0200 Subject: [PATCH 08/12] Revert to setLevel method --- .../library/logger/BacktraceLogLogger.java | 1 + .../library/logger/BacktraceLogger.java | 18 ++++++++++++++++++ .../backtraceio/library/logger/Logger.java | 1 + .../library/logger/BacktraceMockLogger.java | 3 +++ 4 files changed, 23 insertions(+) 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 fb7ac893..f83d2582 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java @@ -25,6 +25,7 @@ public BacktraceLogLogger() { public BacktraceLogLogger(LogLevel logLevel) { this.logLevel = logLevel.ordinal(); } + /** * set logging level from which all messages should be logged to the console * 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 f0be3316..920911eb 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java @@ -18,6 +18,10 @@ public class BacktraceLogger { private static Logger logger = new BacktraceLogLogger(); + public static Logger getLogger() { + return logger; + } + public static void setLogger(Logger logger) { BacktraceLogger.logger = logger; } @@ -64,4 +68,18 @@ public static int e(String tag, String message) { public static int e(String tag, String message, Throwable tr) { return logger.e(tag, message, tr); } + + /** + * Set logging level from which all messages should be logged to the console + * @param level login level (debug, warn, error, off) + * + * @deprecated setting the logging level should be done directly in the passed custom logger + * implementation so as not to depend on the internal implementation of Backtrace internal + * LogLevel types. + * + */ + @Deprecated + public static void setLevel(LogLevel level) { + logger.setLevel(level); + } } \ 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 index 2d2e24c5..d0c434c3 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java @@ -13,4 +13,5 @@ public interface Logger { int w(String tag, String message); int e(String tag, String message); int e(String tag, String message, Throwable tr); + void setLevel(LogLevel level); } \ No newline at end of file diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java index 922d683d..ff56b108 100644 --- a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java +++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java @@ -21,4 +21,7 @@ public int e(String tag, String message) { public int e(String tag, String message, Throwable tr) { return MOCK_VALUE; } + + @Override + public void setLevel(LogLevel level) { }; } From b77bb0b940ac80b54da11919ac2ce3bbb38294c4 Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Tue, 3 Sep 2024 22:35:26 +0200 Subject: [PATCH 09/12] Add NotNull annotations --- .../java/backtraceio/library/logger/BacktraceLogLogger.java | 6 ++++-- .../java/backtraceio/library/logger/BacktraceLogger.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 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 f83d2582..30e35511 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogLogger.java @@ -2,6 +2,8 @@ import android.util.Log; +import org.jetbrains.annotations.NotNull; + /** * Backtrace Logger implementation class for logging messages within the Backtrace library. *

@@ -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. *

*/ -public class BacktraceLogLogger implements Logger { +public class BacktraceInternalLogger implements Logger { private static final String BASE_TAG = "BacktraceLogger: "; + /** * Level from which all information is logged */ private int logLevel; - public BacktraceLogLogger() { + public BacktraceInternalLogger() { this(LogLevel.OFF); } - public BacktraceLogLogger(@NotNull LogLevel logLevel) { + public BacktraceInternalLogger(@NotNull LogLevel logLevel) { this.logLevel = logLevel.ordinal(); } + public int getLogLevel() { + return logLevel; + } + /** * set logging level from which all messages should be logged to the console * @@ -93,6 +98,6 @@ public int e(String tag, String message, Throwable tr) { } private String getTag(String tag) { - return BacktraceLogLogger.BASE_TAG + tag; + return BacktraceInternalLogger.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 0fedfa89..375e799e 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/BacktraceLogger.java @@ -7,7 +7,7 @@ *

* {@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. *

@@ -18,7 +18,7 @@ */ public class BacktraceLogger { - private static Logger logger = new BacktraceLogLogger(); + private static Logger logger = new BacktraceInternalLogger(); public static Logger getLogger() { return logger; @@ -85,6 +85,8 @@ public static int e(String tag, String message, Throwable tr) { */ @Deprecated public static void setLevel(@NotNull LogLevel level) { - logger.setLevel(level); + if (logger instanceof BacktraceInternalLogger) { + ((BacktraceInternalLogger) logger).setLevel(level); + } } } \ 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 index d0c434c3..2d2e24c5 100644 --- a/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java +++ b/backtrace-library/src/main/java/backtraceio/library/logger/Logger.java @@ -13,5 +13,4 @@ public interface Logger { int w(String tag, String message); int e(String tag, String message); int e(String tag, String message, Throwable tr); - void setLevel(LogLevel level); } \ No newline at end of file diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceInternalLoggerTest.java similarity index 96% rename from backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java rename to backtrace-library/src/test/java/backtraceio/library/logger/BacktraceInternalLoggerTest.java index 43362227..d17be196 100644 --- a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLogLoggerTest.java +++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceInternalLoggerTest.java @@ -15,7 +15,7 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -public class BacktraceLogLoggerTest { +public class BacktraceInternalLoggerTest { private final int LOG_ENABLED = 12345; private final int LOG_DISABLED = 0; @@ -29,7 +29,7 @@ public class LogMockAnswer implements Answer { @Mock private static MockedStatic mockLog; - private BacktraceLogLogger logger; + private BacktraceInternalLogger logger; @BeforeClass public static void init() { @@ -43,7 +43,7 @@ public static void close() { @Before public void setUp() { - logger = new BacktraceLogLogger(); + logger = new BacktraceInternalLogger(); mockLogMethods(); } 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 e53b2031..0b1f61fe 100644 --- a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java +++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceLoggerTest.java @@ -33,4 +33,16 @@ public void testCustomLogger() { public void testNullCustomLogger() { BacktraceLogger.setLogger(null); } + + @Test + public void testSetLevel() { + // GIVEN + final BacktraceInternalLogger logger = new BacktraceInternalLogger(); + BacktraceLogger.setLogger(logger); + // WHEN + BacktraceLogger.setLevel(LogLevel.WARN); + + // THEN + Assert.assertEquals(logger.getLogLevel(), LogLevel.WARN.ordinal()); + } } diff --git a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java index ff56b108..58efd203 100644 --- a/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java +++ b/backtrace-library/src/test/java/backtraceio/library/logger/BacktraceMockLogger.java @@ -22,6 +22,4 @@ public int e(String tag, String message, Throwable tr) { return MOCK_VALUE; } - @Override - public void setLevel(LogLevel level) { }; } From 2fa25d13b93ca1112f1d62a501344d6fc693cc79 Mon Sep 17 00:00:00 2001 From: Bartosz Litwiniuk <> Date: Wed, 4 Sep 2024 23:28:53 +0200 Subject: [PATCH 12/12] Update README.md --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index caa30d7d..d3096d31 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,3 @@ -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)