Skip to content

Commit

Permalink
Closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Apr 14, 2018
1 parent 124180d commit 6960d15
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target
.gradle
build
local.properties
out
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Build Status](https://travis-ci.org/mvysny/slf4j-handroid.svg?branch=master)](https://travis-ci.org/mvysny/slf4j-handroid)
[![GitHub tag](https://img.shields.io/github/tag/mvysny/slf4j-handroid.svg)](https://github.com/mvysny/slf4j-handroid/tags)

# slf4j-android fixed

Expand All @@ -9,18 +10,20 @@ Features

* Shows DEBUG messages during the development: http://jira.qos.ch/browse/SLF4J-314
* Does not hide any exceptions, even exceptions hidden by buggy Android Studio 1.5. Fixes https://code.google.com/p/android/issues/detail?id=195164 https://code.google.com/p/android/issues/detail?id=194446 http://stackoverflow.com/questions/28897239/log-e-does-not-print-the-stack-trace-of-unknownhostexception
* Supports Crashlytics; when Crashlytics jar is included in your project then slf4-handroid will
* Supports Crashlytics; just call `HandroidLoggerAdapter.enableLoggingToCrashlytics();` AFTER Crashlytics has been initialized in your App's `onCreate()` method. slf4-handroid will then
automatically log using `Crashlytics.log()` and `Crashlytics.logException()` instead of using plain Android logger.

## Using with your project

slf4j-handroid is now at jcenter, so all you have to do is to add this to your project dependencies:
```groovy
dependencies {
compile 'sk.baka.slf4j:slf4j-handroid:1.7.26'
compile 'sk.baka.slf4j:slf4j-handroid:x.y.z'
}
```

> Note: check the latest version of slf4j-handroid from Git tag stated above.
If this won't work, add the jcenter repo:
```groovy
repositories {
Expand Down Expand Up @@ -58,3 +61,17 @@ otherwise your class names will get chopped.

Unfortunately I can't take advantage of the fact that the tag text is no longer constrained to 23 characters on Androids
24 and newer, because in reality that is not true: see [Issue #2](../../issues/2) for details.

## Crashlytics

slf4j-handroid supports Crashlytics. Make sure to call `HandroidLoggerAdapter.enableLoggingToCrashlytics();` after the Crashlytics is initialized in your App:
```java
@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
HandroidLoggerAdapter.enableLoggingToCrashlytics();
...
}
```
Unfortunately automatic detection of Crashlytics does not work because of https://github.com/mvysny/slf4j-handroid/issues/5
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
defaultTasks 'clean', 'build', 'publishToMavenLocal'

group 'sk.baka.slf4j'
version '1.7.26-1-SNAPSHOT'
version '1.7.27-SNAPSHOT'
ext.local = new Properties()
if (project.rootProject.file('local.properties').exists()) {
ext.local.load(project.rootProject.file('local.properties').newDataInputStream())
Expand Down
39 changes: 21 additions & 18 deletions src/main/java/org/slf4j/impl/HandroidLoggerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,34 @@ public class HandroidLoggerAdapter extends AndroidLoggerAdapter {
public static int ANDROID_API_LEVEL = 1;

/**
* If true, the log messages are routed to the Crashlytics library. The value of this property is auto-detected - if
* <code>com.crashlytics.android.Crashlytics</code> class is present, this is set to true. You can however override that as you see fit.
* If called, the log messages are routed to the Crashlytics library. You must call this AFTER Crashlytics is initialized in your code;
* see https://github.com/mvysny/slf4j-handroid/issues/5 for more details. Example of proper initialization in your App:
* <code><pre>
* &#64;Override
* public void onCreate() {
* super.onCreate();
* Fabric.with(this, new Crashlytics());
* HandroidLoggerAdapter.enableLoggingToCrashlytics();
* ...
* </pre></code>
* <p></p>
* Warning: only exception stacktraces logged as WARNING or ERROR are logged into Crashlytics. See {@link #logInternal(int, String, Throwable)}
* for details.
* @throws RuntimeException if the Crashlytics library is not on your classpath.
*/
public static boolean LOG_TO_CRASHLYTICS;
private static Method crashlyticsLog;
private static Method crashlyticsLogException;
static {
public static void enableLoggingToCrashlytics() {
try {
final Class<?> crashlyticsClass = Class.forName("com.crashlytics.android.Crashlytics");
try {
// yes I know, reflection is slower. Yet crashlytics doesn't seem to provide jars, only aar which I can't link against.
crashlyticsLog = crashlyticsClass.getDeclaredMethod("log", int.class, String.class, String.class);
crashlyticsLogException = crashlyticsClass.getDeclaredMethod("logException", Throwable.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
LOG_TO_CRASHLYTICS = true;
System.out.println("slf4j-handroid: enabling integration with Crashlytics, override by setting HandroidLoggerAdapter.LOG_TO_CRASHLYTICS to false");
} catch (ClassNotFoundException e) {
LOG_TO_CRASHLYTICS = false;
// yes I know, reflection is slower. Yet crashlytics doesn't seem to provide jars, only aar which I can't link against.
crashlyticsLog = crashlyticsClass.getDeclaredMethod("log", int.class, String.class, String.class);
crashlyticsLogException = crashlyticsClass.getDeclaredMethod("logException", Throwable.class);
System.out.println("slf4j-handroid: enabling integration with Crashlytics");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Method crashlyticsLog;
private static Method crashlyticsLogException;

HandroidLoggerAdapter(String tag) {
super(tag);
Expand Down Expand Up @@ -102,7 +105,7 @@ protected void logInternal(int priority, String message, Throwable throwable) {
message += '\n' + getStackTraceString(throwable);
}
message = postprocessMessage(message).trim();
if (LOG_TO_CRASHLYTICS) {
if (crashlyticsLog != null) {
// this also internally calls Log.println(), so no need to do it ourselves
try {
crashlyticsLog.invoke(null, priority, name, message);
Expand Down

0 comments on commit 6960d15

Please sign in to comment.