Skip to content

Commit f6d4703

Browse files
sehropedavecramer
andauthored
Merge pull request from GHSA-673j-qm5f-xpv8
Removes loggerFile and loggerLevel properties and defers to java.util.logging to configure all logging in the driver. Users that previously specifyied custom logging via those connection properties should change their applications to instead configure via a java.util.logging properties file or system properties. The properties and enum values are now deprecated and may be removed in a future driver version. Co-authored-by: Dave Cramer <davecramer@gmail.com>
1 parent c03664e commit f6d4703

9 files changed

Lines changed: 28 additions & 263 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ where:
8383
* **database** (Optional) is the database name. Defaults to the same name as the *user name* used in the connection.
8484
* **propertyX** (Optional) is one or more option connection properties. For more information see *Connection properties*.
8585

86+
### Logging
87+
PgJDBC uses java.util.logging for logging.
88+
To configure log levels and control log output destination (e.g. file or console), configure your java.util.logging properties accordingly for the org.postgresql logger.
89+
Note that the most detailed log levels, "`FINEST`", may include sensitive information such as connection details, query SQL, or command parameters.
90+
8691
#### Connection Properties
8792
In addition to the standard connection parameters the driver supports a number of additional properties which can be used to specify additional driver behaviour specific to PostgreSQL™. These properties may be specified in either the connection URL or an additional Properties object parameter to DriverManager.getConnection.
8893

@@ -104,8 +109,6 @@ In addition to the standard connection parameters the driver supports a number o
104109
| sslpassword | String | null | The password for the client's ssl key (ignored if sslpasswordcallback is set) |
105110
| sendBufferSize | Integer | -1 | Socket write buffer size |
106111
| receiveBufferSize | Integer | -1 | Socket read buffer size |
107-
| loggerLevel | String | null | Logger level of the driver using java.util.logging. Allowed values: OFF, DEBUG or TRACE. |
108-
| loggerFile | String | null | File name output of the Logger, if set, the Logger will use a FileHandler to write to a specified file. If the parameter is not set or the file can't be created the ConsoleHandler will be used instead. |
109112
| logServerErrorDetail | Boolean | true | Allows server error detail (such as sql statements and values) to be logged and passed on in exceptions. Setting to false will mask these errors so they won't be exposed to users, or logs. |
110113
| allowEncodingChanges | Boolean | false | Allow for changes in client_encoding |
111114
| logUnclosedConnections | Boolean | false | When connections that are not explicitly closed are garbage collected, log the stacktrace from the opening of the connection to trace the leak source |

docs/documentation/head/connect.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,13 @@ Connection conn = DriverManager.getConnection(url);
178178

179179
* **loggerLevel** = String
180180

181-
Logger level of the driver. Allowed values: <code>OFF</code>, <code>DEBUG</code> or <code>TRACE</code>.
182-
This enable the <code>java.util.logging.Logger</code> Level of the driver based on the following mapping
183-
of levels: DEBUG -&gt; FINE, TRACE -&gt; FINEST. This property is intended for debug the driver and
184-
not for general SQL query debug.
181+
This property is no longer used by the driver and will be ignored.
182+
All logging configuration is handled by java.util.logging.
185183

186184
* **loggerFile** = String
187185

188-
File name output of the Logger. If set, the Logger will use a <code>java.util.logging.FileHandler</code>
189-
to write to a specified file. If the parameter is not set or the file can’t be created the
190-
<code>java.util.logging.ConsoleHandler</code> will be used instead. This parameter should be use
191-
together with loggerLevel.
186+
This property is no longer used by the driver and will be ignored.
187+
All logging configuration is handled by java.util.logging.
192188

193189
* **allowEncodingChanges** = boolean
194190

pgjdbc/src/main/java/org/postgresql/Driver.java

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
import org.postgresql.jdbc.PgConnection;
1111
import org.postgresql.util.DriverInfo;
12-
import org.postgresql.util.ExpressionProperties;
1312
import org.postgresql.util.GT;
1413
import org.postgresql.util.HostSpec;
15-
import org.postgresql.util.LogWriterHandler;
1614
import org.postgresql.util.PGPropertyPasswordParser;
1715
import org.postgresql.util.PGPropertyServiceParser;
1816
import org.postgresql.util.PGPropertyUtil;
@@ -39,12 +37,8 @@
3937
import java.util.Properties;
4038
import java.util.Set;
4139
import java.util.concurrent.TimeUnit;
42-
import java.util.logging.ConsoleHandler;
43-
import java.util.logging.Formatter;
4440
import java.util.logging.Level;
4541
import java.util.logging.Logger;
46-
import java.util.logging.SimpleFormatter;
47-
import java.util.logging.StreamHandler;
4842

