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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -11,6 +11,7 @@
import de.jplag.JPlag;
import de.jplag.JPlagResult;
import de.jplag.cli.logger.CliProgressBarProvider;
import de.jplag.cli.logger.CollectedLogger;
import de.jplag.cli.logger.CollectedLoggerFactory;
import de.jplag.cli.picocli.CliInputHandler;
import de.jplag.exceptions.ExitException;
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()) {
CollectedLogger.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,15 +15,21 @@
* 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(CollectedLogger.getLogLevel())) {
if (type.isIdleBar()) {
IdleBar idleBar = new IdleBar(type.getDefaultText());
idleBar.start();
return idleBar;
}
me.tongfei.progressbar.ProgressBar progressBar = new ProgressBarBuilder().setTaskName(type.getDefaultText()).setInitialMax(totalSteps)
.setStyle(ProgressBarStyle.ASCII).build();
return new TongfeiProgressBar(progressBar);
} else {
return new VoidProgressBar();
}
me.tongfei.progressbar.ProgressBar progressBar = new ProgressBarBuilder().setTaskName(type.getDefaultText()).setInitialMax(totalSteps)
.setStyle(ProgressBarStyle.ASCII).build();
return new TongfeiProgressBar(progressBar);
}
}
23 changes: 10 additions & 13 deletions cli/src/main/java/de/jplag/cli/logger/CollectedLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class CollectedLogger extends AbstractLogger {
private static final Level LOG_LEVEL_FOR_EXTERNAL_LIBRARIES = Level.ERROR;
private static final int MAXIMUM_MESSAGE_LENGTH = 32;
private static final PrintStream TARGET_STREAM = System.out;
private static Level currentLogLevel = Level.INFO;

private final transient SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss_SSS");
private final ConcurrentLinkedDeque<LogEntry> allErrors = new ConcurrentLinkedDeque<>();
private final Level currentLogLevel;

/**
* Indicator whether finalization is in progress.
Expand All @@ -33,21 +33,10 @@ public class CollectedLogger extends AbstractLogger {

/**
* Creates a logger with a specific name and level.
* @param currentLogLevel is the current log level.
* @param name is the name of the logger.
*/
CollectedLogger(Level currentLogLevel, String name) {
this.currentLogLevel = currentLogLevel;
this.name = name;
}

/**
* Creates a logger with a specific name and {@link Level.INFO}.
* @param currentLogLevel is the current log level.
* @param name is the name of the logger.
*/
CollectedLogger(String name) {
this(Level.INFO, name);
this.name = name;
}

@Override
Expand Down Expand Up @@ -165,4 +154,12 @@ private void printLogEntry(LogEntry entry) {
}
TARGET_STREAM.flush();
}

public static Level getLogLevel() {
return currentLogLevel;
}

public static void setLogLevel(Level logLevel) {
currentLogLevel = logLevel;
}
}
18 changes: 18 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,18 @@
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) {
// does nothing see class description
}

@Override
public void dispose() {
// does nothing see class description
}
}
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
49 changes: 49 additions & 0 deletions cli/src/test/java/de/jplag/cli/logger/VoidProgressBarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.jplag.cli.logger;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.event.Level;

import de.jplag.logging.ProgressBar;
import de.jplag.logging.ProgressBarType;

class VoidProgressBarTest {
@Test
void testVoidProgressBarBehaviour() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream originalOutput = System.out;
System.setOut(new PrintStream(outputStream));

VoidProgressBar progressBar = new VoidProgressBar();
progressBar.step();
progressBar.step(2);
progressBar.dispose();

System.setOut(originalOutput);

Assertions.assertEquals("", outputStream.toString());
}

@ParameterizedTest
@MethodSource("getRelevantLogLevels")
void testVoidProgressBarCreated(Level logLevel) {
Level originalLogLevel = CollectedLogger.getLogLevel();
CollectedLogger.setLogLevel(logLevel);

ProgressBar progressBar = new CliProgressBarProvider().initProgressBar(ProgressBarType.CLUSTERING, 10);
progressBar.dispose();

Assertions.assertInstanceOf(VoidProgressBar.class, progressBar);

CollectedLogger.setLogLevel(originalLogLevel);
}

public static Level[] getRelevantLogLevels() {
return new Level[] {Level.TRACE, Level.DEBUG, Level.ERROR, Level.WARN};
}
}
8 changes: 1 addition & 7 deletions report-viewer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.