-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-1652680: Add option to override j.u.l.ConsoleHandler to write to…
… stdout
- Loading branch information
1 parent
4faff34
commit 21df0d7
Showing
5 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/net/snowflake/client/log/StdOutConsoleHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/net/snowflake/client/log/JDK14LoggerConsoleHandlerOverrideLatestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |