Skip to content

Commit 5913852

Browse files
committed
log exception messages
1 parent 3bccb4d commit 5913852

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.bigquery.jdbc;
1818

19+
import java.io.PrintWriter;
20+
import java.io.StringWriter;
1921
import java.util.function.Supplier;
2022
import java.util.logging.Level;
2123
import java.util.logging.Logger;
@@ -53,6 +55,12 @@ private void logWithCaller(Level level, Supplier<String> msgSupplier) {
5355
logp(level, sourceClass, sourceMethod, msgSupplier);
5456
}
5557

58+
private String formatStackTrace(Throwable thrown) {
59+
StringWriter sw = new StringWriter();
60+
thrown.printStackTrace(new PrintWriter(sw));
61+
return sw.toString();
62+
}
63+
5664
void finest(String format, Object... args) {
5765
logWithCaller(Level.FINEST, () -> String.format(format, args));
5866
}
@@ -77,8 +85,26 @@ void warning(String format, Object... args) {
7785
logWithCaller(Level.WARNING, () -> String.format(format, args));
7886
}
7987

88+
void warning(Throwable thrown, String msg) {
89+
logWithCaller(Level.WARNING, () -> msg + System.lineSeparator() + formatStackTrace(thrown));
90+
}
91+
92+
void warning(Throwable thrown, String format, Object... args) {
93+
logWithCaller(Level.WARNING, () -> String.format(format, args) + System.lineSeparator() + formatStackTrace(thrown));
94+
}
95+
8096
void severe(String format, Object... args) {
8197
logWithCaller(Level.SEVERE, () -> String.format(format, args));
8298
}
99+
100+
void severe(Throwable thrown, String msg) {
101+
logWithCaller(Level.SEVERE, () -> msg + System.lineSeparator() + formatStackTrace(thrown));
102+
}
103+
104+
void severe(Throwable thrown, String format, Object... args) {
105+
logWithCaller(Level.SEVERE, () -> String.format(format, args) + System.lineSeparator() + formatStackTrace(thrown));
106+
}
83107
}
84108

109+
110+

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.bigquery.jdbc;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

2122
import java.util.ArrayList;
2223
import java.util.List;
@@ -77,4 +78,20 @@ public void testLogWithCallerInference() {
7778
assertEquals("testLogWithCallerInference", record.getSourceMethodName());
7879
assertEquals(BigQueryJdbcCustomLoggerTest.class.getName(), record.getSourceClassName());
7980
}
81+
82+
@Test
83+
public void testLogWithException() {
84+
Exception ex = new Exception("Test exception");
85+
logger.severe(ex, "Error occurred: %s", "detail");
86+
87+
List<LogRecord> records = testHandler.getRecords();
88+
assertEquals(1, records.size());
89+
LogRecord record = records.get(0);
90+
91+
assertEquals("testLogWithException", record.getSourceMethodName());
92+
assertEquals(BigQueryJdbcCustomLoggerTest.class.getName(), record.getSourceClassName());
93+
assertTrue(record.getMessage().contains("Error occurred: detail"));
94+
assertTrue(record.getMessage().contains("java.lang.Exception: Test exception"));
95+
}
8096
}
97+

0 commit comments

Comments
 (0)