4943
/**
5044
* <p>The Java SQL framework allows for multiple database drivers. Each driver should supply a class
@@ -249,8 +243,6 @@ private Properties loadDefaultProperties() throws IOException {
249243
return null;
250244
}
251245
try {
252-
// Setup java.util.logging.Logger using connection properties.
253-
setupLoggerFromProperties(props);
254246

255247
LOGGER.log(Level.FINE, "Connecting with URL: {0}", url);
256248

@@ -291,73 +283,13 @@ private Properties loadDefaultProperties() throws IOException {
291283
}
292284
}
293285

294-
// Used to check if the handler file is the same
295-
private static @Nullable String loggerHandlerFile;
296-
297286
/**
298-
* <p>Setup java.util.logging.Logger using connection properties.</p>
299-
*
300-
* <p>See {@link PGProperty#LOGGER_FILE} and {@link PGProperty#LOGGER_FILE}</p>
301-
*
287+
* this is an empty method left here for graalvm
288+
* we removed the ability to setup the logger from properties
289+
* due to a security issue
302290
* @param props Connection Properties
303291
*/
304292
private void setupLoggerFromProperties(final Properties props) {
305-
final String driverLogLevel = PGProperty.LOGGER_LEVEL.get(props);
306-
if (driverLogLevel == null) {
307-
return; // Don't mess with Logger if not set
308-
}
309-
if ("OFF".equalsIgnoreCase(driverLogLevel)) {
310-
PARENT_LOGGER.setLevel(Level.OFF);
311-
return; // Don't mess with Logger if set to OFF
312-
} else if ("DEBUG".equalsIgnoreCase(driverLogLevel)) {
313-
PARENT_LOGGER.setLevel(Level.FINE);
314-
} else if ("TRACE".equalsIgnoreCase(driverLogLevel)) {
315-
PARENT_LOGGER.setLevel(Level.FINEST);
316-
}
317-
318-
ExpressionProperties exprProps = new ExpressionProperties(props, System.getProperties());
319-
final String driverLogFile = PGProperty.LOGGER_FILE.get(exprProps);
320-
if (driverLogFile != null && driverLogFile.equals(loggerHandlerFile)) {
321-
return; // Same file output, do nothing.
322-
}
323-
324-
for (java.util.logging.Handler handlers : PARENT_LOGGER.getHandlers()) {
325-
// Remove previously set Handlers
326-
handlers.close();
327-
PARENT_LOGGER.removeHandler(handlers);
328-
loggerHandlerFile = null;
329-
}
330-
331-
java.util.logging.Handler handler = null;
332-
if (driverLogFile != null) {
333-
try {
334-
handler = new java.util.logging.FileHandler(driverLogFile);
335-
loggerHandlerFile = driverLogFile;
336-
} catch (Exception ex) {
337-
System.err.println("Cannot enable FileHandler, fallback to ConsoleHandler.");
338-
}
339-
}
340-
341-
Formatter formatter = new SimpleFormatter();
342-
343-
if ( handler == null ) {
344-
if (DriverManager.getLogWriter() != null) {
345-
handler = new LogWriterHandler(DriverManager.getLogWriter());
346-
} else if ( DriverManager.getLogStream() != null) {
347-
handler = new StreamHandler(DriverManager.getLogStream(), formatter);
348-
} else {
349-
handler = new ConsoleHandler();
350-
}
351-
} else {
352-
handler.setFormatter(formatter);
353-
}
354-
355-
Level loggerLevel = PARENT_LOGGER.getLevel();
356-
if (loggerLevel != null) {
357-
handler.setLevel(loggerLevel);
358-
}
359-
PARENT_LOGGER.setUseParentHandlers(false);
360-
PARENT_LOGGER.addHandler(handler);
361293
}
362294

363295
/**

pgjdbc/src/main/java/org/postgresql/PGProperty.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,31 +293,17 @@ public enum PGProperty {
293293
"If disabled hosts are connected in the given order. If enabled hosts are chosen randomly from the set of suitable candidates"),
294294

295295
/**
296-
* <p>File name output of the Logger, if set, the Logger will use a
297-
* {@link java.util.logging.FileHandler} to write to a specified file. If the parameter is not set
298-
* or the file can't be created the {@link java.util.logging.ConsoleHandler} will be used instead.</p>
299-
*
300-
* <p>Parameter should be use together with {@link PGProperty#LOGGER_LEVEL}</p>
296+
* This property is no longer used by the driver and will be ignored.
297+
* Logging is configured via java.util.logging.
301298
*/
302299
LOGGER_FILE(
303300
"loggerFile",
304301
null,
305302
"File name output of the Logger"),
306303

