Skip to content

Commit

Permalink
SNOW-1652680: Add option to override j.u.l.ConsoleHandler to write to…
Browse files Browse the repository at this point in the history
… stdout
  • Loading branch information
sfc-gh-dprzybysz committed Dec 4, 2024
1 parent 4faff34 commit 21df0d7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/net/snowflake/client/core/SFSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.snowflake.client.jdbc.telemetry.Telemetry;
import net.snowflake.client.jdbc.telemetry.TelemetryClient;
import net.snowflake.client.jdbc.telemetryOOB.TelemetryService;
import net.snowflake.client.log.JDK14Logger;
import net.snowflake.client.log.SFLogger;
import net.snowflake.client.log.SFLoggerFactory;
import net.snowflake.client.log.SFLoggerUtil;
Expand Down Expand Up @@ -435,6 +436,11 @@ public void addSFSessionProperty(String propertyName, Object propertyValue) thro
tracingLevel = Level.parse(((String) propertyValue).toUpperCase());
}
break;
case JAVA_LOGGING_CONSOLE_STD_OUT:
if (propertyValue != null && (Boolean) propertyValue) {
JDK14Logger.useStdOutConsoleHandler();
}
break;

case DISABLE_SOCKS_PROXY:
// note: if any session has this parameter, it will be used for all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public enum SFSessionProperty {

HTTP_CLIENT_CONNECTION_TIMEOUT("HTTP_CLIENT_CONNECTION_TIMEOUT", false, Integer.class),

HTTP_CLIENT_SOCKET_TIMEOUT("HTTP_CLIENT_SOCKET_TIMEOUT", false, Integer.class);
HTTP_CLIENT_SOCKET_TIMEOUT("HTTP_CLIENT_SOCKET_TIMEOUT", false, Integer.class),

JAVA_LOGGING_CONSOLE_STD_OUT("JAVA_LOGGING_CONSOLE_STD_OUT", false, Boolean.class);

// property key in string
private String propertyKey;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/snowflake/client/log/JDK14Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.logging.SimpleFormatter;
import net.snowflake.client.core.EventHandler;
import net.snowflake.client.core.EventUtil;
import net.snowflake.client.core.SFSessionProperty;
import net.snowflake.client.core.SnowflakeJdbcInternalApi;
import net.snowflake.client.util.SecretDetector;

/**
Expand All @@ -38,10 +40,32 @@ public class JDK14Logger implements SFLogger {

public static String STDOUT = "STDOUT";

private static final StdOutConsoleHandler STD_OUT_CONSOLE_HANDLER = new StdOutConsoleHandler();

public JDK14Logger(String name) {
this.jdkLogger = Logger.getLogger(name);
}

static {
String javaLoggingConsoleStdOut =
System.getProperty(SFSessionProperty.JAVA_LOGGING_CONSOLE_STD_OUT.getPropertyKey());
if ("true".equalsIgnoreCase(javaLoggingConsoleStdOut)) {
useStdOutConsoleHandler();
}
}

@SnowflakeJdbcInternalApi
public static void useStdOutConsoleHandler() {
Logger rootLogger = Logger.getLogger("");
for (Handler handler : rootLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
rootLogger.removeHandler(handler);
rootLogger.addHandler(STD_OUT_CONSOLE_HANDLER);
break;
}
}
}

public boolean isDebugEnabled() {
return this.jdkLogger.isLoggable(Level.FINE);
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/snowflake/client/log/StdOutConsoleHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2012-2019 Snowflake Computing Inc. All rights reserved.
*/
package net.snowflake.client.log;

import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;

class StdOutConsoleHandler extends StreamHandler {
public StdOutConsoleHandler() {
// configure with specific defaults for ConsoleHandler
super(System.out, new SimpleFormatter());
}

@Override
public void publish(LogRecord record) {
super.publish(record);
flush();
}

@Override
public void close() {
flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/
package net.snowflake.client.log;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Properties;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import net.snowflake.client.category.TestTags;
import net.snowflake.client.jdbc.BaseJDBCTest;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag(TestTags.CORE)
public class JDK14LoggerConsoleHandlerOverrideLatestIT extends BaseJDBCTest {
/** Added in > 3.20.0 */
@Test
public void shouldOverrideConsoleLogger() throws Exception {
Properties paramProperties = new Properties();
paramProperties.put("JAVA_LOGGING_CONSOLE_STD_OUT", true);
try (Connection connection = getConnection(paramProperties);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select 1")) {
assertTrue(resultSet.next());
Handler[] handlers = Logger.getLogger("").getHandlers();
assertTrue(handlers.length > 0);
assertFalse(Arrays.stream(handlers).anyMatch(h -> h instanceof ConsoleHandler));
assertTrue(Arrays.stream(handlers).anyMatch(h -> h instanceof StdOutConsoleHandler));
}
}
}

0 comments on commit 21df0d7

Please sign in to comment.