Skip to content

Instantly share code, notes, and snippets.

@ttddyy
Created July 9, 2018 23:35
Show Gist options
  • Save ttddyy/abccd4ade6adc5683fd23536a47e4087 to your computer and use it in GitHub Desktop.
Save ttddyy/abccd4ade6adc5683fd23536a47e4087 to your computer and use it in GitHub Desktop.
public class MyLogEntryCreator extends DefaultQueryLogEntryCreator {
private boolean writeDataSourcename = false;
private boolean writeConnectionId = true;
public String getLogEntry(String sessionId, ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
StringBuilder sb = new StringBuilder();
sb.append("SessionId=" + sessionId);
sb.append(" ");
sb.append(super.getLogEntry(execInfo, queryInfoList, this.writeDataSourcename, this.writeConnectionId));
return sb.toString();
}
}
public class MyLoggingListener implements QueryExecutionListener {
private MyLogEntryCreator queryLogEntryCreator = new MyLogEntryCreator();
// NOTE:
// This implementation makes extra call to retrieve session id on every query execution.
// This should be only called when session-id changes. For example it could be implemented by spring AOP
// or Hibernate interceptor/listener. Then store the session ID to somewhere appropriate to your
// transaction/connection management, for example thread-local, and should be referenced in "afterQuery" method.
@Override
public void beforeQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
String sessionId = "N/A";
try {
// Retrieve session id logic comes here
Connection connection = execInfo.getStatement().getConnection();
} catch (SQLException e) {
// failed to get session-id
}
String entry = this.queryLogEntryCreator.getLogEntry(sessionId, execInfo, queryInfoList);
System.out.println(entry);
}
@Override
public void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment