Dynamically change log level

pull/214/head
M66B 1 year ago
parent 7e0cb664b6
commit 7b42fb1ea6

@ -67,7 +67,7 @@ public class ApplicationEx extends Application
@Override @Override
protected void attachBaseContext(Context base) { protected void attachBaseContext(Context base) {
TinyLogConfigurationLoader.setup(base); FairEmailLoggingProvider.setup(base);
Runtime.getRuntime().addShutdownHook(new Thread() { Runtime.getRuntime().addShutdownHook(new Thread() {
@Override @Override
@ -413,6 +413,10 @@ public class ApplicationEx extends Application
// Should be excluded for import // Should be excluded for import
restart(this, key); restart(this, key);
break; break;
case "debug":
case "log_level":
FairEmailLoggingProvider.setLevel(this);
break;
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);

@ -1308,7 +1308,7 @@ public class DebugHelper {
attachment.progress = 0; attachment.progress = 0;
attachment.id = db.attachment().insertAttachment(attachment); attachment.id = db.attachment().insertAttachment(attachment);
attachment.zip(context, TinyLogConfigurationLoader.getFiles(context)); attachment.zip(context, FairEmailLoggingProvider.getLogFiles(context));
/* /*
// https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html#java // https://cheatsheetseries.owasp.org/cheatsheets/OS_Command_Injection_Defense_Cheat_Sheet.html#java
ProcessBuilder pb = new ProcessBuilder("/system/bin/logcat", ProcessBuilder pb = new ProcessBuilder("/system/bin/logcat",

@ -25,55 +25,74 @@ import android.content.SharedPreferences;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.tinylog.Level; import org.tinylog.Level;
import org.tinylog.configuration.PropertiesConfigurationLoader; import org.tinylog.core.TinylogLoggingProvider;
import org.tinylog.format.MessageFormatter;
import org.tinylog.provider.ProviderRegistry;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.Properties;
// https://tinylog.org/v2/configuration/ // https://tinylog.org/v2/configuration/
// https://github.com/tinylog-org/tinylog-android-example/blob/v2/app/src/main/resources/tinylog.properties // https://github.com/tinylog-org/tinylog-android-example/blob/v2/app/src/main/resources/tinylog.properties
public class TinyLogConfigurationLoader extends PropertiesConfigurationLoader { public class FairEmailLoggingProvider extends TinylogLoggingProvider {
private static Level level = Level.TRACE; private Level activeLevel = Level.WARN;
public void setLevel(Level level) {
activeLevel = level;
}
@Override
public boolean isEnabled(int depth, String tag, Level level) {
return (activeLevel.ordinal() <= level.ordinal() &&
super.isEnabled(depth + 1, tag, level));
}
@Override @Override
public Properties load() { public void log(int depth, String tag, Level level, Throwable exception, MessageFormatter formatter, Object obj, Object... arguments) {
Properties props = super.load(); if (activeLevel.ordinal() <= level.ordinal())
props.setProperty("level", level.name()); super.log(depth, tag, level, exception, formatter, obj, arguments);
return props; }
@Override
public void log(String loggerClassName, String tag, Level level, Throwable exception, MessageFormatter formatter, Object obj, Object... arguments) {
if (activeLevel.ordinal() <= level.ordinal())
super.log(loggerClassName, tag, level, exception, formatter, obj, arguments);
} }
static void setup(Context context) { static void setup(Context context) {
System.setProperty("tinylog.directory",
new File(context.getFilesDir(), "logs").getAbsolutePath());
setLevel(context);
}
static void setLevel(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean debug = prefs.getBoolean("debug", false); // Changing requires force stop boolean debug = prefs.getBoolean("debug", false);
FairEmailLoggingProvider provider = (FairEmailLoggingProvider) ProviderRegistry.getLoggingProvider();
if (debug) if (debug)
level = Level.DEBUG; provider.activeLevel = Level.DEBUG;
else { else {
int def = (BuildConfig.DEBUG || BuildConfig.TEST_RELEASE ? android.util.Log.INFO : android.util.Log.WARN); int def = (BuildConfig.DEBUG || BuildConfig.TEST_RELEASE ? android.util.Log.INFO : android.util.Log.WARN);
int _level = prefs.getInt("log_level", def); int _level = prefs.getInt("log_level", def);
if (_level == android.util.Log.VERBOSE) if (_level == android.util.Log.VERBOSE)
level = Level.TRACE; provider.activeLevel = Level.TRACE;
else if (_level == android.util.Log.DEBUG) else if (_level == android.util.Log.DEBUG)
level = Level.DEBUG; provider.activeLevel = Level.DEBUG;
else if (_level == android.util.Log.INFO) else if (_level == android.util.Log.INFO)
level = Level.INFO; provider.activeLevel = Level.INFO;
else if (_level == android.util.Log.WARN) else if (_level == android.util.Log.WARN)
level = Level.WARN; provider.activeLevel = Level.WARN;
else if (_level == android.util.Log.ERROR) else if (_level == android.util.Log.ERROR)
level = Level.ERROR; provider.activeLevel = Level.ERROR;
} }
System.setProperty("tinylog.configurationloader",
TinyLogConfigurationLoader.class.getName());
System.setProperty("tinylog.directory",
new File(context.getFilesDir(), "logs").getAbsolutePath());
} }
static File[] getFiles(Context context) { static File[] getLogFiles(Context context) {
File[] files = new File(context.getFilesDir(), "logs").listFiles(); File[] files = new File(context.getFilesDir(), "logs").listFiles();
if (files == null) if (files == null)

@ -901,8 +901,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swLogInfo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { swLogInfo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton v, boolean checked) { public void onCheckedChanged(CompoundButton v, boolean checked) {
prefs.edit().putInt("log_level", checked ? android.util.Log.INFO : android.util.Log.WARN).commit(); prefs.edit().putInt("log_level", checked ? android.util.Log.INFO : android.util.Log.WARN).apply();
ApplicationEx.restart(v.getContext(), "log_level");
} }
}); });

@ -784,19 +784,6 @@
app:layout_constraintTop_toBottomOf="@id/tvProtocolHint" app:layout_constraintTop_toBottomOf="@id/tvProtocolHint"
app:switchPadding="12dp" /> app:switchPadding="12dp" />
<TextView
android:id="@+id/tvLogInfoHint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_english_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swLogInfo" />
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
android:id="@+id/swDebug" android:id="@+id/swDebug"
android:layout_width="0dp" android:layout_width="0dp"
@ -807,7 +794,7 @@
android:text="@string/title_advanced_debug" android:text="@string/title_advanced_debug"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvLogInfoHint" app:layout_constraintTop_toBottomOf="@id/swLogInfo"
app:switchPadding="12dp" /> app:switchPadding="12dp" />
<TextView <TextView

@ -1,4 +1,5 @@
level = warn provider = fair email
level = info
writer1 = logcat writer1 = logcat
writer1.format = {tag} {message} writer1.format = {tag} {message}

Loading…
Cancel
Save