Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added command line option for log-level #1751

Merged
merged 8 commits into from
Jun 27, 2024
2 changes: 2 additions & 0 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.jplag.JPlagResult;
import de.jplag.cli.logger.CliProgressBarProvider;
import de.jplag.cli.logger.CollectedLoggerFactory;
import de.jplag.cli.logger.JPlagLoggerBase;
import de.jplag.cli.picocli.CliInputHandler;
import de.jplag.exceptions.ExitException;
import de.jplag.logging.ProgressBarLogger;
Expand Down Expand Up @@ -50,6 +51,7 @@ public void executeCli() throws ExitException, IOException {
logger.debug("Your version of JPlag is {}", JPlag.JPLAG_VERSION);

if (!this.inputHandler.parse()) {
JPlagLoggerBase.setLogLevel(this.inputHandler.getCliOptions().advanced.logLevel);
ProgressBarLogger.setProgressBarProvider(new CliProgressBarProvider());

switch (this.inputHandler.getCliOptions().mode) {
Expand Down
24 changes: 17 additions & 7 deletions cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package de.jplag.cli.logger;

import java.util.Set;

import org.slf4j.event.Level;

import de.jplag.logging.ProgressBar;
import de.jplag.logging.ProgressBarProvider;
import de.jplag.logging.ProgressBarType;
Expand All @@ -11,16 +15,22 @@
* A ProgressBar provider, that used the tongfei progress bar library underneath, to show progress bars on the cli.
*/
public class CliProgressBarProvider implements ProgressBarProvider {
private static final Set<Level> allowedLogLevels = Set.of(Level.INFO);

@Override
public ProgressBar initProgressBar(ProgressBarType type, int totalSteps) {
if (type.isIdleBar()) {
IdleBar idleBar = new IdleBar(type.getDefaultText());
idleBar.start();
return idleBar;
if (allowedLogLevels.contains(JPlagLoggerBase.getLogLevel())) {
if (type.isIdleBar()) {
IdleBar idleBar = new IdleBar(type.getDefaultText());
idleBar.start();
return idleBar;
} else {
me.tongfei.progressbar.ProgressBar progressBar = new ProgressBarBuilder().setTaskName(type.getDefaultText()).setInitialMax(totalSteps)
.setStyle(ProgressBarStyle.ASCII).build();
return new TongfeiProgressBar(progressBar);
}
} else {
me.tongfei.progressbar.ProgressBar progressBar = new ProgressBarBuilder().setTaskName(type.getDefaultText()).setInitialMax(totalSteps)
.setStyle(ProgressBarStyle.ASCII).build();
return new TongfeiProgressBar(progressBar);
return new VoidProgressBar();
}
}
}
2 changes: 1 addition & 1 deletion cli/src/main/java/de/jplag/cli/logger/CollectedLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class CollectedLogger extends JPlagLoggerBase {
private final ConcurrentLinkedDeque<LogEntry> allErrors = new ConcurrentLinkedDeque<>();

public CollectedLogger(String name) {
super(LOG_LEVEL_INFO, name);
super(name);
}

@Override
Expand Down
16 changes: 11 additions & 5 deletions cli/src/main/java/de/jplag/cli/logger/JPlagLoggerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ public abstract class JPlagLoggerBase extends AbstractLogger {

private static final Level LOG_LEVEL_FOR_EXTERNAL_LIBRARIES = LOG_LEVEL_ERROR;

private final Level currentLogLevel;
private static Level currentLogLevel = LOG_LEVEL_INFO;

public static void setLogLevel(Level logLevel) {
currentLogLevel = logLevel;
}

public static Level getLogLevel() {
return currentLogLevel;
}

/**
* @param currentLogLevel The current log level
* @param name The name of the logger
*/
protected JPlagLoggerBase(Level currentLogLevel, String name) {
this.currentLogLevel = currentLogLevel;
protected JPlagLoggerBase(String name) {
this.name = name;
}

Expand Down Expand Up @@ -78,7 +84,7 @@ public boolean isErrorEnabled(Marker marker) {
}

private boolean isLogLevelEnabled(Level logLevel) {
return logLevel.toInt() >= (isJPlagLog() ? this.currentLogLevel.toInt() : LOG_LEVEL_FOR_EXTERNAL_LIBRARIES.toInt());
return logLevel.toInt() >= (isJPlagLog() ? currentLogLevel.toInt() : LOG_LEVEL_FOR_EXTERNAL_LIBRARIES.toInt());
}

private boolean isJPlagLog() {
Expand Down
16 changes: 16 additions & 0 deletions cli/src/main/java/de/jplag/cli/logger/VoidProgressBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.jplag.cli.logger;

import de.jplag.logging.ProgressBar;

/**
* An empty {@link ProgressBar} implementation, used to hide the progress bar depending on the log level.
*/
public class VoidProgressBar implements ProgressBar {
@Override
public void step(int number) {
}

@Override
public void dispose() {
}
}
5 changes: 5 additions & 0 deletions cli/src/main/java/de/jplag/cli/options/CliOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;

import org.slf4j.event.Level;

import de.jplag.Language;
import de.jplag.clustering.ClusteringAlgorithm;
import de.jplag.clustering.ClusteringOptions;
Expand Down Expand Up @@ -101,6 +103,9 @@ public static class Advanced {

@Option(names = "--overwrite", description = "Existing result files will be overwritten.")
public boolean overwrite = false;

@Option(names = "--log-level", description = "Set the log level for the cli.")
public Level logLevel = Level.INFO;
}

public static class Clustering {
Expand Down