Skip to content

Commit

Permalink
Add logger (logback) to test-frame (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Kral <[email protected]>
  • Loading branch information
im-konge authored Mar 5, 2024
1 parent f78b733 commit e5f0449
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 6 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<maven.compiler.target>17</maven.compiler.target>

<fabric8.version>6.10.0</fabric8.version>
<logback.version>1.4.12</logback.version>

<maven.spotbugs.version>4.7.3.4</maven.spotbugs.version>
<maven.checkstyle.version>3.3.0</maven.checkstyle.version>
Expand Down Expand Up @@ -171,6 +172,13 @@
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.surefire.version}</version>
</dependency>
<!-- Logger -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
<version>${logback.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
6 changes: 6 additions & 0 deletions test-frame-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<!-- Logger -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public final <T extends HasMetadata> boolean waitResourceCondition(T resource, R
ResourceType<T> type = findResourceType(resource);
boolean[] resourceReady = new boolean[1];

Wait.until(String.format("Resource condition: %s to be fulfilled for resource %s/%s",
Wait.until(String.format("Resource condition: %s to be fulfilled for resource %s/%s",
condition.getConditionName(), resource.getKind(), resource.getMetadata().getName()),
TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, TestFrameConstants.GLOBAL_TIMEOUT,
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void until(String description, long pollIntervalMs, long timeoutMs
*/
public static void until(String description, long pollIntervalMs, long timeoutMs, BooleanSupplier ready,
Runnable onTimeout) {
System.out.println("Waiting for " + description);
LOGGER.info("Waiting for: {}", description);
long deadline = System.currentTimeMillis() + timeoutMs;

String exceptionMessage = null;
Expand All @@ -82,7 +82,7 @@ public static void until(String description, long pollIntervalMs, long timeoutMs

if (++exceptionCount == exceptionAppearanceCount && exceptionMessage != null
&& exceptionMessage.equals(previousExceptionMessage)) {
System.out.println("While waiting for " + description + " exception occurred: " + exceptionMessage);
LOGGER.info("While waiting for: {} exception occurred: {}", description, exceptionMessage);
// log the stacktrace
e.printStackTrace(new PrintWriter(stackTraceError));
} else if (exceptionMessage != null && !exceptionMessage.equals(previousExceptionMessage)
Expand All @@ -98,11 +98,11 @@ public static void until(String description, long pollIntervalMs, long timeoutMs
}
if (timeLeft <= 0) {
if (exceptionCount > 1) {
System.out.println("Exception waiting for " + description + ", " + exceptionMessage);
LOGGER.error("Exception waiting for: {}, {}", description, exceptionMessage);

if (!stackTraceError.toString().isEmpty()) {
// printing handled stacktrace
System.out.println(stackTraceError);
LOGGER.error(String.valueOf(stackTraceError));
}
}
onTimeout.run();
Expand Down
31 changes: 31 additions & 0 deletions test-frame-common/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>

<property name="LOG_DIR" value="${ARTIFACTS_DIR:-/tmp/logs}" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss,GMT} %highlight(%-5p) [%c{0}:%L] %m%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${LOG_DIR}/testFile.log</file>
<append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z',GMT} [%thread] %-5p [%c{0}:%L] %m%n</pattern>
</encoder>
</appender>


<root level="${LOG_LEVEL:-info}">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>

<logger name="io.fabric8.kubernetes.client" level="OFF"/>

<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
</configuration>
6 changes: 6 additions & 0 deletions test-frame-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
<artifactId>maven-failsafe-plugin</artifactId>
<scope>test</scope>
</dependency>
<!-- Logger -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//TODO Implement mock kube to be able to work with RM
@EnableKubernetesMockClient(crud = true)
public class UnitTests {
private KubernetesClient kubernetesClient;
private KubernetesMockServer server;
private static final Logger LOGGER = LoggerFactory.getLogger(UnitTests.class);

@Test
void tmpTest() {
System.out.println("Placeholder for test");
LOGGER.info("Placeholder for test");
}
}
31 changes: 31 additions & 0 deletions test-frame-test/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>

<property name="LOG_DIR" value="${ARTIFACTS_DIR:-/tmp/logs}" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss,GMT} %highlight(%-5p) [%c{0}:%L] %m%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${LOG_DIR}/testFile.log</file>
<append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z',GMT} [%thread] %-5p [%c{0}:%L] %m%n</pattern>
</encoder>
</appender>


<root level="${LOG_LEVEL:-info}">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>

<logger name="io.fabric8.kubernetes.client" level="OFF"/>

<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
</configuration>

0 comments on commit e5f0449

Please sign in to comment.