mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-21 07:06:19 +00:00
#1402 - Refactor SQL Logging internals (io.ebean.SQL) such that it is easier to capture for testing/asserts
This commit is contained in:
commit
3f8d705439
22 files changed
+305
-131
No files matched your search
@@ -1,13 +1,7 @@
|
||||
package org.ebeantest;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.UnsynchronizedAppenderBase;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import io.ebeantest.LoggedSql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -20,28 +14,11 @@ import java.util.List;
|
||||
*/
|
||||
public class LoggedSqlCollector {
|
||||
|
||||
static Logger logger = (Logger) LoggerFactory.getLogger("io.ebean.SQL");
|
||||
|
||||
private static BasicAppender basicAppender = new BasicAppender();
|
||||
|
||||
static {
|
||||
|
||||
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
|
||||
|
||||
basicAppender.setContext(lc);
|
||||
Level level = logger.getEffectiveLevel();
|
||||
if (level.isGreaterOrEqual(Level.INFO)) {
|
||||
logger.setAdditive(false);
|
||||
}
|
||||
logger.addAppender(basicAppender);
|
||||
logger.setLevel(Level.TRACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start collection of the logged SQL statements.
|
||||
*/
|
||||
public static List<String> start() {
|
||||
return basicAppender.collectStart();
|
||||
return LoggedSql.start();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,54 +26,11 @@ public class LoggedSqlCollector {
|
||||
* the SQL.
|
||||
*/
|
||||
public static List<String> stop() {
|
||||
return basicAppender.collectEnd();
|
||||
return LoggedSql.stop();
|
||||
}
|
||||
|
||||
public static List<String> current() {
|
||||
return basicAppender.collectContinue();
|
||||
return LoggedSql.collect();
|
||||
}
|
||||
|
||||
private static class BasicAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
|
||||
|
||||
List<String> messages = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
if (started) {
|
||||
messages.add(eventObject.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start collection.
|
||||
*/
|
||||
List<String> collectStart() {
|
||||
List<String> tempMessages = messages;
|
||||
messages = new ArrayList<>();
|
||||
// set started flag
|
||||
start();
|
||||
return tempMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* End collection.
|
||||
*/
|
||||
List<String> collectEnd() {
|
||||
// set stopped state
|
||||
stop();
|
||||
List<String> tempMessages = messages;
|
||||
messages = new ArrayList<>();
|
||||
return tempMessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the collected SQL and continue.
|
||||
*/
|
||||
List<String> collectContinue() {
|
||||
List<String> tempMessages = messages;
|
||||
messages = new ArrayList<>();
|
||||
return tempMessages;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user