Repository: iluwatar/java-design-patterns
Issue: Reduce build logging
Labels: info: help wanted
epic: build issue
type: refactoring

Currrently, the build with tests enabled produces a zipped log file of about 65MB. This is a bit too large to analyze effectively. Let's try to reduce the logging a bit while keeping the most important parts.

Acceptance criteria

- Build and tests produce more compact logs


Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/monitor/src/main/java/com/iluwatar/monitor/Bank.java b/monitor/src/main/java/com/iluwatar/monitor/Bank.java
index 6ff65f28cb9a..3f19e0f593a7 100644
--- a/monitor/src/main/java/com/iluwatar/monitor/Bank.java
+++ b/monitor/src/main/java/com/iluwatar/monitor/Bank.java
@@ -78,12 +78,14 @@ public synchronized void transfer(int accountA, int accountB, int amount) {
     if (accounts[accountA] >= amount) {
       accounts[accountB] += amount;
       accounts[accountA] -= amount;
-      LOGGER.info(
-          "Transferred from account: {} to account: {} , amount: {} , balance: {}",
-          accountA,
-          accountB,
-          amount,
-          getBalance());
+      if (LOGGER.isDebugEnabled()) {
+        LOGGER.debug(
+            "Transferred from account: {} to account: {} , amount: {} , balance: {}",
+            accountA,
+            accountB,
+            amount,
+            getBalance());
+      }
     }
   }
 
diff --git a/monitor/src/main/java/com/iluwatar/monitor/Main.java b/monitor/src/main/java/com/iluwatar/monitor/Main.java
index 8b2e8ab47997..923c720d8ef1 100644
--- a/monitor/src/main/java/com/iluwatar/monitor/Main.java
+++ b/monitor/src/main/java/com/iluwatar/monitor/Main.java
@@ -25,7 +25,7 @@
 package com.iluwatar.monitor;
 
 import java.security.SecureRandom;
-import java.util.concurrent.ExecutorService;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executors;
 import lombok.extern.slf4j.Slf4j;
 
@@ -40,20 +40,26 @@
 @Slf4j
 public class Main {
 
+  private static final int NUMBER_OF_THREADS = 5;
+
   /**
    * Runner to perform a bunch of transfers and handle exception.
    *
-   * @param bank bank object
+   * @param bank  bank object
+   * @param latch signal finished execution
    */
-  public static void runner(Bank bank) {
+  public static void runner(Bank bank, CountDownLatch latch) {
     try {
       SecureRandom random = new SecureRandom();
       Thread.sleep(random.nextInt(1000));
+      LOGGER.info("Start transferring...");
       for (int i = 0; i < 1000000; i++) {
         bank.transfer(random.nextInt(4), random.nextInt(4), random.nextInt());
       }
+      LOGGER.info("Finished transferring.");
+      latch.countDown();
     } catch (InterruptedException e) {
-      LOGGER.info(e.getMessage());
+      LOGGER.error(e.getMessage());
       Thread.currentThread().interrupt();
     }
   }
@@ -63,12 +69,15 @@ public static void runner(Bank bank) {
    *
    * @param args command line args
    */
-  public static void main(String[] args) {
+  public static void main(String[] args) throws InterruptedException {
     var bank = new Bank(4, 1000);
-    Runnable runnable = () -> runner(bank);
-    ExecutorService executorService = Executors.newFixedThreadPool(5);
-    for (int i = 0; i < 5; i++) {
-      executorService.execute(runnable);
+    var latch = new CountDownLatch(NUMBER_OF_THREADS);
+    var executorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
+
+    for (int i = 0; i < NUMBER_OF_THREADS; i++) {
+      executorService.execute(() -> runner(bank, latch));
     }
+
+    latch.await();
   }
 }
diff --git a/monitor/src/test/resources/logback.xml b/monitor/src/test/resources/logback.xml
new file mode 100644
index 000000000000..ce9892722558
--- /dev/null
+++ b/monitor/src/test/resources/logback.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    The MIT License
+    Copyright © 2014-2022 Ilkka Seppälä
+
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE.
+
+-->
+<configuration>
+    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+    <root level="info">
+        <appender-ref ref="STDOUT" />
+    </root>
+</configuration>


Test patch:
diff --git a/monitor/src/test/java/com/iluwatar/monitor/MainTest.java b/monitor/src/test/java/com/iluwatar/monitor/MainTest.java
index 360a897fd6a6..4d1c217372d7 100644
--- a/monitor/src/test/java/com/iluwatar/monitor/MainTest.java
+++ b/monitor/src/test/java/com/iluwatar/monitor/MainTest.java
@@ -25,6 +25,7 @@
 package com.iluwatar.monitor;
 
 import org.junit.jupiter.api.Test;
+import java.util.concurrent.CountDownLatch;
 import static org.junit.jupiter.api.Assertions.*;
 
 /** Test if the application starts without throwing an exception. */
@@ -38,6 +39,9 @@ void shouldExecuteApplicationWithoutException() {
   @Test
   void RunnerExecuteWithoutException() {
     var bank = new Bank(4, 1000);
-    assertDoesNotThrow(() -> Main.runner(bank));
+    var latch = new CountDownLatch(1);
+
+    assertDoesNotThrow(() -> Main.runner(bank, latch));
+    assertEquals(0, latch.getCount());
   }
 }