Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added log listener #584

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/PowerAuth-SDK-for-Android.md
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,27 @@ if (BuildConfig.DEBUG) {
```
<!-- end -->

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.getlime.security.powerauth.system;

import androidx.annotation.Nullable;

/**
* Class that provides logging facility for PowerAuth SDK library.
*
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Loading