Skip to content

Commit

Permalink
reverted to java.util.logging for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Mar 3, 2021
1 parent 48003a7 commit d061cdf
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 51 deletions.
10 changes: 3 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,12 @@ subprojects {
jar {
enabled = true
}
dependencyManagement {
imports {
mavenBom 'org.apache.logging.log4j:log4j-bom:2.14.0'
}
}
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.springframework.batch:spring-batch-test'
testImplementation 'org.hsqldb:hsqldb'
}
Expand All @@ -71,7 +66,8 @@ subprojects {
}
dependencies {
implementation project(':core')
testImplementation project(':test')
testImplementation project(':test')
implementation 'org.slf4j:slf4j-jdk14'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import picocli.CommandLine;
import picocli.CommandLine.Command;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -28,6 +29,9 @@ public abstract class AbstractFileImportCommand<T> extends AbstractImportCommand
@Override
protected Flow flow() throws Exception {
String[] expandedFiles = FileUtils.expand(files);
if (expandedFiles.length == 0) {
throw new FileNotFoundException("File not found: " + String.join(", ", files));
}
List<Step> steps = new ArrayList<>();
for (String file : expandedFiles) {
FileType fileType = FileUtils.fileType(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static boolean isGcs(String file) {
}

public static Resource inputResource(String file, FileOptions options) throws IOException {
if (FileUtils.isConsole(file)) {
if (isConsole(file)) {
return new StandardInputResource();
}
Resource resource = resource(file, options, true);
Expand Down Expand Up @@ -237,15 +237,17 @@ public static <T> XmlItemReader<T> xmlReader(Resource resource, Class<T> clazz)
public static String[] expand(String... files) throws IOException {
List<String> expandedFiles = new ArrayList<>();
for (String file : files) {
if (FileUtils.isFile(file)) {
if (isFile(file)) {
Path path = Paths.get(file);
if (Files.exists(path)) {
expandedFiles.add(file);
} else {
// Path might be glob pattern
Path parent = path.getParent();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(parent, path.getFileName().toString())) {
stream.forEach(p -> expandedFiles.add(p.toString()));
if (parent != null) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(parent, path.getFileName().toString())) {
stream.forEach(p -> expandedFiles.add(p.toString()));
}
}
}
} else {
Expand Down
6 changes: 1 addition & 5 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
dependencies {
api 'com.redislabs:spring-batch-redis:2.8.2'
api 'org.springframework.batch:spring-batch-core'
api 'org.slf4j:slf4j-api'
compile 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'me.tongfei:progressbar:0.9.0'
implementation 'org.apache.commons:commons-lang3'
implementation 'org.latencyutils:LatencyUtils:2.0.3'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'
api group: 'info.picocli', name: 'picocli', version: picocli_version
annotationProcessor group: 'info.picocli', name: 'picocli-codegen', version: picocli_version
implementation 'org.apache.logging.log4j:log4j-api'
implementation 'org.apache.logging.log4j:log4j-core'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl'
implementation 'org.slf4j:jcl-over-slf4j'

}

jar {
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/com/redislabs/riot/AbstractTaskCommand.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.redislabs.riot;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.job.flow.support.SimpleFlow;
import org.springframework.batch.item.redis.support.JobFactory;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import picocli.CommandLine;

@Slf4j
@CommandLine.Command
public abstract class AbstractTaskCommand extends RiotCommand {

Expand Down Expand Up @@ -49,10 +50,15 @@ protected void execute() throws Exception {
}

private Job job() throws Exception {
JobBuilder jobBuilder = jobFactory.job(ClassUtils.getShortName(getClass()));
return jobBuilder.start(flow()).build().build();
return jobFactory.job(ClassUtils.getShortName(getClass())).start(flow()).build().build();
}

/**
* For unit-testing
*
* @return
* @throws Exception
*/
public JobExecution executeAsync() throws Exception {
afterPropertiesSet();
JobExecution execution = jobFactory.getAsyncLauncher().run(job(), new JobParameters());
Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/com/redislabs/riot/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import picocli.CommandLine;

import java.util.concurrent.Callable;

@CommandLine.Command(usageHelpAutoWidth = true)
public class HelpCommand implements Runnable {
public class HelpCommand implements Callable<Integer> {

@CommandLine.Option(names = "--help", usageHelp = true, description = "Show this help message and exit")
private boolean helpRequested;

@Override
public void run() {
public Integer call() throws Exception {
CommandLine.usage(this, System.out);
return 0;
}

}
36 changes: 36 additions & 0 deletions core/src/main/java/com/redislabs/riot/OneLineLogFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.redislabs.riot;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class OneLineLogFormat extends Formatter {

private final DateTimeFormatter d = new DateTimeFormatterBuilder().appendValue(ChronoField.HOUR_OF_DAY, 2).appendLiteral(':').appendValue(ChronoField.MINUTE_OF_HOUR, 2).optionalStart().appendLiteral(':').appendValue(ChronoField.SECOND_OF_MINUTE, 2).optionalStart().appendFraction(ChronoField.NANO_OF_SECOND, 3, 3, true).toFormatter();
private final ZoneId offset = ZoneOffset.systemDefault();

@Override
public String format(LogRecord record) {
String message = formatMessage(record);
ZonedDateTime time = Instant.ofEpochMilli(record.getMillis()).atZone(offset);
if (record.getThrown() == null) {
return String.format("%s %s %s\t: %s%n", time.format(d), record.getLevel().getLocalizedName(), record.getLoggerName(), message);
}
return String.format("%s %s %s\t: %s%n%s%n", time.format(d), record.getLevel().getLocalizedName(), record.getLoggerName(), message, stackTrace(record));
}

private String stackTrace(LogRecord record) {
StringWriter sw = new StringWriter(4096);
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
return sw.toString();
}
}
114 changes: 97 additions & 17 deletions core/src/main/java/com/redislabs/riot/RiotApp.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.redislabs.riot;

import com.redislabs.riot.redis.AbstractRedisCommand;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.JdkLoggerFactory;
import lombok.Getter;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import picocli.CommandLine;
import picocli.CommandLine.*;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

@Command(usageHelpAutoWidth = true, sortOptions = false, versionProvider = ManifestVersionProvider.class, subcommands = HiddenGenerateCompletion.class, abbreviateSynopsis = true)
public class RiotApp implements Runnable {

private static final String ROOT_LOGGER = "";

@Option(names = {"-H", "--help"}, usageHelp = true, description = "Show this help message and exit.")
private boolean helpRequested;
@Option(names = {"-V", "--version"}, versionHelp = true, description = "Print version information and exit.")
Expand All @@ -24,31 +32,103 @@ public class RiotApp implements Runnable {
private boolean info;
@Option(names = {"-d", "--debug"}, description = "Log in debug mode (includes normal stacktrace).")
private boolean debug;
@Option(names = {"-S", "--stacktrace"}, description = "Print out the stacktrace for all exceptions.")
private boolean stacktrace;
@Getter
@ArgGroup(heading = "Redis connection options%n", exclusive = false)
private RedisOptions redisOptions = new RedisOptions();

public int execute(String... args) {
CommandLine commandLine = commandLine();
ParseResult[] parseResult = new ParseResult[1];
try {
CommandLine commandLine = commandLine();
ParseResult parseResult = parse(commandLine, args);
Configurator.setRootLevel(logLevel());
return commandLine.getExecutionStrategy().execute(parseResult);
} catch (PicocliException e) {
System.err.println(e.getMessage());
return 1;
parseResult[0] = parse(commandLine, args);
initializeLogging();
return commandLine.getExecutionStrategy().execute(parseResult[0]);
} catch (ParameterException ex) {
try {
return commandLine.getParameterExceptionHandler().handleParseException(ex, args);
} catch (Exception ex2) {
return handleUnhandled(ex2, ex.getCommandLine(), ex.getCommandLine().getCommandSpec().exitCodeOnInvalidInput());
}
} catch (ExecutionException ex) {
try {
Exception cause = ex.getCause() instanceof Exception ? (Exception) ex.getCause() : ex;
return commandLine.getExecutionExceptionHandler().handleExecutionException(cause, ex.getCommandLine(), parseResult[0]);
} catch (Exception ex2) {
return handleUnhandled(ex2, ex.getCommandLine(), ex.getCommandLine().getCommandSpec().exitCodeOnExecutionException());
}
} catch (Exception ex) {
return handleUnhandled(ex, commandLine, commandLine.getCommandSpec().exitCodeOnExecutionException());
}
}

private static String throwableToColorString(Throwable t, Help.ColorScheme existingColorScheme) {
Help.ColorScheme colorScheme = new Help.ColorScheme.Builder(existingColorScheme).applySystemProperties().build();
StringWriter stringWriter = new ColoredStackTraceWriter(colorScheme);
t.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}

static class ColoredStackTraceWriter extends StringWriter {
Help.ColorScheme colorScheme;

public ColoredStackTraceWriter(Help.ColorScheme colorScheme) { this.colorScheme = colorScheme; }

@Override
public void write(String str, int off, int len) {
List<Help.Ansi.IStyle> styles = str.startsWith("\t") ? colorScheme.stackTraceStyles() : colorScheme.errorStyles();
super.write(colorScheme.apply(str.substring(off, len), styles).toString());
}
}

private static int handleUnhandled(Exception ex, CommandLine cmd, int defaultExitCode) {
cmd.getErr().print(throwableToColorString(ex, cmd.getColorScheme()));
cmd.getErr().flush();
return mappedExitCode(ex, cmd.getExitCodeExceptionMapper(), defaultExitCode);
}

private static int mappedExitCode(Throwable t, IExitCodeExceptionMapper mapper, int defaultExitCode) {
try {
return (mapper != null) ? mapper.getExitCode(t) : defaultExitCode;
} catch (Exception ex) {
ex.printStackTrace();
return defaultExitCode;
}
}

private void initializeLogging() {
InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
LogManager.getLogManager().reset();
Logger activeLogger = Logger.getLogger(ROOT_LOGGER);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(java.util.logging.Level.ALL);
handler.setFormatter(new OneLineLogFormat());
activeLogger.addHandler(handler);
Logger.getLogger(ROOT_LOGGER).setLevel(loggingLevel());
if (debug) {
Logger.getLogger("io.lettuce").setLevel(java.util.logging.Level.INFO);
Logger.getLogger("io.netty").setLevel(java.util.logging.Level.INFO);
}
}

public CommandLine commandLine() {
CommandLine commandLine = new CommandLine(this);
commandLine.setExecutionExceptionHandler(new PrintExceptionMessageHandler());
registerConverters(commandLine);
commandLine.setCaseInsensitiveEnumValuesAllowed(true);
return commandLine;
}

class PrintExceptionMessageHandler implements IExecutionExceptionHandler {

@Override
public int handleExecutionException(Exception ex, CommandLine cmd, ParseResult parseResult) {
// bold red error message
cmd.getErr().println(cmd.getColorScheme().errorText(ex.getMessage()));
return cmd.getExitCodeExceptionMapper() != null ? cmd.getExitCodeExceptionMapper().getExitCode(ex) : cmd.getCommandSpec().exitCodeOnExecutionException();
}

}

@SuppressWarnings({"rawtypes", "unchecked"})
public ParseResult parse(CommandLine commandLine, String[] args) {
ParseResult parseResult = commandLine.parseArgs(args);
Expand Down Expand Up @@ -80,20 +160,20 @@ public void run() {
CommandLine.usage(this, System.out);
}

private Level logLevel() {
private java.util.logging.Level loggingLevel() {
if (debug) {
return Level.DEBUG;
return java.util.logging.Level.FINE;
}
if (info) {
return Level.INFO;
return java.util.logging.Level.INFO;
}
if (warn) {
return Level.WARN;
return java.util.logging.Level.WARNING;
}
if (quiet) {
return Level.FATAL;
return java.util.logging.Level.OFF;
}
return Level.ERROR;
return Level.SEVERE;
}

}
15 changes: 3 additions & 12 deletions core/src/main/java/com/redislabs/riot/RiotCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.support.ConnectionPoolSupport;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;
Expand Down Expand Up @@ -41,17 +38,11 @@ protected Duration getCommandTimeout() {
}

@Override
public void run() {
try {
afterPropertiesSet();
} catch (Exception e) {
LoggerFactory.getLogger(RiotCommand.class).error("Could not initialize command", e);
return;
}
public Integer call() throws Exception {
afterPropertiesSet();
try {
execute();
} catch (Exception e) {
LoggerFactory.getLogger(RiotCommand.class).error("Could not execute command", e);
return 0;
} finally {
shutdown();
}
Expand Down
Loading

0 comments on commit d061cdf

Please sign in to comment.