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

Allow usage of a custom logger (Timber) instead of Logcat #248

Merged
merged 3 commits into from
Jul 17, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Version 1.4.0-SNAPSHOT
* Added pre-scan verification for excessive scan (undocumented Android 7.0 "feature") (https://github.com/Polidea/RxAndroidBle/issues/227)
* Adjusted `BleCannotSetCharacteristicNotificationException` to contain the cause exception if available. `RxBleConnection.setupNotification()`/`RxBleConnection.setupIndication()` will now throw the cause of disconnection if subscribed after connection was disconnected. (https://github.com/Polidea/RxAndroidBle/issues/225)
* _Changed Behaviour_ of `RxBleDevice.observeConnectionStateChanges()` - does not emit initial state and reflects best `BluetoothGatt` state. (https://github.com/Polidea/RxAndroidBle/issues/50)
* Added support for a custom Logger `RxBleLog.setLogger(Logger)` as alternative to Logcat (https://github.com/Polidea/RxAndroidBle/pull/248)

Version 1.3.3
* Fixed scan filtering by name on API <21 (https://github.com/Polidea/RxAndroidBle/pull/243)
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ For connection debugging you can use extended logging
RxBleClient.setLogLevel(RxBleLog.DEBUG);
```

By default `RxBleLog` uses logcat to print the messages. You can provide your own logger implementation to forward it to other logging libraries such as Timber.
```java
RxBleLog.setLogger((level, tag, msg) -> Timber.tag(tag).log(level, msg));
```

### Error handling
Every error you may encounter is provided via onError callback. Each public method has JavaDoc explaining possible errors.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.polidea.rxandroidble.internal;

import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;

import java.lang.annotation.Retention;
Expand Down Expand Up @@ -28,12 +29,61 @@ public class RxBleLog {
private static final Pattern ANONYMOUS_CLASS = Pattern.compile("\\$\\d+$");
private static final ThreadLocal<String> NEXT_TAG = new ThreadLocal<>();

private static Logger logcatLogger = new Logger() {
@Override
public void log(final int level, final String tag, final String msg) {
Log.println(level, tag, msg);
}
};

private static int logLevel = Integer.MAX_VALUE;

private static Logger logger = logcatLogger;

private RxBleLog() {

}

/**
* Simple logging interface for log messages from RxAndroidBle
*
* @see #setLogger(Logger)
*/
public interface Logger {

/**
* @param level one of {@link Log#VERBOSE}, {@link Log#DEBUG},{@link Log#INFO},
* {@link Log#WARN},{@link Log#ERROR}
* @param tag log tag, caller
* @param msg message to log
*/
void log(int level, String tag, String msg);
}

/**
* Set a custom logger implementation, set it to {@code null} to use default logcat logging
*
* Example how to forward logs to Timber:<br>
*
* <code>
* <pre>
* RxBleLog.setLogger(new RxBleLog.Logger() {
* &#64;Override
* public void log(final int level, final String tag, final String msg) {
* Timber.tag(tag).log(level, msg);
* }
* });
* </pre>
* </code>
*/
public static void setLogger(@Nullable final Logger logger) {
if (logger == null) {
RxBleLog.logger = logcatLogger;
} else {
RxBleLog.logger = logger;
}
}

public static void setLogLevel(@LogLevel int logLevel) {
RxBleLog.logLevel = logLevel;
}
Expand Down Expand Up @@ -132,14 +182,14 @@ private static void throwShade(int priority, Throwable t, String message, Object

private static void println(int priority, String tag, String message) {
if (message.length() < 4000) {
Log.println(priority, tag, message);
logger.log(priority, tag, message);
} else {
// It's rare that the message will be this large, so we're ok with the perf hit of splitting
// and calling Log.println N times. It's possible but unlikely that a single line will be
// longer than 4000 characters: we're explicitly ignoring this case here.
String[] lines = message.split("\n");
for (String line : lines) {
Log.println(priority, tag, line);
logger.log(priority, tag, line);
}
}
}
Expand Down