1616
1717package com .google .cloud .bigquery .jdbc ;
1818
19+ import com .google .common .base .Strings ;
1920import java .io .IOException ;
2021import java .lang .management .ManagementFactory ;
2122import java .nio .file .Files ;
2425import java .nio .file .StandardCopyOption ;
2526import java .text .SimpleDateFormat ;
2627import java .util .Date ;
27- import java .util .Optional ;
2828import java .util .logging .ConsoleHandler ;
2929import java .util .logging .FileHandler ;
3030import java .util .logging .Formatter ;
@@ -49,6 +49,32 @@ class BigQueryJdbcRootLogger {
4949 private static Path currentLogPath = null ;
5050 private static int fileCounter = 0 ;
5151
52+ static final String PROCESS_ID = ManagementFactory .getRuntimeMXBean ().getName ().split ("@" )[0 ];
53+
54+ private static final ThreadLocal <SimpleDateFormat > DATE_FORMATTER =
55+ ThreadLocal .withInitial (() -> new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS" ));
56+
57+ static String getThreadName (long threadId ) {
58+ Thread current = Thread .currentThread ();
59+ if (current .getId () == threadId ) {
60+ return current .getName ();
61+ }
62+ ThreadGroup rootGroup = current .getThreadGroup ();
63+ while (rootGroup .getParent () != null ) {
64+ rootGroup = rootGroup .getParent ();
65+ }
66+
67+ int count = rootGroup .activeCount ();
68+ Thread [] threads = new Thread [count * 2 ];
69+ int actualCount = rootGroup .enumerate (threads );
70+ for (int i = 0 ; i < actualCount ; i ++) {
71+ if (threads [i ].getId () == threadId ) {
72+ return threads [i ].getName ();
73+ }
74+ }
75+ return "" ;
76+ }
77+
5278 static {
5379 logger .setUseParentHandlers (false );
5480 storageLogger .setUseParentHandlers (true );
@@ -62,49 +88,43 @@ class BigQueryJdbcRootLogger {
6288
6389 public static Formatter getFormatter () {
6490 return new Formatter () {
65- private static final String PATTERN = "yyyy-MM-dd HH:mm:ss.SSS" ;
66- private static final String FORMAT =
67- "%1$s %2$5s %3$d --- [%4$-7.15s] %5$-50s %6$-20s: %7$s%8$s" ;
6891 private static final int MAX_THREAD_NAME_LENGTH = 15 ;
6992
70- /**
71- * Returns the thread for the given thread id.
72- *
73- * @param threadId ID for the thread being logged.
74- * @return returns the thread
75- */
76- Optional <Thread > getThread (long threadId ) {
77- return Thread .getAllStackTraces ().keySet ().stream ()
78- .filter (thread -> thread .getId () == threadId )
79- .findFirst ();
80- }
81-
8293 @ Override
8394 public String format (LogRecord record ) {
84- String date = new SimpleDateFormat (PATTERN ).format (new Date (record .getMillis ()));
85- String threadName =
86- getThread (record .getThreadID ())
87- .map (Thread ::getName )
88- .map (
89- name ->
90- name .length () > MAX_THREAD_NAME_LENGTH
91- ? name .substring (name .length () - MAX_THREAD_NAME_LENGTH )
92- : name )
93- .orElse ("" );
94- long processId =
95- Long .parseLong (ManagementFactory .getRuntimeMXBean ().getName ().split ("@" )[0 ]);
95+ String date = DATE_FORMATTER .get ().format (new Date (record .getMillis ()));
96+
97+ long threadId = record .getThreadID ();
98+ String threadName = getThreadName (threadId );
99+
100+ if (threadName .length () > MAX_THREAD_NAME_LENGTH ) {
101+ threadName = threadName .substring (threadName .length () - MAX_THREAD_NAME_LENGTH );
102+ }
103+
96104 String sourceClassName = record .getLoggerName ();
97105 String sourceMethodName = record .getSourceMethodName ();
98- return String .format (
99- FORMAT ,
100- date ,
101- record .getLevel ().getName (),
102- processId ,
103- threadName ,
104- sourceClassName ,
105- sourceMethodName ,
106- record .getMessage (),
107- System .lineSeparator ());
106+
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
111+ StringBuilder sb = new StringBuilder (256 );
112+ sb .append (date )
113+ .append (" " )
114+ .append (Strings .padStart (record .getLevel ().getName (), 5 , ' ' ))
115+ .append (" " )
116+ .append (PROCESS_ID )
117+ .append (" --- [" )
118+ .append (Strings .padEnd (threadName , 7 , ' ' ))
119+ .append ("] " )
120+ .append (Strings .padEnd (sourceClassName != null ? sourceClassName : "" , 50 , ' ' ))
121+ .append (" " )
122+ .append (Strings .padEnd (sourceMethodName != null ? sourceMethodName : "" , 20 , ' ' ))
123+ .append (": " )
124+ .append (record .getMessage ())
125+ .append (System .lineSeparator ());
126+
127+ return sb .toString ();
108128 }
109129 };
110130 }
0 commit comments