From d2bcccaeef45b48f2772fd00d8b7c8acf46d9816 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Wed, 13 May 2015 23:15:20 +0200 Subject: [PATCH] Use logback-android for logging to external storage (no WRITE_EXTERNAL_STORAGE permission necessary!) --- app/build.gradle | 4 ++- app/src/main/assets/logback.xml | 28 +++++++++++++++ .../gadgetbridge/GBApplication.java | 34 +++++++++++++++++++ app/src/main/resources/logback.xml | 25 ++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 app/src/main/assets/logback.xml create mode 100644 app/src/main/resources/logback.xml diff --git a/app/build.gradle b/app/build.gradle index bf3f51a7..a1505890 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,5 +23,7 @@ dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:support-v4:21.0.3' - compile 'com.noveogroup.android:android-logger:1.3.5' +// compile 'com.noveogroup.android:android-logger:1.3.5' + compile 'com.github.tony19:logback-android-classic:1.1.1-3' + compile 'org.slf4j:slf4j-api:1.7.7' } diff --git a/app/src/main/assets/logback.xml b/app/src/main/assets/logback.xml new file mode 100644 index 00000000..4cab9d3c --- /dev/null +++ b/app/src/main/assets/logback.xml @@ -0,0 +1,28 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + ${GB_LOGFILES_DIR}/gadgetbridge.log + true + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{25} - %msg%n + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index cee47fee..263f2fa9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -3,6 +3,17 @@ package nodomain.freeyourgadget.gadgetbridge; import android.app.Application; import android.content.Context; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileOutputStream; + +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.core.CoreConstants; +import ch.qos.logback.core.util.FileUtil; +import ch.qos.logback.core.util.StatusPrinter; + public class GBApplication extends Application { private static GBApplication context; @@ -10,6 +21,29 @@ public class GBApplication extends Application { context = this; } + @Override + public void onCreate() { + super.onCreate(); + + setupLogFileDir(); +// For debugging problems with the logback configuration +// LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); +// print logback's internal status +// StatusPrinter.print(lc); +// String dataDir = lc.getProperty(CoreConstants.DATA_DIR_KEY); +// String filename = FileUtil.prefixRelativePath(dataDir, "gadgetbridge.log"); +// Logger logger = LoggerFactory.getLogger(GBApplication.class); + } + + private void setupLogFileDir() { + File dir = getExternalFilesDir(null); + if (dir != null && !dir.exists()) { + dir.mkdirs(); + } + // used by assets/logback.xml since the location cannot be statically determined + System.setProperty("GB_LOGFILES_DIR", dir.getAbsolutePath()); + } + public static Context getContext() { return context; } diff --git a/app/src/main/resources/logback.xml b/app/src/main/resources/logback.xml new file mode 100644 index 00000000..2cec774c --- /dev/null +++ b/app/src/main/resources/logback.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + gadgetbridge.log + + + + + %date [%thread] %-5level %logger{25} - %msg%n + + + + + + + \ No newline at end of file