Refactor log config

This commit is contained in:
AsamK 2023-11-09 15:55:43 +01:00
parent 19e9f31afd
commit 3027ba2cf1

View file

@ -45,23 +45,18 @@ public class Main {
installSecurityProviderWorkaround(); installSecurityProviderWorkaround();
// Configuring the logger needs to happen before any logger is initialized // Configuring the logger needs to happen before any logger is initialized
final var loggingConfig = parseLoggingConfig(args);
configureLogging(loggingConfig);
final var nsLog = parseArgs(args); final var parser = App.buildArgumentParser();
final var verboseLevel = nsLog == null ? 0 : nsLog.getInt("verbose"); final var ns = parser.parseArgsOrFail(args);
final var logFile = nsLog == null ? null : nsLog.<File>get("log-file");
final var scrubLog = nsLog != null && nsLog.getBoolean("scrub-log");
configureLogging(verboseLevel, logFile, scrubLog);
var parser = App.buildArgumentParser();
var ns = parser.parseArgsOrFail(args);
int status = 0; int status = 0;
try { try {
new App(ns).init(); new App(ns).init();
} catch (CommandException e) { } catch (CommandException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
if (verboseLevel > 0 && e.getCause() != null) { if (loggingConfig.verboseLevel > 0 && e.getCause() != null) {
e.getCause().printStackTrace(System.err); e.getCause().printStackTrace(System.err);
} }
status = getStatusForError(e); status = getStatusForError(e);
@ -78,6 +73,21 @@ public class Main {
Security.addProvider(new BouncyCastleProvider()); Security.addProvider(new BouncyCastleProvider());
} }
private static LoggingConfig parseLoggingConfig(final String[] args) {
final var nsLog = parseArgs(args);
if (nsLog == null) {
return new LoggingConfig(0, null, false);
}
final var verboseLevel = nsLog.getInt("verbose");
final var logFile = nsLog.<File>get("log-file");
final var scrubLog = nsLog.getBoolean("scrub-log");
return new LoggingConfig(verboseLevel, logFile, scrubLog);
}
/**
* This method only parses commandline args relevant for logging configuration.
*/
private static Namespace parseArgs(String[] args) { private static Namespace parseArgs(String[] args) {
var parser = ArgumentParsers.newFor("signal-cli", DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS) var parser = ArgumentParsers.newFor("signal-cli", DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS)
.includeArgumentNamesAsKeysInResult(true) .includeArgumentNamesAsKeysInResult(true)
@ -94,14 +104,16 @@ public class Main {
} }
} }
private static void configureLogging(final int verboseLevel, final File logFile, final boolean scrubLog) { private static void configureLogging(final LoggingConfig loggingConfig) {
LogConfigurator.setVerboseLevel(verboseLevel); LogConfigurator.setVerboseLevel(loggingConfig.verboseLevel);
LogConfigurator.setLogFile(logFile); LogConfigurator.setLogFile(loggingConfig.logFile);
LogConfigurator.setScrubSensitiveInformation(scrubLog); LogConfigurator.setScrubSensitiveInformation(loggingConfig.scrubLog);
if (verboseLevel > 0) { if (loggingConfig.verboseLevel > 0) {
java.util.logging.Logger.getLogger("") java.util.logging.Logger.getLogger("")
.setLevel(verboseLevel > 2 ? java.util.logging.Level.FINEST : java.util.logging.Level.INFO); .setLevel(loggingConfig.verboseLevel > 2
? java.util.logging.Level.FINEST
: java.util.logging.Level.INFO);
ManagerLogger.initLogger(); ManagerLogger.initLogger();
} }
SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.removeHandlersForRootLogger();
@ -118,4 +130,6 @@ public class Main {
case null -> 2; case null -> 2;
}; };
} }
private record LoggingConfig(int verboseLevel, File logFile, boolean scrubLog) {}
} }