Skip to content

Commit

Permalink
Merge pull request #103 from qrollin/add_verbose_log_level
Browse files Browse the repository at this point in the history
Add log level handling as verbose parameter
  • Loading branch information
osalvador authored Oct 14, 2022
2 parents f8cc7ee + e3fa4e2 commit 3087647
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
14 changes: 5 additions & 9 deletions src/main/java/org/replicadb/ReplicaDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ public static int processReplica(ToolOptions options) {

LOG.info("Running ReplicaDB version: " + options.getVersion());

if (options.isVerbose()) {
setLogToDebugMode();
LOG.info("Setting verbose mode");
LOG.debug(options.toString());
}
ReplicaDB.setLogToMode(options.getVerboseLevel());
LOG.info("Setting verbose mode " + options.getVerboseLevel());
LOG.debug(options.toString());

if (!options.isHelp() && !options.isVersion()) {
// Sentry
Expand Down Expand Up @@ -163,14 +161,12 @@ public static int processReplica(ToolOptions options) {
}


private static void setLogToDebugMode() {

private static void setLogToMode(Level level) {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.setLevel(Level.valueOf("DEBUG"));
loggerConfig.setLevel(level);
ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfi

}


Expand Down
38 changes: 25 additions & 13 deletions src/main/java/org/replicadb/cli/ToolOptions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.replicadb.cli;

import org.apache.commons.cli.*;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -43,7 +44,7 @@ public class ToolOptions {
private int bandwidthThrottling = 0;
private Boolean help = false;
private Boolean version = false;
private Boolean verbose = false;
private Level verboseLevel = Level.INFO;
private Boolean quotedIdentifiers = false;
private String optionsFile;

Expand Down Expand Up @@ -335,7 +336,7 @@ private void checkOptions(String[] args) throws ParseException, IOException {
}

//get & set Options
if (line.hasOption("verbose")) setVerbose(true);
if (line.hasOption("verbose")) handleVerboseLevelArgument(line.getOptionValue("verbose"));
if (line.hasOption("sink-disable-index")) setSinkDisableIndexNotNull(true);
if (line.hasOption("sink-disable-escape")) setSinkDisableEscapeNotNull(true);
if (line.hasOption("sink-disable-truncate")) setSinkDisableTruncateNotNull(true);
Expand Down Expand Up @@ -372,6 +373,22 @@ private void checkOptions(String[] args) throws ParseException, IOException {

}

private void handleVerboseLevelArgument(String verboseLevel) {
if (verboseLevel == null || verboseLevel.isEmpty()) {
setVerboseLevel(Level.INFO);
return;
} else if (Boolean.parseBoolean(verboseLevel)) {
setVerboseLevel(Level.DEBUG);
return;
}

try {
Level argumentLevel = Level.valueOf(verboseLevel);
setVerboseLevel(argumentLevel);
} catch (IllegalArgumentException e) {
setVerboseLevel(Level.INFO);
}
}

private void printHelp() {
String header = "\nArguments: \n";
Expand Down Expand Up @@ -433,7 +450,7 @@ private void loadOptionsFile() throws IOException {
Properties prop = of.getProperties();
setSinkAnalyze(Boolean.parseBoolean(prop.getProperty("sink.analyze")));

setVerbose(Boolean.parseBoolean(prop.getProperty("verbose")));
handleVerboseLevelArgument(prop.getProperty("verbose"));
setMode(prop.getProperty("mode"));

setSinkColumns(prop.getProperty("sink.columns"));
Expand Down Expand Up @@ -671,17 +688,12 @@ public void setHelp(Boolean help) {
}


public Boolean isVerbose() {
return verbose;
}

public void setVerbose(Boolean verbose) {
this.verbose = verbose;
public Level getVerboseLevel() {
return verboseLevel;
}

public void setVerboseNotNull(Boolean verbose) {
if (verbose != null)
this.verbose = verbose;
public void setVerboseLevel(Level verboseLevel) {
this.verboseLevel = verboseLevel;
}

public String getOptionsFile() {
Expand Down Expand Up @@ -872,7 +884,7 @@ public String toString() {
",\n\tfetchSize=" + fetchSize +
",\n\thelp=" + help +
",\n\tversion=" + version +
",\n\tverbose=" + verbose +
",\n\tverbose=" + verboseLevel +
",\n\toptionsFile='" + optionsFile + '\'' +
",\n\tmode='" + mode + '\'' +
",\n\tsentryDsn='" + sentryDsn + '\'' +
Expand Down

0 comments on commit 3087647

Please sign in to comment.