Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add quiet and debug flags and also a simple debug configuration
  • Loading branch information
pesse committed Feb 7, 2019
commit 8bdaf0f9edde93f269ffad73127d7e0b337fd686
43 changes: 33 additions & 10 deletions src/main/java/org/utplsql/cli/LoggerConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,55 @@
import org.slf4j.LoggerFactory;
import org.utplsql.api.TestRunner;

public class LoggerConfiguration {
class LoggerConfiguration {

Comment thread
pesse marked this conversation as resolved.
static void configureDefault() {
static void configure(boolean silent, boolean debug) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use two parameters here? Are all 4 combination of their values valid? Maybe we should accept 3-valued enum?

SILENT, DEFAULT, DEBUG

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason is to make it easy to just pass the parameters of the RunCommand to the LoggerConfigurator.
Enum would increase clarity of the LoggerConfigurator class taken alone but not its usage. This way it's clear that logging is depenent on two flags.
If we can increase clarity on the fact that silent is superior to debug, I'd appreciate it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should. If non flags are passed then default logging is enabled, if silent - non, if debug - debug. And then we end up with 3 levels, set on application level and non of other classes should think about it. Classes just log using appropriate log level (logger.info, logger.debug).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I get you completely right.
You'd like to introduce a 3-leveled ENUM and shift the logic which configuration to take to the RunCommand?
The classes themselves already use appropriate log level, the purpose of the configuration is to either hide or show the log information.

if ( silent )
configureSilent();
else if ( debug )
configureDebug();
else
configureDefault();
}

private static void configureSilent() {
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.INFO);
root.setLevel(Level.OFF);
}

Logger hikariLogger = (Logger) LoggerFactory.getLogger(HikariDataSource.class);
hikariLogger.setLevel(Level.OFF);
private static void configureDefault() {
Comment thread
pesse marked this conversation as resolved.
Outdated
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.INFO);

((Logger) LoggerFactory.getLogger(HikariDataSource.class)).setLevel(Level.OFF);
((Logger) LoggerFactory.getLogger(TestRunner.class)).setLevel(Level.ERROR);

setSingleConsoleAppenderWithLayout(root, "%msg%n");
}

private static void configureDebug() {
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.DEBUG);

setSingleConsoleAppenderWithLayout(root, "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n");
}

private static void setSingleConsoleAppenderWithLayout( Logger logger, String patternLayout ) {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

PatternLayoutEncoder ple = new PatternLayoutEncoder();
ple.setPattern("%msg%n");
ple.setPattern(patternLayout);

ple.setContext(lc);
ple.start();

ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<ILoggingEvent>();
ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
consoleAppender.setEncoder(ple);
consoleAppender.setContext(lc);
consoleAppender.start();

root.detachAndStopAllAppenders();
root.setAdditive(false);
root.addAppender(consoleAppender);
logger.detachAndStopAllAppenders();
logger.setAdditive(false);
logger.addAppender(consoleAppender);
}
}
17 changes: 14 additions & 3 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ public class RunCommand implements ICommand {
)
private String excludeObjects = null;

@Parameter(
names = {"-q", "--quiet"},
description = "Does not output the informational messages normally printed to console")
private boolean logSilent = false;

@Parameter(
names = {"-d", "--debug"},
description = "Outputs a load of debug information to console")
private boolean logDebug = false;

private CompatibilityProxy compatibilityProxy;
private ReporterFactory reporterFactory;
Expand All @@ -115,10 +124,12 @@ public List<String> getTestPaths() {
return testPaths;
}

public int run() {

LoggerConfiguration.configureDefault();
public void init() {
LoggerConfiguration.configure(logSilent, logDebug);
}

public int run() {
init();
outputMainInformation();

try {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/utplsql/cli/RunCommandIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public void run_Default() throws Exception {
else
assertEquals(0, result);
}

@Test
public void run_Debug() throws Exception {

int result = TestHelper.runApp("run",
TestHelper.getConnectionString(),
"--debug");

assertEquals(1, result);
}

@Test
public void run_MultipleReporters() throws Exception {

Expand Down
39 changes: 39 additions & 0 deletions src/test/java/org/utplsql/cli/RunCommandLogLevelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.utplsql.cli;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RunCommandLogLevelTest {

private Logger getRootLogger() {
return (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
}

@Test
void defaultIsInfo() {
TestHelper.createRunCommand(TestHelper.getConnectionString())
.init();

assertEquals(Level.INFO, getRootLogger().getLevel());
}

@Test
void silentModeSetsLoggerToOff() {
TestHelper.createRunCommand(TestHelper.getConnectionString(), "-q")
.init();

assertEquals(Level.OFF, getRootLogger().getLevel());
}

@Test
void debugModeSetsLoggerToDebug() {
TestHelper.createRunCommand(TestHelper.getConnectionString(), "-d")
.init();

assertEquals(Level.DEBUG, getRootLogger().getLevel());
}
}