diff --git a/docs/PowerAuth-SDK-for-Android.md b/docs/PowerAuth-SDK-for-Android.md index cb471666..47f29bce 100644 --- a/docs/PowerAuth-SDK-for-Android.md +++ b/docs/PowerAuth-SDK-for-Android.md @@ -2843,6 +2843,27 @@ if (BuildConfig.DEBUG) { ``` +You can intercept the log and log it into your own report system, you can do so with `PowerAuthLogListener`. + +```kotlin +PowerAuthLog.logListener = object : PowerAuthLogListener { + override fun powerAuthDebugLog(message: String) { + // Process debug log... + // Note that the debug log is only reported when + // PowerAuthLog.setEnabled(true) is set. + // We highly discourage using debug logs in production builds. + } + + override fun powerAuthWarningLog(message: String) { + // Process warning log... + } + + override fun powerAuthErrorLog(message: String) { + // Process error log... + } +} +``` + ## Additional Features PowerAuth SDK for Android contains multiple additional features that are useful for mobile apps. diff --git a/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java index ceb89fe4..7aeb8773 100644 --- a/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java +++ b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLog.java @@ -16,6 +16,8 @@ package io.getlime.security.powerauth.system; +import androidx.annotation.Nullable; + /** * Class that provides logging facility for PowerAuth SDK library. * @@ -37,6 +39,11 @@ public class PowerAuthLog { */ private static final String LOG_TAG = "PowerAuthLibrary"; + /** + * Listener that can tap into the log stream and process it on it's own. + */ + public @Nullable static PowerAuthLogListener logListener; + /** * Controls logging from PowerAuth classes * @param enabled enables or disables debug logs @@ -74,6 +81,9 @@ public static void d(String format, Object... args) { if (logIsEnabled) { String message = String.format(format, args); android.util.Log.d(LOG_TAG, message); + if (logListener != null) { + logListener.powerAuthDebugLog(message); + } } } @@ -87,6 +97,9 @@ public static void d(String format, Object... args) { public static void e(String format, Object... args) { String message = String.format(format, args); android.util.Log.e(LOG_TAG, message); + if (logListener != null) { + logListener.powerAuthErrorLog(message); + } } /** @@ -99,5 +112,8 @@ public static void e(String format, Object... args) { public static void w(String format, Object... args) { String message = String.format(format, args); android.util.Log.w(LOG_TAG, message); + if (logListener != null) { + logListener.powerAuthWarningLog(message); + } } } diff --git a/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLogListener.java b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLogListener.java new file mode 100644 index 00000000..6b943097 --- /dev/null +++ b/proj-android/PowerAuthLibrary/src/main/java/io/getlime/security/powerauth/system/PowerAuthLogListener.java @@ -0,0 +1,51 @@ +/* + * Copyright 2024 Wultra s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.getlime.security.powerauth.system; + +import androidx.annotation.NonNull; + +/** + * Interface that represents a listener that can tap into the library logs and use them for example + * to report to a online system or to a logfile for user to send with some report. Note that by default + * all logs are logged into the Logcat. + * + * @author Jan Kobersky + */ +public interface PowerAuthLogListener { + + /** + * Debug message reported by the library. Debug messages are only reported when + * {@link PowerAuthLog} setEnabled is set to true. + * + * @param message Debug message. + */ + void powerAuthDebugLog(@NonNull String message); + + /** + * Warning reported by the library. + * + * @param message Warning message. + */ + void powerAuthWarningLog(@NonNull String message); + + /** + * Error reported by the library. + * + * @param message Error message. + */ + void powerAuthErrorLog(@NonNull String message); +}