Make file-logging configurable

This commit is contained in:
cpfeiffer 2015-05-23 00:45:12 +02:00
parent c469248de1
commit 4518e8819d
5 changed files with 40 additions and 10 deletions

View File

@ -10,11 +10,12 @@
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${GB_LOGFILES_DIR}/gadgetbridge.log</file> <file>${GB_LOGFILES_DIR}/gadgetbridge.log</file>
<lazy>true</lazy> <lazy>true</lazy>
<!-- encoders are by default assigned the type <!-- encoders are by default assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder --> ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>gadgetbridge-%d{yyyy-MM-dd}.log.zip</fileNamePattern> <fileNamePattern>${GB_LOGFILES_DIR}/gadgetbridge-%d{yyyy-MM-dd}.log.zip</fileNamePattern>
<maxHistory>10</maxHistory> <maxHistory>10</maxHistory>
</rollingPolicy> </rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">

View File

@ -7,7 +7,9 @@ import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -2,6 +2,11 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
@ -16,23 +21,37 @@ public class GBApplication extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
setupLogFileDir(); setupLogging();
// For debugging problems with the logback configuration // For debugging problems with the logback configuration
// LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); // LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
// print logback's internal status // print logback's internal status
// StatusPrinter.print(lc); // StatusPrinter.print(lc);
// String dataDir = lc.getProperty(CoreConstants.DATA_DIR_KEY);
// String filename = FileUtil.prefixRelativePath(dataDir, "gadgetbridge.log");
// Logger logger = LoggerFactory.getLogger(GBApplication.class); // Logger logger = LoggerFactory.getLogger(GBApplication.class);
} }
private void setupLogFileDir() { public static boolean isFileLoggingEnabled() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(GBApplication.getContext());
return prefs.getBoolean(GBApplication.getContext().getString(R.string.pref_log_to_file), false);
}
private void setupLogging() {
if (isFileLoggingEnabled()) {
File dir = getExternalFilesDir(null); File dir = getExternalFilesDir(null);
if (dir != null && !dir.exists()) { if (dir != null && !dir.exists()) {
dir.mkdirs(); dir.mkdirs();
} }
// used by assets/logback.xml since the location cannot be statically determined // used by assets/logback.xml since the location cannot be statically determined
System.setProperty("GB_LOGFILES_DIR", dir.getAbsolutePath()); System.setProperty("GB_LOGFILES_DIR", dir.getAbsolutePath());
} else {
System.setProperty("GB_LOGFILES_DIR", "/dev/null"); // just to please logback configuration, not used at all
try {
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.detachAppender("FILE");
} catch (Throwable ex) {
System.out.println("Error removing logger FILE appender");
ex.printStackTrace();
}
}
} }
public static Context getContext() { public static Context getContext() {

View File

@ -101,5 +101,7 @@
<string name="pref_header_vibration_count">Vibration Count</string> <string name="pref_header_vibration_count">Vibration Count</string>
<string name="title_activity_sleepmonitor">Sleep Monitor</string> <string name="title_activity_sleepmonitor">Sleep Monitor</string>
<string name="pref_log_to_file">LogToFile</string>
<string name="pref_write_logfiles">Write Log Files (needs restart)</string>
</resources> </resources>

View File

@ -60,4 +60,10 @@
android:key="pref_key_miband" android:key="pref_key_miband"
android:title="@string/preferences_miband_settings" /> android:title="@string/preferences_miband_settings" />
</PreferenceCategory> </PreferenceCategory>
<CheckBoxPreference
android:defaultValue="false"
android:key="@string/pref_log_to_file"
android:title="@string/pref_write_logfiles" />
</PreferenceScreen> </PreferenceScreen>