Skip to content

Commit

Permalink
Print stacktraces on error with debug level >= debug
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed Aug 18, 2021
1 parent 0910df6 commit e9f4f72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/src/main/java/de/atextor/owlcli/LoggingMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public boolean[] getVerbosity() {
return getTopLevelCommandLoggingMixin( mixee ).verbosity;
}

private Level calcLogLevel() {
public Level calcLogLevel() {
return switch ( getVerbosity().length ) {
case 0:
yield Level.WARN;
Expand Down
17 changes: 13 additions & 4 deletions cli/src/main/java/de/atextor/owlcli/OWLCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package de.atextor.owlcli;

import ch.qos.logback.classic.Level;
import io.vavr.collection.List;
import org.apache.jena.sys.JenaSystem;
import org.slf4j.Logger;
Expand All @@ -42,19 +43,27 @@ public class OWLCLI implements Runnable {

private static final Logger LOG = LoggerFactory.getLogger( OWLCLI.class );

private static void printError( final CommandLine commandLine, final Exception exception ) {
final Level logLevel = ( (LoggingMixin) commandLine.getMixins().values().iterator().next() ).calcLogLevel();
if ( logLevel.equals( Level.DEBUG ) || logLevel.equals( Level.TRACE ) ) {
LOG.debug( exception.getMessage(), exception );
} else {
final PrintWriter writer = commandLine.getErr();
writer.println( "Error: " + exception.getMessage() );
}
}

private static final CommandLine.IParameterExceptionHandler PARAMETER_EXCEPTION_HANDLER =
( exception, args ) -> {
final CommandLine cmd = exception.getCommandLine();
final PrintWriter writer = cmd.getErr();
writer.println( "Error: " + exception.getMessage() );
printError( cmd, exception );
cmd.getErr().println( cmd.getHelp().fullSynopsis() );
return 1;
};

private static final CommandLine.IExecutionExceptionHandler EXECUTION_EXCEPTION_HANDLER =
( exception, commandLine, parseResult ) -> {
final PrintWriter writer = commandLine.getErr();
writer.println( "Error: " + exception.getMessage() );
printError( commandLine, exception );
return 1;
};

Expand Down

0 comments on commit e9f4f72

Please sign in to comment.