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

Tests | Add logging stream handler for tests #1027

Merged
merged 11 commits into from
May 3, 2019
1 change: 0 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* Various driver utilities.
*
*/

final class Util {
final static String SYSTEM_SPEC_VERSION = System.getProperty("java.specification.version");
final static char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package com.microsoft.sqlserver.testframework;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -15,6 +17,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -61,6 +64,12 @@ public abstract class AbstractTest {
private static boolean _isSqlAzure = false;
private static boolean _isSqlAzureDW = false;

/**
* Byte array containing logging output Content can be retrieved using toByteArray() or toString()
*/
public static ByteArrayOutputStream logOutputStream = null;
private static PrintStream logPrintStream = null;

/**
* This will take care of all initialization before running the Test Suite.
*
Expand Down Expand Up @@ -210,8 +219,18 @@ public static void teardown() throws Exception {
if (null != connection && !connection.isClosed()) {
connection.close();
}

if (null != logOutputStream) {
logOutputStream.close();
}

if (null != logPrintStream) {
logPrintStream.close();
}
} finally {
connection = null;
logOutputStream = null;
logPrintStream = null;
}
}

Expand Down Expand Up @@ -241,35 +260,44 @@ public static String getConfiguredProperty(String key, String defaultValue) {
public static void invokeLogging() {
Handler handler = null;

String enableLogging = getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING, Boolean.FALSE.toString());
// enable logging to stream by default for tests
String enableLogging = getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING, Boolean.TRUE.toString());

// If logging is not enable then return.
if (!Boolean.TRUE.toString().equalsIgnoreCase(enableLogging)) {
return;
}

String loggingHandler = getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING_HANDLER, "not_configured");
String loggingHandler = getConfiguredProperty(Constants.MSSQL_JDBC_LOGGING_HANDLER,
Constants.LOGGING_HANDLER_STREAM);

try {
if (Constants.LOGGING_HANDLER_CONSOLE.equalsIgnoreCase(loggingHandler)) {
handler = new ConsoleHandler();
handler.setFormatter(new SimpleFormatter());
} else if (Constants.LOGGING_HANDLER_FILE.equalsIgnoreCase(loggingHandler)) {
handler = new FileHandler(Constants.DEFAULT_DRIVER_LOG);
handler.setFormatter(new SimpleFormatter());
System.out.println("Look for Driver.log file in your classpath for detail logs");
} else if (Constants.LOGGING_HANDLER_STREAM.equalsIgnoreCase(loggingHandler)) {
logOutputStream = new ByteArrayOutputStream();
logPrintStream = new PrintStream(logOutputStream);
handler = new StreamHandler(logPrintStream, new SimpleFormatter());
}

if (handler != null) {
handler.setFormatter(new SimpleFormatter());
handler.setLevel(Level.FINEST);
Logger.getLogger(Constants.MSSQL_JDBC_LOGGING_HANDLER).addHandler(handler);
}
// By default, Loggers also send their output to their parent logger.
// Typically the root Logger is configured with a set of Handlers that essentially act as default handlers
// for all loggers.

/*
* By default, Loggers also send their output to their parent logger. Typically the root Logger is
* configured with a set of Handlers that essentially act as default handlers for all loggers.
*/
Logger logger = Logger.getLogger(Constants.MSSQL_JDBC_PACKAGE);
logger.setLevel(Level.FINEST);
} catch (Exception e) {
System.err.println("Some how could not invoke logging: " + e.getMessage());
System.err.println("Could not invoke logging: " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class Constants {

public static final String LOGGING_HANDLER_FILE = "file";
public static final String LOGGING_HANDLER_CONSOLE = "console";
public static final String LOGGING_HANDLER_STREAM = "stream";

public final static int ENGINE_EDITION_FOR_SQL_AZURE = 5;
public final static int ENGINE_EDITION_FOR_SQL_AZURE_DW = 6;
Expand Down