JAVA-18116 Review log statements for projects - Week 7 - 2023 (#13583)

JAVA-18116 Review log statements for projects - Week 7 - 2023 (#13583)
---------

Co-authored-by: jogra <joseph.sterling.grah@miles.no>
This commit is contained in:
jsgrah-spring 2023-04-10 13:35:40 +02:00 committed by GitHub
parent 26790b429f
commit 23c1abe3f2
12 changed files with 99 additions and 23 deletions

View File

@ -1,9 +1,14 @@
package com.baeldung.concurrent.countdownlatch; package com.baeldung.concurrent.countdownlatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
public class Worker implements Runnable { public class Worker implements Runnable {
private static Logger log = LoggerFactory.getLogger(Worker.class);
private final List<String> outputScraper; private final List<String> outputScraper;
private final CountDownLatch countDownLatch; private final CountDownLatch countDownLatch;
@ -15,7 +20,7 @@ public class Worker implements Runnable {
@Override @Override
public void run() { public void run() {
// Do some work // Do some work
System.out.println("Doing some logic"); log.debug("Doing some logic");
outputScraper.add("Counted down"); outputScraper.add("Counted down");
countDownLatch.countDown(); countDownLatch.countDown();
} }

View File

@ -1,8 +1,13 @@
package com.baeldung.concurrent.phaser; package com.baeldung.concurrent.phaser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Phaser; import java.util.concurrent.Phaser;
class LongRunningAction implements Runnable { class LongRunningAction implements Runnable {
private static Logger log = LoggerFactory.getLogger(LongRunningAction.class);
private String threadName; private String threadName;
private Phaser ph; private Phaser ph;
@ -14,18 +19,18 @@ class LongRunningAction implements Runnable {
@Override @Override
public void run() { public void run() {
System.out.println("This is phase " + ph.getPhase()); log.info("This is phase {}", ph.getPhase());
System.out.println("Thread " + threadName + " before long running action"); log.info("Thread {} before long running action", threadName);
try { try {
Thread.sleep(2000); Thread.sleep(2000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("Thread " + threadName + " action completed and waiting for others"); log.debug("Thread {} action completed and waiting for others", threadName);
ph.arriveAndAwaitAdvance(); ph.arriveAndAwaitAdvance();
System.out.println("Thread " + threadName + " proceeding in phase " + ph.getPhase()); log.debug("Thread {} proceeding in phase {}", threadName, ph.getPhase());
ph.arriveAndDeregister(); ph.arriveAndDeregister();
} }

View File

@ -4,6 +4,9 @@ import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Phaser; import java.util.concurrent.Phaser;
@ -13,6 +16,8 @@ import static junit.framework.TestCase.assertEquals;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PhaserUnitTest { public class PhaserUnitTest {
private static Logger log = LoggerFactory.getLogger(PhaserUnitTest.class);
@Test @Test
public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() { public void givenPhaser_whenCoordinateWorksBetweenThreads_thenShouldCoordinateBetweenMultiplePhases() {
//given //given
@ -26,19 +31,19 @@ public class PhaserUnitTest {
executorService.submit(new LongRunningAction("thread-3", ph)); executorService.submit(new LongRunningAction("thread-3", ph));
//then //then
System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others"); log.debug("Thread {} waiting for others", Thread.currentThread().getName());
ph.arriveAndAwaitAdvance(); ph.arriveAndAwaitAdvance();
System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase()); log.debug("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase());
assertEquals(1, ph.getPhase()); assertEquals(1, ph.getPhase());
//and //and
executorService.submit(new LongRunningAction("thread-4", ph)); executorService.submit(new LongRunningAction("thread-4", ph));
executorService.submit(new LongRunningAction("thread-5", ph)); executorService.submit(new LongRunningAction("thread-5", ph));
System.out.println("Thread " + Thread.currentThread().getName() + " waiting for others"); log.debug("Thread {} waiting for others", Thread.currentThread().getName());
ph.arriveAndAwaitAdvance(); ph.arriveAndAwaitAdvance();
System.out.println("Thread " + Thread.currentThread().getName() + " proceeding in phase " + ph.getPhase()); log.debug("Thread {} proceeding in phase {}", Thread.currentThread().getName(), ph.getPhase());
assertEquals(2, ph.getPhase()); assertEquals(2, ph.getPhase());

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -23,6 +23,11 @@
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>${commons-io.version}</version> <version>${commons-io.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>

View File

@ -22,6 +22,9 @@ import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CommonsIOUnitTest { public class CommonsIOUnitTest {
@Test @Test
@ -47,9 +50,9 @@ public class CommonsIOUnitTest {
String extension = FilenameUtils.getExtension(path); String extension = FilenameUtils.getExtension(path);
String baseName = FilenameUtils.getBaseName(path); String baseName = FilenameUtils.getBaseName(path);
System.out.println("full path" + fullPath); log.debug("full path: " + fullPath);
System.out.println("Extension" + extension); log.debug("Extension: " + extension);
System.out.println("Base name" + baseName); log.debug("Base name: " + baseName);
} }
@Test @Test

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -1,5 +1,8 @@
package com.baeldung.templatemethod.model; package com.baeldung.templatemethod.model;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HighEndComputerBuilder extends ComputerBuilder { public class HighEndComputerBuilder extends ComputerBuilder {
@Override @Override
@ -11,7 +14,7 @@ public class HighEndComputerBuilder extends ComputerBuilder {
public void setupMotherboard() { public void setupMotherboard() {
motherboardSetupStatus.add("Screwing the high-end motherboard to the case."); motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors."); motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step)); motherboardSetupStatus.forEach(step -> log.debug(step));
} }
@Override @Override

View File

@ -1,5 +1,8 @@
package com.baeldung.templatemethod.model; package com.baeldung.templatemethod.model;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class StandardComputerBuilder extends ComputerBuilder { public class StandardComputerBuilder extends ComputerBuilder {
@Override @Override
@ -11,7 +14,7 @@ public class StandardComputerBuilder extends ComputerBuilder {
public void setupMotherboard() { public void setupMotherboard() {
motherboardSetupStatus.add("Screwing the standard motherboard to the case."); motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors."); motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step)); motherboardSetupStatus.forEach(step -> log.debug(step));
} }
@Override @Override

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -2,6 +2,8 @@ package com.baeldung.spring.drools.service;
import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -11,6 +13,8 @@ import com.baeldung.spring.drools.model.TaxiRide;
@Service @Service
public class TaxiFareCalculatorService { public class TaxiFareCalculatorService {
private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareCalculatorService.class);
@Autowired @Autowired
private KieContainer kContainer; private KieContainer kContainer;
@ -20,7 +24,7 @@ public class TaxiFareCalculatorService {
kieSession.insert(taxiRide); kieSession.insert(taxiRide);
kieSession.fireAllRules(); kieSession.fireAllRules();
kieSession.dispose(); kieSession.dispose();
System.out.println("!! RIDE FARE !! " + rideFare.getTotalFare()); LOGGER.debug("!! RIDE FARE !! " + rideFare.getTotalFare());
return rideFare.getTotalFare(); return rideFare.getTotalFare();
} }
} }

View File

@ -45,18 +45,18 @@ public class VertxWithRxJavaIntegrationTest {
// read the file that contains one city name per line // read the file that contains one city name per line
fileSystem fileSystem
.rxReadFile("cities.txt").toFlowable() .rxReadFile("cities.txt").toFlowable()
.doOnNext(buffer -> log.info("File buffer ---\n{}\n---", buffer)) .doOnNext(buffer -> log.debug("File buffer ---\n{}\n---", buffer))
.flatMap(buffer -> Flowable.fromArray(buffer.toString().split("\\r?\\n"))) .flatMap(buffer -> Flowable.fromArray(buffer.toString().split("\\r?\\n")))
.doOnNext(city -> log.info("City from file: '{}'", city)) .doOnNext(city -> log.debug("City from file: '{}'", city))
.filter(city -> !city.startsWith("#")) .filter(city -> !city.startsWith("#"))
.doOnNext(city -> log.info("City that survived filtering: '{}'", city)) .doOnNext(city -> log.debug("City that survived filtering: '{}'", city))
.flatMap(city -> searchByCityName(httpClient, city)) .flatMap(city -> searchByCityName(httpClient, city))
.flatMap(HttpClientResponse::toFlowable) .flatMap(HttpClientResponse::toFlowable)
.doOnNext(buffer -> log.info("JSON of city detail: '{}'", buffer)) .doOnNext(buffer -> log.debug("JSON of city detail: '{}'", buffer))
.map(extractingWoeid()) .map(extractingWoeid())
.flatMap(cityId -> getDataByPlaceId(httpClient, cityId)) .flatMap(cityId -> getDataByPlaceId(httpClient, cityId))
.flatMap(toBufferFlowable()) .flatMap(toBufferFlowable())
.doOnNext(buffer -> log.info("JSON of place detail: '{}'", buffer)) .doOnNext(buffer -> log.debug("JSON of place detail: '{}'", buffer))
.map(Buffer::toJsonObject) .map(Buffer::toJsonObject)
.map(toCityAndDayLength()) .map(toCityAndDayLength())
.subscribe(System.out::println, Throwable::printStackTrace); .subscribe(System.out::println, Throwable::printStackTrace);