Sebastian Stenzel 6 年之前
父节点
当前提交
a3474e05eb

+ 21 - 42
main/launcher/src/main/java/org/cryptomator/logging/DebugMode.java

@@ -8,73 +8,52 @@ package org.cryptomator.logging;
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
-import org.cryptomator.common.FxApplicationScoped;
+import javafx.beans.value.ObservableValue;
 import org.cryptomator.common.settings.Settings;
-import org.slf4j.ILoggerFactory;
 import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import java.util.Collection;
-
-import static java.util.Arrays.asList;
+import java.util.Map;
 
 @Singleton
 public class DebugMode {
 
 	private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(DebugMode.class);
 
-	private static final Collection<LoggerUpgrade> LOGGER_UPGRADES = asList( //
-			loggerUpgrade(org.slf4j.Logger.ROOT_LOGGER_NAME, Level.INFO), //
-			loggerUpgrade("org.cryptomator", Level.TRACE), //
-			loggerUpgrade("org.eclipse.jetty.server.HttpChannel", Level.DEBUG) //
-	);
-
 	private final Settings settings;
+	private final LoggerContext context;
 
 	@Inject
-	public DebugMode(Settings settings) {
+	public DebugMode(Settings settings, LoggerContext context) {
 		this.settings = settings;
+		this.context = context;
 	}
 
 	public void initialize() {
-		if (settings.debugMode().get()) {
-			enable();
-			LOG.debug("Debug mode initialized");
-		}
-	}
-
-	private void enable() {
-		ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
-		if (loggerFactory instanceof LoggerContext) {
-			LoggerContext context = (LoggerContext) loggerFactory;
-			LOGGER_UPGRADES.forEach(loggerUpgrade -> loggerUpgrade.execute(context));
-		} else {
-			LOG.warn("SLF4J not bound to Logback.");
-		}
+		setLogLevels(settings.debugMode().get());
+		settings.debugMode().addListener(this::logLevelChanged);
 	}
 
-	private static LoggerUpgrade loggerUpgrade(String loggerName, Level minLevel) {
-		return new LoggerUpgrade(loggerName, minLevel);
+	private void logLevelChanged(@SuppressWarnings("unused") ObservableValue<? extends Boolean> observable, @SuppressWarnings("unused") Boolean oldValue, Boolean newValue) {
+		setLogLevels(newValue);
 	}
 
-	private static class LoggerUpgrade {
-
-		private final Level level;
-		private final String loggerName;
-
-		public LoggerUpgrade(String loggerName, Level minLevel) {
-			this.loggerName = loggerName;
-			this.level = minLevel;
+	private void setLogLevels(boolean debugMode) {
+		if (debugMode) {
+			setLogLevels(LoggerModule.DEBUG_LOG_LEVELS);
+			LOG.debug("Debug mode enabled");
+		} else {
+			LOG.debug("Debug mode disabled");
+			setLogLevels(LoggerModule.DEFAULT_LOG_LEVELS);
 		}
+	}
 
-		public void execute(LoggerContext context) {
-			Logger logger = context.getLogger(loggerName);
-			if (logger != null && logger.getEffectiveLevel().isGreaterOrEqual(level)) {
-				logger.setLevel(level);
-			}
+	private void setLogLevels(Map<String, Level> logLevels) {
+		for (Map.Entry<String, Level> loglevel : logLevels.entrySet()) {
+			Logger logger = context.getLogger(loglevel.getKey());
+			logger.setLevel(loglevel.getValue());
 		}
-
 	}
 
 }

+ 23 - 11
main/launcher/src/main/java/org/cryptomator/logging/LoggerModule.java

@@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
 import javax.inject.Named;
 import javax.inject.Singleton;
 import java.nio.file.Path;
+import java.util.Map;
 
 @Module
 public class LoggerModule {
@@ -32,8 +33,15 @@ public class LoggerModule {
 	private static final int LOGFILE_ROLLING_MIN = 1;
 	private static final int LOGFILE_ROLLING_MAX = 9;
 	private static final double SHUTDOWN_DELAY_MS = 100;
-	private static final Level ROOT_LOG_LEVEL = Level.INFO;
 	private static final String LOG_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n";
+	static final Map<String, Level> DEFAULT_LOG_LEVELS = Map.of( //
+			Logger.ROOT_LOGGER_NAME, Level.INFO, //
+			"org.cryptomator", Level.INFO //
+	);
+	static final Map<String, Level> DEBUG_LOG_LEVELS = Map.of( //
+			Logger.ROOT_LOGGER_NAME, Level.INFO, //
+			"org.cryptomator", Level.TRACE //
+	);
 
 	@Provides
 	@Singleton
@@ -133,17 +141,21 @@ public class LoggerModule {
 			return () -> {
 				context.reset();
 
-				// configure root logger:
-				Logger root = context.getLogger(Logger.ROOT_LOGGER_NAME);
-				root.setLevel(ROOT_LOG_LEVEL);
-				root.addAppender(stdout);
-				root.addAppender(file);
+				// configure loggers:
+				for (Map.Entry<String, Level> loglevel : DEFAULT_LOG_LEVELS.entrySet()) {
+					Logger logger = context.getLogger(loglevel.getKey());
+					logger.setLevel(loglevel.getValue());
+					logger.setAdditive(false);
+					logger.addAppender(stdout);
+					logger.addAppender(file);
+				}
 
-				// configure root logger:
-				Logger uprades = context.getLogger("org.cryptomator.ui.model");
-				uprades.setLevel(Level.DEBUG);
-				uprades.addAppender(stdout);
-				uprades.addAppender(upgrade);
+				// configure upgrade logger:
+				Logger upgrades = context.getLogger("org.cryptomator.ui.model.upgrade");
+				upgrades.setLevel(Level.DEBUG);
+				upgrades.addAppender(stdout);
+				upgrades.addAppender(upgrade);
+				upgrades.setAdditive(false);
 
 				// add shutdown hook
 				DelayingShutdownHook shutdownHook = new DelayingShutdownHook();

+ 0 - 1
main/ui/src/main/resources/fxml/settings.fxml

@@ -56,5 +56,4 @@
 
 		</children>
 	</GridPane>
-	<Label VBox.vgrow="NEVER" text="%settings.requiresRestartLabel" alignment="CENTER" cacheShape="true" cache="true" />
 </VBox>			

+ 1 - 2
main/ui/src/main/resources/localization/en.txt

@@ -130,8 +130,7 @@ settings.webdav.port.label=WebDAV Port
 settings.webdav.port.prompt=0 = Choose automatically
 settings.webdav.port.apply=Apply
 settings.webdav.prefGvfsScheme.label=WebDAV Scheme
-settings.debugMode.label=Debug Mode *
-settings.requiresRestartLabel=* Cryptomator needs to restart
+settings.debugMode.label=Debug Mode
 settings.volume.label=Preferred Volume Type
 settings.volume.webdav=WebDAV
 settings.volume.fuse=FUSE