Move logger initialization.

pull/192/head
Brett Wooldridge 10 years ago
parent 2e169fafbf
commit 250581cef3

@ -20,6 +20,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
@ -30,6 +31,8 @@ import org.slf4j.LoggerFactory;
*/
public class LeakTask implements Runnable
{
private static final Logger LOGGER = LoggerFactory.getLogger(LeakTask.class);
private final long leakTime;
private final ScheduledFuture<?> scheduledFuture;
private StackTraceElement[] stackTrace;
@ -57,7 +60,7 @@ public class LeakTask implements Runnable
System.arraycopy(stackTrace, 3, trace, 0, trace.length);
LeakException e = new LeakException(trace);
LoggerFactory.getLogger(LeakTask.class).warn("Connection leak detection triggered, stack trace follows", e);
LOGGER.warn("Connection leak detection triggered, stack trace follows", e);
stackTrace = null;
}
}

@ -20,6 +20,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
@ -30,6 +31,8 @@ import org.slf4j.LoggerFactory;
*/
public class LeakTask implements Runnable
{
private static final Logger LOGGER = LoggerFactory.getLogger(LeakTask.class);
private final long leakTime;
private final ScheduledFuture<?> scheduledFuture;
private StackTraceElement[] stackTrace;
@ -57,7 +60,7 @@ public class LeakTask implements Runnable
System.arraycopy(stackTrace, 3, trace, 0, trace.length);
LeakException e = new LeakException(trace);
LoggerFactory.getLogger(LeakTask.class).warn("Connection leak detection triggered, stack trace follows", e);
LOGGER.warn("Connection leak detection triggered, stack trace follows", e);
stackTrace = null;
}
}

@ -93,6 +93,10 @@ public class MiscTest
@Test
public void testLeakDetection() throws SQLException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(LeakTask.class, ps);
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(4);
@ -103,14 +107,12 @@ public class MiscTest
final HikariDataSource ds = new HikariDataSource(config);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(LeakTask.class, ps);
Connection connection = ds.getConnection();
PoolUtilities.quietlySleep(TimeUnit.SECONDS.toMillis(5));
ps.close();
PoolUtilities.quietlySleep(TimeUnit.SECONDS.toMillis(4));
connection.close();
PoolUtilities.quietlySleep(TimeUnit.SECONDS.toMillis(1));
ps.close();
String s = new String(baos.toByteArray());
Assert.assertNotNull("Exception string was null", s);
Assert.assertTrue("Expected exception to contain 'Connection leak detection' but contains\n" + s, s.contains("Connection leak detection"));

Loading…
Cancel
Save