-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBigQueryJdbcResultSetLogger.java
More file actions
198 lines (168 loc) · 6.27 KB
/
Copy pathBigQueryJdbcResultSetLogger.java
File metadata and controls
198 lines (168 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery.jdbc;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/**
* Specialized high-performance logger for ResultSet and its related classes. Avoids expensive stack
* trace walking (caller inference) on hot-path logging calls.
*/
public class BigQueryJdbcResultSetLogger extends BigQueryJdbcCustomLogger {
private static final ConcurrentHashMap<String, BigQueryJdbcResultSetLogger> cache =
new ConcurrentHashMap<>();
private final String targetClassName;
private final String connectionId;
public static BigQueryJdbcResultSetLogger getLogger(Class<?> clazz) {
return getLogger(clazz, null);
}
public static BigQueryJdbcResultSetLogger getLogger(Class<?> clazz, String connectionId) {
if (connectionId == null) {
return cache.computeIfAbsent(
clazz.getName(), k -> new BigQueryJdbcResultSetLogger(clazz, null));
}
return new BigQueryJdbcResultSetLogger(clazz, connectionId);
}
public String getConnectionId() {
return connectionId;
}
private BigQueryJdbcResultSetLogger arrowStructLogger;
private BigQueryJdbcResultSetLogger jsonStructLogger;
private BigQueryJdbcResultSetLogger arrowArrayLogger;
private BigQueryJdbcResultSetLogger jsonArrayLogger;
public BigQueryJdbcResultSetLogger getArrowStructLogger() {
if (arrowStructLogger == null) {
arrowStructLogger = getLogger(BigQueryArrowStruct.class, connectionId);
}
return arrowStructLogger;
}
public BigQueryJdbcResultSetLogger getJsonStructLogger() {
if (jsonStructLogger == null) {
jsonStructLogger = getLogger(BigQueryJsonStruct.class, connectionId);
}
return jsonStructLogger;
}
public BigQueryJdbcResultSetLogger getArrowArrayLogger() {
if (arrowArrayLogger == null) {
arrowArrayLogger = getLogger(BigQueryArrowArray.class, connectionId);
}
return arrowArrayLogger;
}
public BigQueryJdbcResultSetLogger getJsonArrayLogger() {
if (jsonArrayLogger == null) {
jsonArrayLogger = getLogger(BigQueryJsonArray.class, connectionId);
}
return jsonArrayLogger;
}
public BigQueryJdbcResultSetLogger(Class<?> clazz) {
this(clazz, null);
}
public BigQueryJdbcResultSetLogger(Class<?> clazz, String connectionId) {
super(clazz.getName());
this.targetClassName = clazz.getName();
this.connectionId = connectionId;
}
@Override
public void log(LogRecord record) {
record.setSourceClassName(targetClassName);
record.setThreadID((int) Thread.currentThread().getId());
if (connectionId != null) {
Object[] existingParams = record.getParameters();
if (existingParams == null || existingParams.length == 0) {
record.setParameters(new Object[] {connectionId});
} else {
Object[] newParams = new Object[existingParams.length + 1];
newParams[0] = connectionId;
System.arraycopy(existingParams, 0, newParams, 1, existingParams.length);
record.setParameters(newParams);
}
}
super.log(record);
}
private void logTrace(Level level, String methodName, String msg) {
if (isLoggable(level)) {
LogRecord record = new LogRecord(level, msg);
record.setSourceMethodName(methodName);
this.log(record);
}
}
private void logTrace(Level level, String methodName, Supplier<String> msgSupplier) {
if (isLoggable(level)) {
LogRecord record = new LogRecord(level, msgSupplier.get());
record.setSourceMethodName(methodName);
this.log(record);
}
}
/**
* Log a message at Level.FINEST with predefined class name, method name and messageto avoid stack
* trace parsing on the hot-path.
*/
public void finestTrace(String methodName) {
logTrace(Level.FINEST, methodName, "++enter++");
}
/**
* Log a message at Level.FINEST with predefined class name and method name to avoid stack trace
* parsing on the hot-path.
*/
public void finestTrace(String methodName, String msg) {
logTrace(Level.FINEST, methodName, msg);
}
/** Log a lazy message at Level.FINEST with predefined class name and method name. */
public void finestTrace(String methodName, Supplier<String> msgSupplier) {
logTrace(Level.FINEST, methodName, msgSupplier);
}
/** Log a message at Level.FINER with predefined class name and method name. */
public void finerTrace(String methodName, String msg) {
logTrace(Level.FINER, methodName, msg);
}
/** Log a lazy message at Level.FINER with predefined class name and method name. */
public void finerTrace(String methodName, Supplier<String> msgSupplier) {
logTrace(Level.FINER, methodName, msgSupplier);
}
/** Log a message at Level.FINE with predefined class name and method name. */
public void fineTrace(String methodName, String msg) {
logTrace(Level.FINE, methodName, msg);
}
/** Log a lazy message at Level.FINE with predefined class name and method name. */
public void fineTrace(String methodName, Supplier<String> msgSupplier) {
logTrace(Level.FINE, methodName, msgSupplier);
}
@Override
public void finest(String msg) {
logTrace(Level.FINEST, "unknown", msg);
}
@Override
public void finest(Supplier<String> msgSupplier) {
logTrace(Level.FINEST, "unknown", msgSupplier);
}
@Override
public void finer(String msg) {
logTrace(Level.FINER, "unknown", msg);
}
@Override
public void finer(Supplier<String> msgSupplier) {
logTrace(Level.FINER, "unknown", msgSupplier);
}
@Override
public void fine(String msg) {
logTrace(Level.FINE, "unknown", msg);
}
@Override
public void fine(Supplier<String> msgSupplier) {
logTrace(Level.FINE, "unknown", msgSupplier);
}
}