Skip to content

Commit

Permalink
Start logging programatically as log4j not started in time
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Jun 6, 2018
1 parent d1d979d commit b6bae67
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
9 changes: 9 additions & 0 deletions framework-tools-replay/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

<modelVersion>4.0.0</modelVersion>

<properties>
<log4j-core.version>2.5</log4j-core.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand Down Expand Up @@ -107,6 +111,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-core.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.justice.framework.tools.replay;

import static org.apache.log4j.Level.DEBUG;
import static org.apache.log4j.Level.INFO;
import static org.apache.log4j.Level.WARN;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerRepository;

public class LoggerConfigurator {

public void startLoggingProgramaticallyAsLog4JNotYetStarted() {
final Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(INFO);
final LoggerRepository loggerRepository = rootLogger.getLoggerRepository();

loggerRepository.getLogger("uk.gov.justice").setLevel(DEBUG);
loggerRepository.getLogger("org.hibernate").setLevel(WARN);
loggerRepository.getLogger("org.apache.deltaspike").setLevel(WARN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ public class StartReplay implements ManagedTaskListener {
@Inject
private AsyncStreamDispatcher asyncStreamDispatcher;

@Inject
private LoggerConfigurator loggerConfigurator;

private Deque<Future<UUID>> outstandingTasks = new LinkedBlockingDeque<>();

private boolean allTasksCreated = false;

@PostConstruct
void go() {

loggerConfigurator.startLoggingProgramaticallyAsLog4JNotYetStarted();

logger.info("-------------- Invoke Event Streams Replay-------------!");
checkForMainProcessFile();

Expand All @@ -64,6 +70,8 @@ void go() {

}



@Override
public void taskAborted(final Future<?> dispatchTaskFuture, final ManagedExecutorService managedExecutorService, final Object dispatchTask, final Throwable throwable) {
logger.debug("Dispatch task aborted");
Expand Down
2 changes: 1 addition & 1 deletion framework-tools-replay/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1 file
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class StartReplayTest {
@Mock
private Logger logger;

@Mock
private LoggerConfigurator loggerConfigurator;

@InjectMocks
private StartReplay startReplay;

Expand All @@ -66,6 +69,7 @@ public void shouldDispatchStreams() throws IOException {

startReplay.go();

verify(loggerConfigurator).startLoggingProgramaticallyAsLog4JNotYetStarted();
verify(executorService, times(2)).submit(any(StreamDispatchTask.class));
verify(logger).info("-------------- Invoke Event Streams Replay-------------!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public class FrameworkToolsTestListener {

@Handles("framework.update-user")
public void handle(final JsonEnvelope envelope) {
logger.info("Replaying event: " + envelope);

logger.debug("Replaying event: " + envelope);
testViewstoreRepository.save(fromJsonEnvelope(envelope));
}

private User fromJsonEnvelope(final JsonEnvelope envelope) {


final String payload = envelope.payloadAsJsonObject().toString();

try {
Expand Down

0 comments on commit b6bae67

Please sign in to comment.