-
Notifications
You must be signed in to change notification settings - Fork 25.2k
SQL: jdbc debugging enhancement #53880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
7832e66
c3f59d3
a3d7140
686f64b
0808286
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,11 @@ final class DebugLog { | |
private static final String HEADER = "%tF/%tT.%tL - "; | ||
|
||
final PrintWriter print; | ||
private boolean debugBuffered; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to keep the prefix |
||
|
||
DebugLog(PrintWriter print) { | ||
DebugLog(PrintWriter print, boolean debugBuffered) { | ||
this.print = print; | ||
this.debugBuffered = debugBuffered; | ||
} | ||
|
||
void logMethod(Method m, Object[] args) { | ||
|
@@ -31,9 +33,11 @@ void logMethod(Method m, Object[] args) { | |
m.getName(), | ||
//array(m.getParameterTypes()), | ||
array(args)); | ||
if (debugBuffered == false) { | ||
print.flush(); | ||
} | ||
} | ||
|
||
|
||
void logResult(Method m, Object[] args, Object r) { | ||
long time = System.currentTimeMillis(); | ||
print.printf(Locale.ROOT, HEADER + "%s#%s(%s) returned %s%n", | ||
|
@@ -44,6 +48,9 @@ void logResult(Method m, Object[] args, Object r) { | |
//array(m.getParameterTypes()), | ||
array(args), | ||
r); | ||
if (debugBuffered == false) { | ||
print.flush(); | ||
} | ||
} | ||
|
||
void logException(Method m, Object[] args, Throwable t) { | ||
|
@@ -57,6 +64,25 @@ void logException(Method m, Object[] args, Throwable t) { | |
print.flush(); | ||
} | ||
|
||
void logSystemInfo() { | ||
long time = System.currentTimeMillis(); | ||
print.printf(Locale.ROOT, HEADER + "OS[%s/%s/%s], JVM[%s/%s/%s/%s]", | ||
time, time, time, | ||
System.getProperty("os.name"), | ||
System.getProperty("os.version"), | ||
System.getProperty("os.arch"), | ||
System.getProperty("java.vm.vendor"), | ||
System.getProperty("java.vm.name"), | ||
System.getProperty("java.version"), | ||
System.getProperty("java.vm.version")); | ||
print.println(); | ||
time = System.currentTimeMillis(); | ||
print.printf(Locale.ROOT, HEADER + "JVM default timezone: %s", | ||
time, time, time, | ||
java.util.TimeZone.getDefault().toString()); | ||
print.println(); | ||
print.flush(); | ||
} | ||
|
||
private static String array(Object[] a) { | ||
if (a == null || a.length == 0) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,23 @@ public void testDebugOut() throws Exception { | |
assertThat(ci.debugOut(), is("jdbc.out")); | ||
} | ||
|
||
public void testDebugBuffered() throws Exception { | ||
JdbcConfiguration ci = ci("jdbc:es://a:1/?debug=true&debug.buffered=false"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
assertThat(ci.baseUri().toString(), is("http://a:1/")); | ||
assertThat(ci.debug(), is(true)); | ||
assertThat(ci.debugBuffered(), is(false)); | ||
|
||
ci = ci("jdbc:es://a:1/?debug=true&debug.buffered=true"); | ||
assertThat(ci.baseUri().toString(), is("http://a:1/")); | ||
assertThat(ci.debug(), is(true)); | ||
assertThat(ci.debugBuffered(), is(true)); | ||
|
||
ci = ci("jdbc:es://a:1/?debug=true"); | ||
assertThat(ci.baseUri().toString(), is("http://a:1/")); | ||
assertThat(ci.debug(), is(true)); | ||
assertThat(ci.debugBuffered(), is(true)); | ||
} | ||
|
||
public void testTypeInParam() throws Exception { | ||
Exception e = expectThrows(JdbcSQLException.class, () -> ci("jdbc:es://a:1/foo/bar/tar?debug=true&debug.out=jdbc.out")); | ||
assertEquals("Unknown parameter [debug.out]; did you mean [debug.output]", e.getMessage()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method changes make it more complicated and hard to follow - there's this
log ==null
being repeated which begs the question, what happens it if it initialized?I think the method is good as it is - it creates a logger or returns one that already exists. Either wrap this method in another one that calls
logSystemInfo
(rename the existing method into something like createLogger and then logger callscreateLogger().logSystemInfo
or wrap that at the consumer site - insideproxy
.However currently the systemInfo is being called per each
Connection
which I think is excessive - we want the system information to be once per log, at the beginning.In which case, the system info should called when a new log is created, essentially after each
new DebugLog
.To keep things incapsulated, instead of calling the constructor, one could call a method
createLog
which internally callsnew DebugLog
and right after calls logSystemInfo.This way any other initialization that would need to occur, would happen in that method regardless of the actual method taking place and only once per logger.
It might make sense to call
flush
after logging the system info.