Permalink
Browse files

fix: logger should be generally quiet (#1187)

The logger used in the driver has some calls to levels WARNING and SEVERE
this lower the level to FINE to make the logger quiet in some cases.

fixes #1009
fixes #1162
closes #1149
  • Loading branch information...
jorsol authored and vlsi committed Jul 1, 2018
1 parent 435e2f7 commit 30f06e1411373d72ab59debc352ddf746f6812da
@@ -4,6 +4,8 @@ Notable changes since version 42.0.0, read the complete [History of Changes](htt
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Changed
- Avoid the print of highest logger levels when the exception is re-thrown. [PR 1187](https://github.com/pgjdbc/pgjdbc/pull/1187)
## [42.2.2] (2018-03-15)
### Added
@@ -122,11 +122,14 @@ private Properties loadDefaultProperties() throws IOException {
// neither case can throw SecurityException.
ClassLoader cl = getClass().getClassLoader();
if (cl == null) {
LOGGER.log(Level.FINE, "Can't find our classloader for the Driver; "
+ "attempt to use the system class loader");
cl = ClassLoader.getSystemClassLoader();
}
if (cl == null) {
LOGGER.log(Level.WARNING, "Can't find a classloader for the Driver; not loading driver configuration");
LOGGER.log(Level.WARNING, "Can't find a classloader for the Driver; not loading driver "
+ "configuration from org/postgresql/driverconfig.properties");
return merged; // Give up on finding defaults.
}
@@ -233,7 +236,6 @@ public Connection connect(String url, Properties info) throws SQLException {
}
// parse URL and add more properties
if ((props = parseURL(url, props)) == null) {
LOGGER.log(Level.SEVERE, "Error in url: {0}", url);
return null;
}
try {
@@ -261,7 +263,7 @@ public Connection connect(String url, Properties info) throws SQLException {
thread.start();
return ct.getResult(timeout);
} catch (PSQLException ex1) {
LOGGER.log(Level.SEVERE, "Connection error: ", ex1);
LOGGER.log(Level.FINE, "Connection error: ", ex1);
// re-throw the exception, otherwise it will be caught next, and a
// org.postgresql.unusual error will be returned instead.
throw ex1;
@@ -271,7 +273,7 @@ public Connection connect(String url, Properties info) throws SQLException {
"Your security policy has prevented the connection from being attempted. You probably need to grant the connect java.net.SocketPermission to the database server host and port that you wish to connect to."),
PSQLState.UNEXPECTED_ERROR, ace);
} catch (Exception ex2) {
LOGGER.log(Level.SEVERE, "Unexpected connection error: ", ex2);
LOGGER.log(Level.FINE, "Unexpected connection error: ", ex2);
throw new PSQLException(
GT.tr(
"Something unusual has occurred to cause the driver to fail. Please report this exception."),
@@ -552,6 +554,7 @@ public static Properties parseURL(String url, Properties defaults) {
}
if (!l_urlServer.startsWith("jdbc:postgresql:")) {
LOGGER.log(Level.FINE, "JDBC URL must start with \"jdbc:postgresql:\" but was: {0}", url);
return null;
}
l_urlServer = l_urlServer.substring("jdbc:postgresql:".length());
@@ -560,6 +563,7 @@ public static Properties parseURL(String url, Properties defaults) {
l_urlServer = l_urlServer.substring(2);
int slash = l_urlServer.indexOf('/');
if (slash == -1) {
LOGGER.log(Level.WARNING, "JDBC URL must contain a slash at the end of the host or port: {0}", url);
return null;
}
urlProps.setProperty("PGDBNAME", URLCoder.decode(l_urlServer.substring(slash + 1)));
@@ -572,10 +576,13 @@ public static Properties parseURL(String url, Properties defaults) {
if (portIdx != -1 && address.lastIndexOf(']') < portIdx) {
String portStr = address.substring(portIdx + 1);
try {
// squid:S2201 The return value of "parseInt" must be used.
// The side effect is NumberFormatException, thus ignore sonar error here
Integer.parseInt(portStr); //NOSONAR
} catch (NumberFormatException ex) {
int port = Integer.parseInt(portStr);
if (port < 1 || port > 65535) {
LOGGER.log(Level.WARNING, "JDBC URL port in invalid range: {0}", portStr);
return null;
}
} catch (NumberFormatException ignore) {
LOGGER.log(Level.WARNING, "JDBC URL invalid port number: {0}", portStr);
return null;
}
ports.append(portStr);
@@ -67,12 +67,6 @@
private static final int AUTH_REQ_SASL_CONTINUE = 11;
private static final int AUTH_REQ_SASL_FINAL = 12;
/**
* Marker exception; thrown when we want to fall back to using V2.
*/
private static class UnsupportedProtocolException extends IOException {
}
private ISSPIClient createSSPI(PGStream pgStream,
String spnServiceClass,
boolean enableNegotiate) {
@@ -230,18 +224,13 @@ public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, String user, Strin
// And we're done.
return queryExecutor;
} catch (UnsupportedProtocolException upe) {
// Swallow this and return null so ConnectionFactory tries the next protocol.
LOGGER.log(Level.SEVERE, "Protocol not supported, abandoning connection.", upe);
closeStream(newStream);
return null;
} catch (ConnectException cex) {
// Added by Peter Mount <peter@retep.org.uk>
// ConnectException is thrown when the connection cannot be made.
// we trap this an return a more meaningful message for the end user
GlobalHostStatusTracker.reportHostStatus(hostSpec, HostStatus.ConnectFail);
knownStates.put(hostSpec, HostStatus.ConnectFail);
log(Level.WARNING, "ConnectException occurred while connecting to {0}", cex, hostSpec);
log(Level.FINE, "ConnectException occurred while connecting to {0}", cex, hostSpec);
if (hostIter.hasNext()) {
// still more addresses to try
continue;
@@ -253,7 +242,7 @@ public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, String user, Strin
closeStream(newStream);
GlobalHostStatusTracker.reportHostStatus(hostSpec, HostStatus.ConnectFail);
knownStates.put(hostSpec, HostStatus.ConnectFail);
log(Level.WARNING, "IOException occurred while connecting to {0}", ioe, hostSpec);
log(Level.FINE, "IOException occurred while connecting to {0}", ioe, hostSpec);
if (hostIter.hasNext()) {
// still more addresses to try
continue;
@@ -262,7 +251,7 @@ public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, String user, Strin
PSQLState.CONNECTION_UNABLE_TO_CONNECT, ioe);
} catch (SQLException se) {
closeStream(newStream);
log(Level.WARNING, "SQLException occurred while connecting to {0}", se, hostSpec);
log(Level.FINE, "SQLException occurred while connecting to {0}", se, hostSpec);
GlobalHostStatusTracker.reportHostStatus(hostSpec, HostStatus.ConnectFail);
knownStates.put(hostSpec, HostStatus.ConnectFail);
if (hostIter.hasNext()) {
@@ -467,11 +456,6 @@ private void doAuthentication(PGStream pgStream, String host, String user, Prope
// "User authentication failed"
//
int l_elen = pgStream.receiveInteger4();
if (l_elen > 30000) {
// if the error length is > than 30000 we assume this is really a v2 protocol
// server, so trigger fallback.
throw new UnsupportedProtocolException();
}
ServerErrorMessage errorMsg =
new ServerErrorMessage(pgStream.receiveErrorString(l_elen - 4));
@@ -668,7 +652,7 @@ private void doAuthentication(PGStream pgStream, String host, String user, Prope
try {
sspiClient.dispose();
} catch (RuntimeException ex) {
LOGGER.log(Level.WARNING, "Unexpected error during SSPI context disposal", ex);
LOGGER.log(Level.FINE, "Unexpected error during SSPI context disposal", ex);
}
}
@@ -2608,7 +2608,7 @@ public void receiveParameterStatus() throws IOException, SQLException {
if (name.equals("client_encoding")) {
if (allowEncodingChanges) {
if (!value.equalsIgnoreCase("UTF8") && !value.equalsIgnoreCase("UTF-8")) {
LOGGER.log(Level.WARNING,
LOGGER.log(Level.FINE,
"pgjdbc expects client_encoding to be UTF8 for proper operation. Actual encoding is {0}",
value);
}
@@ -97,7 +97,7 @@ public Connection getConnection(String user, String password) throws SQLExceptio
}
return con;
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, "Failed to create a {0} for {1} at {2}: {3}",
LOGGER.log(Level.FINE, "Failed to create a {0} for {1} at {2}: {3}",
new Object[]{getDescription(), user, getUrl(), e});
throw e;
}
@@ -1386,7 +1386,7 @@ public boolean isValid(int timeout) throws SQLException {
// "current transaction aborted", assume the connection is up and running
return true;
}
LOGGER.log(Level.WARNING, GT.tr("Validating connection."), e);
LOGGER.log(Level.FINE, GT.tr("Validating connection."), e);
}
return false;
}
@@ -88,7 +88,7 @@ public boolean isSSPISupported() {
* won't have JNA and this will throw a NoClassDefFoundError.
*/
if (!Platform.isWindows()) {
LOGGER.log(Level.WARNING, "SSPI not supported: non-Windows host");
LOGGER.log(Level.FINE, "SSPI not supported: non-Windows host");
return false;
}
/* Waffle must be on the CLASSPATH */
@@ -65,10 +65,13 @@ public void testAcceptsURL() throws Exception {
verifyUrl(drv, "jdbc:postgresql://[::1]:5740/db", "[::1]", "5740", "db");
// Badly formatted url's
assertTrue(!drv.acceptsURL("jdbc:postgres:test"));
assertTrue(!drv.acceptsURL("postgresql:test"));
assertTrue(!drv.acceptsURL("db"));
assertTrue(!drv.acceptsURL("jdbc:postgresql://localhost:5432a/test"));
assertFalse(drv.acceptsURL("jdbc:postgres:test"));
assertFalse(drv.acceptsURL("postgresql:test"));
assertFalse(drv.acceptsURL("db"));
assertFalse(drv.acceptsURL("jdbc:postgresql://localhost:5432a/test"));
assertFalse(drv.acceptsURL("jdbc:postgresql://localhost:500000/test"));
assertFalse(drv.acceptsURL("jdbc:postgresql://localhost:0/test"));
assertFalse(drv.acceptsURL("jdbc:postgresql://localhost:-2/test"));
// failover urls
verifyUrl(drv, "jdbc:postgresql://localhost,127.0.0.1:5432/test", "localhost,127.0.0.1",

0 comments on commit 30f06e1

Please sign in to comment.