307304
/**
308-
* <p>Logger level of the driver. Allowed values: {@code OFF}, {@code DEBUG} or {@code TRACE}.</p>
309-
*
310-
* <p>This enable the {@link java.util.logging.Logger} of the driver based on the following mapping
311-
* of levels:</p>
312-
* <ul>
313-
* <li>FINE -&gt; DEBUG</li>
314-
* <li>FINEST -&gt; TRACE</li>
315-
* </ul>
316-
*
317-
* <p><b>NOTE:</b> The recommended approach to enable java.util.logging is using a
318-
* {@code logging.properties} configuration file with the property
319-
* {@code -Djava.util.logging.config.file=myfile} or if your are using an application server
320-
* you should use the appropriate logging subsystem.</p>
305+
* This property is no longer used by the driver and will be ignored.
306+
* Logging is configured via java.util.logging.
321307
*/
322308
LOGGER_LEVEL(
323309
"loggerLevel",

pgjdbc/src/main/java/org/postgresql/ds/common/BaseDataSource.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,34 +1208,38 @@ public void setEscapeSyntaxCallMode(@Nullable String callMode) {
12081208
}
12091209

12101210
/**
1211-
* @return Logger Level of the JDBC Driver
1212-
* @see PGProperty#LOGGER_LEVEL
1211+
* This property is no longer used by the driver and will be ignored.
1212+
* @deprecated Configure via java.util.logging
12131213
*/
1214+
@Deprecated
12141215
public @Nullable String getLoggerLevel() {
12151216
return PGProperty.LOGGER_LEVEL.get(properties);
12161217
}
12171218

12181219
/**
1219-
* @param loggerLevel of the JDBC Driver
1220-
* @see PGProperty#LOGGER_LEVEL
1220+
* This property is no longer used by the driver and will be ignored.
1221+
* @deprecated Configure via java.util.logging
12211222
*/
1223+
@Deprecated
12221224
public void setLoggerLevel(@Nullable String loggerLevel) {
12231225
PGProperty.LOGGER_LEVEL.set(properties, loggerLevel);
12241226
}
12251227

12261228
/**
1227-
* @return File output of the Logger.
1228-
* @see PGProperty#LOGGER_FILE
1229+
* This property is no longer used by the driver and will be ignored.
1230+
* @deprecated Configure via java.util.logging
12291231
*/
1232+
@Deprecated
12301233
public @Nullable String getLoggerFile() {
12311234
ExpressionProperties exprProps = new ExpressionProperties(properties, System.getProperties());
12321235
return PGProperty.LOGGER_FILE.get(exprProps);
12331236
}
12341237

12351238
/**
1236-
* @param loggerFile File output of the Logger.
1237-
* @see PGProperty#LOGGER_LEVEL
1239+
* This property is no longer used by the driver and will be ignored.
1240+
* @deprecated Configure via java.util.logging
12381241
*/
1242+
@Deprecated
12391243
public void setLoggerFile(@Nullable String loggerFile) {
12401244
PGProperty.LOGGER_FILE.set(properties, loggerFile);
12411245
}

pgjdbc/src/test/java/org/postgresql/test/jdbc2/DriverTest.java

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import org.postgresql.PGEnvironment;
1717
import org.postgresql.PGProperty;
1818
import org.postgresql.test.TestUtil;
19-
import org.postgresql.util.LogWriterHandler;
20-
import org.postgresql.util.NullOutputStream;
2119
import org.postgresql.util.URLCoder;
2220

2321
import org.junit.Test;
@@ -29,7 +27,6 @@
2927

3028
import java.io.ByteArrayOutputStream;
3129
import java.io.PrintStream;
32-
import java.io.PrintWriter;
3330
import java.lang.reflect.Method;
3431
import java.net.URL;
3532
import java.nio.file.Files;
@@ -40,8 +37,6 @@
4037
import java.util.ArrayList;
4138
import java.util.Collections;
4239
import java.util.Properties;
43-
import java.util.logging.Handler;
44-
import java.util.logging.Logger;
4540

4641
/*
4742
* Tests the dynamically created class org.postgresql.Driver
@@ -497,68 +492,6 @@ public void testRegistration() throws Exception {
497492
fail("Driver has not been found in DriverManager's list but it should be registered");
498493
}
499494

500-
@Test
501-
public void testSetLogWriter() throws Exception {
502-
503-
// this is a dummy to make sure TestUtil is initialized
504-
Connection con = DriverManager.getConnection(TestUtil.getURL(), TestUtil.getUser(), TestUtil.getPassword());
505-
con.close();
506-
String loggerLevel = System.getProperty("loggerLevel");
507-
String loggerFile = System.getProperty("loggerFile");
508-
509-
PrintWriter prevLog = DriverManager.getLogWriter();
510-
try {
511-
PrintWriter printWriter = new PrintWriter(new NullOutputStream(System.err));
512-
DriverManager.setLogWriter(printWriter);
513-
assertEquals(DriverManager.getLogWriter(), printWriter);
514-
System.clearProperty("loggerFile");
515-
System.clearProperty("loggerLevel");
516-
Properties props = new Properties();
517-
props.setProperty("user", TestUtil.getUser());
518-
props.setProperty("password", TestUtil.getPassword());
519-
props.setProperty("loggerLevel", "DEBUG");
520-
con = DriverManager.getConnection(TestUtil.getURL(), props);
521-
522-
Logger logger = Logger.getLogger("org.postgresql");
523-
Handler[] handlers = logger.getHandlers();
524-
assertTrue(handlers[0] instanceof LogWriterHandler );
525-
con.close();
526-
} finally {
527-
DriverManager.setLogWriter(prevLog);
528-
setProperty("loggerLevel", loggerLevel);
529-
setProperty("loggerFile", loggerFile);
530-
}
531-
}
532-
533-
@Test
534-
public void testSetLogStream() throws Exception {
535-
// this is a dummy to make sure TestUtil is initialized
536-
Connection con = DriverManager.getConnection(TestUtil.getURL(), TestUtil.getUser(), TestUtil.getPassword());
537-
con.close();
538-
String loggerLevel = System.getProperty("loggerLevel");
539-
String loggerFile = System.getProperty("loggerFile");
540-
541-
try {
542-
DriverManager.setLogStream(new NullOutputStream(System.err));
543-
System.clearProperty("loggerFile");
544-
System.clearProperty("loggerLevel");
545-
Properties props = new Properties();
546-
props.setProperty("user", TestUtil.getUser());
547-
props.setProperty("password", TestUtil.getPassword());
548-
props.setProperty("loggerLevel", "DEBUG");
549-
con = DriverManager.getConnection(TestUtil.getURL(), props);
550-
551-
Logger logger = Logger.getLogger("org.postgresql");
552-
Handler []handlers = logger.getHandlers();
553-
assertTrue( handlers[0] instanceof LogWriterHandler );
554-
con.close();
555-
} finally {
556-
DriverManager.setLogStream(null);
557-
setProperty("loggerLevel", loggerLevel);
558-
setProperty("loggerFile", loggerFile);
559-
}
560-
}
561-
562495
@Test
563496
public void testSystemErrIsNotClosedWhenCreatedMultipleConnections() throws Exception {
564497
TestUtil.initDriver();

pgjdbc/src/test/java/org/postgresql/test/jdbc2/PGPropertyTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static org.junit.Assert.assertEquals;
99
import static org.junit.Assert.assertFalse;
1010
import static org.junit.Assert.assertNotNull;
11-
import static org.junit.Assert.assertNull;
1211
import static org.junit.Assert.assertSame;
1312
import static org.junit.Assert.assertTrue;
1413

@@ -215,15 +214,6 @@ public void testPresenceCheck() {
215214
assertFalse(PGProperty.READ_ONLY.isPresent(empty));
216215
}
217216

218-
@Test
219-
public void testNullValue() {
220-
Properties empty = new Properties();
221-
assertNull(PGProperty.LOGGER_LEVEL.getSetString(empty));
222-
Properties withLogging = new Properties();
223-
withLogging.setProperty(PGProperty.LOGGER_LEVEL.getName(), "OFF");
224-
assertNotNull(PGProperty.LOGGER_LEVEL.getSetString(withLogging));
225-
}
226-
227217
@Test
228218
public void testEncodedUrlValues() {
229219
String databaseName = "d&a%ta+base";

pgjdbc/src/test/java/org/postgresql/test/jdbc4/Jdbc4TestSuite.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
DatabaseMetaDataTest.class,
2525
IsValidTest.class,
2626
JsonbTest.class,
27-
LogTest.class,
2827
PGCopyInputStreamTest.class,
2928
UUIDTest.class,
3029
WrapperTest.class,

0 commit comments

Comments
 (0)