Skip to content

Commit 5c593b1

Browse files
authored
Merge pull request #121 from utPLSQL/feature/simple_logging
Feature/simple logging
2 parents e944707 + 2578f8a commit 5c593b1

12 files changed

+402
-58
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ The file tnsnames.ora must contain valid TNS entries.
123123
-exclude=pckg_list - Comma-separated object list to exclude from the coverage report.
124124
Format: [schema.]package[,[schema.]package ...].
125125
See coverage reporting options in framework documentation.
126+
127+
-q - Does not output the informational messages normally printed to console.
128+
Default: false
129+
130+
-d - Outputs a load of debug information to console
131+
Default: false
126132
```
127133

128134
Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter.

pom.xml

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,42 @@
2020
</properties>
2121

2222
<dependencies>
23-
<dependency>
24-
<groupId>org.utplsql</groupId>
25-
<artifactId>java-api</artifactId>
26-
<version>3.1.2</version>
27-
<scope>compile</scope>
28-
<exclusions>
29-
<exclusion>
30-
<groupId>com.oracle.jdbc</groupId>
31-
<artifactId>ucp</artifactId>
32-
</exclusion>
33-
</exclusions>
34-
</dependency>
35-
<dependency>
36-
<groupId>com.beust</groupId>
37-
<artifactId>jcommander</artifactId>
38-
<version>1.72</version>
39-
<scope>compile</scope>
40-
</dependency>
41-
<dependency>
42-
<groupId>com.zaxxer</groupId>
43-
<artifactId>HikariCP</artifactId>
44-
<version>2.7.2</version>
45-
<scope>compile</scope>
46-
</dependency>
4723
<dependency>
48-
<groupId>org.slf4j</groupId>
49-
<artifactId>slf4j-nop</artifactId>
50-
<version>1.7.25</version>
24+
<groupId>org.utplsql</groupId>
25+
<artifactId>java-api</artifactId>
26+
<version>3.1.3-SNAPSHOT</version>
5127
<scope>compile</scope>
28+
<exclusions>
29+
<exclusion>
30+
<groupId>com.oracle.jdbc</groupId>
31+
<artifactId>ucp</artifactId>
32+
</exclusion>
33+
</exclusions>
5234
</dependency>
35+
<dependency>
36+
<groupId>com.beust</groupId>
37+
<artifactId>jcommander</artifactId>
38+
<version>1.72</version>
39+
<scope>compile</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.zaxxer</groupId>
43+
<artifactId>HikariCP</artifactId>
44+
<version>2.7.2</version>
45+
<scope>compile</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>javax.xml.bind</groupId>
49+
<artifactId>jaxb-api</artifactId>
50+
<version>2.3.0</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>ch.qos.logback</groupId>
54+
<artifactId>logback-classic</artifactId>
55+
<version>1.2.3</version>
56+
</dependency>
57+
58+
<!-- Test -->
5359
<dependency>
5460
<groupId>org.junit.jupiter</groupId>
5561
<artifactId>junit-jupiter-api</artifactId>
@@ -62,11 +68,7 @@
6268
<version>${junit.jupiter.version}</version>
6369
<scope>test</scope>
6470
</dependency>
65-
<dependency>
66-
<groupId>javax.xml.bind</groupId>
67-
<artifactId>jaxb-api</artifactId>
68-
<version>2.3.0</version>
69-
</dependency>
71+
7072
</dependencies>
7173

7274
<build>

src/main/java/org/utplsql/cli/Cli.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public static void main(String[] args) {
1717
}
1818

1919
static int runWithExitCode( String[] args ) {
20+
21+
LoggerConfiguration.configure(LoggerConfiguration.ConfigLevel.NONE);
2022
LocaleInitializer.initLocale();
2123

2224
JCommander jc = new JCommander();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.utplsql.cli;
2+
3+
import ch.qos.logback.classic.Level;
4+
import ch.qos.logback.classic.Logger;
5+
import ch.qos.logback.classic.LoggerContext;
6+
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
7+
import ch.qos.logback.classic.spi.ILoggingEvent;
8+
import ch.qos.logback.core.ConsoleAppender;
9+
import com.zaxxer.hikari.HikariDataSource;
10+
import org.slf4j.LoggerFactory;
11+
12+
class LoggerConfiguration {
13+
14+
public enum ConfigLevel {
15+
BASIC, NONE, DEBUG
16+
}
17+
18+
private LoggerConfiguration() {
19+
throw new UnsupportedOperationException();
20+
}
21+
22+
static void configure(ConfigLevel level) {
23+
switch ( level ) {
24+
case BASIC:
25+
configureInfo();
26+
break;
27+
case NONE:
28+
configureSilent();
29+
break;
30+
case DEBUG:
31+
configureDebug();
32+
break;
33+
}
34+
}
35+
36+
private static void configureSilent() {
37+
setRootLoggerLevel(Level.OFF);
38+
}
39+
40+
private static void configureInfo() {
41+
setRootLoggerLevel(Level.INFO);
42+
muteHikariLogger();
43+
setSingleConsoleAppenderWithLayout("%msg%n");
44+
}
45+
46+
private static void configureDebug() {
47+
setRootLoggerLevel(Level.DEBUG);
48+
setSingleConsoleAppenderWithLayout("%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n");
49+
}
50+
51+
private static void setRootLoggerLevel( Level level ) {
52+
Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
53+
root.setLevel(level);
54+
}
55+
56+
private static void muteHikariLogger() {
57+
((Logger) LoggerFactory.getLogger(HikariDataSource.class)).setLevel(Level.OFF);
58+
}
59+
60+
private static void setSingleConsoleAppenderWithLayout( String patternLayout ) {
61+
Logger logger = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
62+
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
63+
64+
PatternLayoutEncoder ple = new PatternLayoutEncoder();
65+
ple.setPattern(patternLayout);
66+
67+
ple.setContext(lc);
68+
ple.start();
69+
70+
ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
71+
consoleAppender.setEncoder(ple);
72+
consoleAppender.setContext(lc);
73+
consoleAppender.start();
74+
75+
logger.detachAndStopAllAppenders();
76+
logger.setAdditive(false);
77+
logger.addAppender(consoleAppender);
78+
}
79+
}

src/main/java/org/utplsql/cli/ReportersCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public int run() {
4747
}
4848
catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | IllegalArgumentException e ) {
4949
System.out.println(e.getMessage());
50+
return 1;
5051
}
5152
catch (Exception e) {
5253
e.printStackTrace();

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
import com.beust.jcommander.Parameter;
44
import com.beust.jcommander.Parameters;
5-
import org.utplsql.api.FileMapperOptions;
6-
import org.utplsql.api.KeyValuePair;
7-
import org.utplsql.api.TestRunner;
8-
import org.utplsql.api.Version;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.utplsql.api.*;
98
import org.utplsql.api.compatibility.CompatibilityProxy;
9+
import org.utplsql.api.db.DefaultDatabaseInformation;
1010
import org.utplsql.api.exception.DatabaseNotCompatibleException;
1111
import org.utplsql.api.exception.SomeTestsFailedException;
1212
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
1313
import org.utplsql.api.reporter.Reporter;
1414
import org.utplsql.api.reporter.ReporterFactory;
1515
import org.utplsql.cli.exception.DatabaseConnectionFailed;
16+
import org.utplsql.cli.log.StringBlockFormatter;
1617

1718
import javax.sql.DataSource;
1819
import java.io.File;
@@ -34,6 +35,8 @@
3435
@Parameters(separators = "=", commandDescription = "run tests")
3536
public class RunCommand implements ICommand {
3637

38+
private static final Logger logger = LoggerFactory.getLogger(RunCommand.class);
39+
3740
@Parameter(
3841
required = true,
3942
converter = ConnectionInfo.ConnectionStringConverter.class,
@@ -99,6 +102,15 @@ public class RunCommand implements ICommand {
99102
)
100103
private String excludeObjects = null;
101104

105+
@Parameter(
106+
names = {"-q", "--quiet"},
107+
description = "Does not output the informational messages normally printed to console")
108+
private boolean logSilent = false;
109+
110+
@Parameter(
111+
names = {"-d", "--debug"},
112+
description = "Outputs a load of debug information to console")
113+
private boolean logDebug = false;
102114

103115
private CompatibilityProxy compatibilityProxy;
104116
private ReporterFactory reporterFactory;
@@ -112,7 +124,22 @@ public List<String> getTestPaths() {
112124
return testPaths;
113125
}
114126

127+
void init() {
128+
129+
LoggerConfiguration.ConfigLevel level = LoggerConfiguration.ConfigLevel.BASIC;
130+
if ( logSilent ) {
131+
level = LoggerConfiguration.ConfigLevel.NONE;
132+
}
133+
else if ( logDebug ) {
134+
level = LoggerConfiguration.ConfigLevel.DEBUG;
135+
}
136+
137+
LoggerConfiguration.configure(level);
138+
}
139+
115140
public int run() {
141+
init();
142+
outputMainInformation();
116143

117144
try {
118145

@@ -148,25 +175,8 @@ public int run() {
148175

149176
final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters() + 1);
150177

151-
// Do the reporters initialization, so we can use the id to run and gather results.
152-
try (Connection conn = dataSource.getConnection()) {
153-
154-
// Check if orai18n exists if database version is 11g
155-
RunCommandChecker.checkOracleI18nExists(conn);
156-
157-
// First of all do a compatibility check and fail-fast
158-
compatibilityProxy = checkFrameworkCompatibility(conn);
159-
reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy);
160-
161-
reporterList = getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy);
162-
163-
} catch (SQLException e) {
164-
if (e.getErrorCode() == 1017 || e.getErrorCode() == 12514) {
165-
throw new DatabaseConnectionFailed(e);
166-
} else {
167-
throw e;
168-
}
169-
}
178+
initDatabase(dataSource);
179+
reporterList = initReporters(dataSource);
170180

171181
// Output a message if --failureExitCode is set but database framework is not capable of
172182
String msg = RunCommandChecker.getCheckFailOnErrorMessage(failureExitCode, compatibilityProxy.getDatabaseVersion());
@@ -190,6 +200,8 @@ public int run() {
190200
.includeObjects(finalIncludeObjectsList)
191201
.excludeObjects(finalExcludeObjectsList);
192202

203+
logger.info("Running tests now.");
204+
logger.info("--------------------------------------");
193205
testRunner.run(conn);
194206
} catch (SomeTestsFailedException e) {
195207
returnCode[0] = this.failureExitCode;
@@ -205,6 +217,10 @@ public int run() {
205217

206218
executorService.shutdown();
207219
executorService.awaitTermination(60, TimeUnit.MINUTES);
220+
221+
logger.info("--------------------------------------");
222+
logger.info("All tests done.");
223+
208224
return returnCode[0];
209225
}
210226
catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed e ) {
@@ -221,6 +237,49 @@ public String getCommand() {
221237
}
222238

223239

240+
private void outputMainInformation() {
241+
242+
StringBlockFormatter formatter = new StringBlockFormatter("utPLCSL cli");
243+
formatter.appendLine(CliVersionInfo.getInfo());
244+
formatter.appendLine(JavaApiVersionInfo.getInfo());
245+
formatter.appendLine("Java-Version: " + System.getProperty("java.version"));
246+
formatter.appendLine("ORACLE_HOME: " + EnvironmentVariableUtil.getEnvValue("ORACLE_HOME"));
247+
formatter.appendLine("NLS_LANG: " + EnvironmentVariableUtil.getEnvValue("NLS_LANG"));
248+
formatter.appendLine("");
249+
formatter.appendLine("Thanks for testing!");
250+
251+
logger.info(formatter.toString());
252+
logger.info("");
253+
}
254+
255+
private void initDatabase(DataSource dataSource) throws SQLException {
256+
try (Connection conn = dataSource.getConnection()) {
257+
258+
// Check if orai18n exists if database version is 11g
259+
RunCommandChecker.checkOracleI18nExists(conn);
260+
261+
// First of all do a compatibility check and fail-fast
262+
compatibilityProxy = checkFrameworkCompatibility(conn);
263+
264+
logger.info("Successfully connected to database. UtPLSQL core: {}", compatibilityProxy.getDatabaseVersion());
265+
logger.info("Oracle-Version: {}", new DefaultDatabaseInformation().getOracleVersion(conn));
266+
}
267+
catch (SQLException e) {
268+
if (e.getErrorCode() == 1017 || e.getErrorCode() == 12514) {
269+
throw new DatabaseConnectionFailed(e);
270+
} else {
271+
throw e;
272+
}
273+
}
274+
}
275+
276+
private List<Reporter> initReporters(DataSource dataSource) throws SQLException {
277+
try (Connection conn = dataSource.getConnection()) {
278+
reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy);
279+
return getReporterManager().initReporters(conn, reporterFactory, compatibilityProxy);
280+
}
281+
}
282+
224283
/** Returns FileMapperOptions for the first item of a given param list in a baseDir
225284
*
226285
* @param pathParams

0 commit comments

Comments
 (0)