Skip to content

Commit e156cb4

Browse files
committed
Integrate the PerConnectionFileHandler with BigQueryJdbcRootLogger
1 parent fac4092 commit e156cb4

1 file changed

Lines changed: 32 additions & 76 deletions

File tree

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

Lines changed: 32 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@
1919
import com.google.common.base.Strings;
2020
import java.io.IOException;
2121
import java.lang.management.ManagementFactory;
22-
import java.nio.file.Files;
23-
import java.nio.file.Path;
24-
import java.nio.file.Paths;
25-
import java.nio.file.StandardCopyOption;
26-
import java.text.SimpleDateFormat;
27-
import java.util.Date;
22+
import java.time.Instant;
23+
import java.time.ZoneId;
24+
import java.time.format.DateTimeFormatter;
2825
import java.util.logging.ConsoleHandler;
29-
import java.util.logging.FileHandler;
3026
import java.util.logging.Formatter;
3127
import java.util.logging.Handler;
3228
import java.util.logging.Level;
@@ -46,13 +42,11 @@ class BigQueryJdbcRootLogger {
4642
private static final boolean isTest = Boolean.getBoolean("JDBC_TESTS");
4743

4844
private static Handler fileHandler = null;
49-
private static Path currentLogPath = null;
50-
private static int fileCounter = 0;
5145

5246
static final String PROCESS_ID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
5347

54-
private static final ThreadLocal<SimpleDateFormat> DATE_FORMATTER =
55-
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
48+
private static final DateTimeFormatter DATE_FORMATTER =
49+
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(ZoneId.systemDefault());
5650

5751
static String getThreadName(long threadId) {
5852
Thread current = Thread.currentThread();
@@ -92,7 +86,10 @@ public static Formatter getFormatter() {
9286

9387
@Override
9488
public String format(LogRecord record) {
95-
String date = DATE_FORMATTER.get().format(new Date(record.getMillis()));
89+
String date = DATE_FORMATTER.format(Instant.ofEpochMilli(record.getMillis()));
90+
String connectionId = BigQueryJdbcMdc.getConnectionId();
91+
String connStr =
92+
(connectionId != null && !connectionId.isEmpty()) ? connectionId : "NO_CONN";
9693

9794
long threadId = record.getThreadID();
9895
String threadName = getThreadName(threadId);
@@ -104,13 +101,13 @@ public String format(LogRecord record) {
104101
String sourceClassName = record.getLoggerName();
105102
String sourceMethodName = record.getSourceMethodName();
106103

107-
// Expected log format: yyyy-MM-dd HH:mm:ss.SSS LEVEL PID --- [THREAD] CLASS METHOD: MESSAGE
108-
// Example: 2026-04-22 10:16:00.123 INFO 12345 --- [main ]
109-
// com.google.cloud.bigquery.jdbc.BigQueryConnection connect : Connection
110-
// successful
104+
// Expected log format: yyyy-MM-dd HH:mm:ss.SSS [CONNECTION_ID] LEVEL PID --- [THREAD] CLASS
105+
// METHOD: MESSAGE
111106
StringBuilder sb = new StringBuilder(256);
112107
sb.append(date)
113-
.append(" ")
108+
.append(" [")
109+
.append(connStr)
110+
.append("] ")
114111
.append(Strings.padStart(record.getLevel().getName(), 5, ' '))
115112
.append(" ")
116113
.append(PROCESS_ID)
@@ -139,87 +136,46 @@ public static Logger getRootLogger() {
139136
return logger;
140137
}
141138

142-
private static void setHandler() throws IOException {
143-
// If Console handler exists, remove it.
144-
// If File handler exists, use it. Else create new one.
145-
for (Handler h : logger.getHandlers()) {
146-
if (h instanceof ConsoleHandler) {
147-
if (!isTest) {
148-
h.close();
149-
logger.removeHandler(h);
150-
}
151-
} else if (h instanceof FileHandler) {
152-
fileHandler = h;
153-
}
154-
}
155-
156-
if (fileHandler == null) {
157-
String fileName = String.format("BigQueryJdbc%d", fileCounter);
158-
fileCounter++;
159-
160-
currentLogPath = Files.createTempFile(fileName, ".log");
161-
currentLogPath.toFile().deleteOnExit();
162-
163-
fileHandler = new FileHandler(currentLogPath.toString(), 0, 1, true);
164-
logger.addHandler(fileHandler);
165-
}
166-
}
167-
168139
public static void setLevel(Level level, String logPath) throws IOException {
169140
if (level != Level.OFF) {
170-
setPath(logPath);
171-
if (logger.getHandlers().length == 0) {
172-
setHandler();
173-
fileHandler.setFormatter(getFormatter());
174-
logger.setUseParentHandlers(false);
175-
}
176-
fileHandler.setLevel(level);
141+
setPath(logPath, level);
177142
logger.setLevel(level);
178143
} else {
179144
for (Handler h : logger.getHandlers()) {
180145
h.close();
181146
logger.removeHandler(h);
182147
}
183148
fileHandler = null;
184-
currentLogPath = null;
185149
}
186150
}
187151

188-
static void setPath(String logPath) {
152+
static void setPath(String logPath, Level level) {
189153
try {
154+
if (logPath == null) {
155+
logPath = "";
156+
}
190157
if (!logPath.isEmpty() && !logPath.endsWith("/")) {
191158
logPath = logPath + "/";
192159
}
193-
Path dir = Paths.get(logPath);
194-
if (!Files.exists(dir)) {
195-
Files.createDirectory(dir);
196-
}
197-
198-
String fileName = String.format("BigQueryJdbc%d.log", fileCounter);
199-
fileCounter++;
200-
Path destination = Paths.get(logPath + fileName).toAbsolutePath();
201160

202-
if (currentLogPath != null && !currentLogPath.equals(destination)) {
203-
Path source = Paths.get(currentLogPath.toUri());
204-
Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
205-
}
206-
207-
currentLogPath = destination;
208-
fileHandler = new FileHandler(currentLogPath.toString(), 0, 1, true);
209-
fileHandler.setFormatter(getFormatter());
210-
211-
for (Handler h : logger.getHandlers()) {
212-
if (h instanceof FileHandler) {
213-
h.close();
214-
logger.removeHandler(h);
215-
break;
216-
}
161+
if (fileHandler != null) {
162+
fileHandler.close();
163+
logger.removeHandler(fileHandler);
217164
}
218165

166+
fileHandler = new PerConnectionFileHandler(logPath, level);
167+
fileHandler.setLevel(level);
219168
logger.addHandler(fileHandler);
169+
logger.setUseParentHandlers(false);
220170

221-
} catch (IOException ex) {
171+
} catch (Exception ex) {
222172
logger.warning("Log File warning : " + ex);
223173
}
224174
}
175+
176+
public static void closeConnectionHandler(String connectionId) {
177+
if (fileHandler instanceof PerConnectionFileHandler) {
178+
((PerConnectionFileHandler) fileHandler).closeHandler(connectionId);
179+
}
180+
}
225181
}

0 commit comments

Comments
 (0)