Merge pull request #11750 from hkhan/JAVA-9019-clean-up-log-statements

Java 9019 clean up log statements
This commit is contained in:
kwoyke 2022-02-02 11:21:55 +01:00 committed by GitHub
commit 896d32c182
12 changed files with 95 additions and 82 deletions

View File

@ -1,17 +1,16 @@
package com.baeldung.collections.iterators; package com.baeldung.collections.iterators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/**
* Source code https://github.com/eugenp/tutorials
*
* @author Santosh Thakur
*/
public class Iterators { public class Iterators {
private static final Logger LOG = LoggerFactory.getLogger(Iterators.class);
public static int failFast1() { public static int failFast1() {
ArrayList<Integer> numbers = new ArrayList<>(); ArrayList<Integer> numbers = new ArrayList<>();
@ -44,7 +43,7 @@ public class Iterators {
} }
} }
System.out.println("using iterator's remove method = " + numbers); LOG.debug("using iterator's remove method = {}", numbers);
iterator = numbers.iterator(); iterator = numbers.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {

View File

@ -3,6 +3,8 @@ package com.baeldung.collections.bitset;
import org.junit.Test; import org.junit.Test;
import org.openjdk.jol.info.ClassLayout; import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout; import org.openjdk.jol.info.GraphLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.BitSet; import java.util.BitSet;
@ -10,18 +12,20 @@ import static org.assertj.core.api.Assertions.assertThat;
public class BitSetUnitTest { public class BitSetUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(BitSetUnitTest.class);
@Test @Test
public void givenBoolArray_whenMemoryLayout_thenConsumeMoreThanOneBit() { public void givenBoolArray_whenMemoryLayout_thenConsumeMoreThanOneBit() {
boolean[] bits = new boolean[1024 * 1024]; boolean[] bits = new boolean[1024 * 1024];
System.out.println(ClassLayout.parseInstance(bits).toPrintable()); LOG.debug(ClassLayout.parseInstance(bits).toPrintable());
} }
@Test @Test
public void givenBitSet_whenMemoryLayout_thenConsumeOneBitPerFlag() { public void givenBitSet_whenMemoryLayout_thenConsumeOneBitPerFlag() {
BitSet bitSet = new BitSet(1024 * 1024); BitSet bitSet = new BitSet(1024 * 1024);
System.out.println(GraphLayout.parseInstance(bitSet).toPrintable()); LOG.debug(GraphLayout.parseInstance(bitSet).toPrintable());
} }
@Test @Test
@ -157,7 +161,7 @@ public class BitSetUnitTest {
BitSet bitSet = new BitSet(); BitSet bitSet = new BitSet();
bitSet.set(15, 25); bitSet.set(15, 25);
bitSet.stream().forEach(System.out::println); bitSet.stream().forEach(bit -> LOG.debug(String.valueOf(bit)));
assertThat(bitSet.stream().count()).isEqualTo(10); assertThat(bitSet.stream().count()).isEqualTo(10);
} }

View File

@ -0,0 +1,13 @@
<?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>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -1,9 +1,15 @@
package com.baeldung.concurrent.prioritytaskexecution; package com.baeldung.concurrent.prioritytaskexecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Job implements Runnable { public class Job implements Runnable {
private String jobName;
private JobPriority jobPriority; private static final Logger LOGGER = LoggerFactory.getLogger(Job.class);
private final String jobName;
private final JobPriority jobPriority;
public Job(String jobName, JobPriority jobPriority) { public Job(String jobName, JobPriority jobPriority) {
this.jobName = jobName; this.jobName = jobName;
this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM; this.jobPriority = jobPriority != null ? jobPriority : JobPriority.MEDIUM;
@ -16,8 +22,7 @@ public class Job implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
System.out.println("Job:" + jobName + LOGGER.debug("Job:{} Priority:{}", jobName, jobPriority);
" Priority:" + jobPriority);
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }

View File

@ -1,19 +1,21 @@
package com.baeldung.forkjoin; package com.baeldung.forkjoin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveAction; import java.util.concurrent.RecursiveAction;
import java.util.logging.Logger;
public class CustomRecursiveAction extends RecursiveAction { public class CustomRecursiveAction extends RecursiveAction {
final Logger logger = LoggerFactory.getLogger(CustomRecursiveAction.class);
private String workLoad = ""; private String workLoad = "";
private static final int THRESHOLD = 4; private static final int THRESHOLD = 4;
private static Logger logger = Logger.getAnonymousLogger();
public CustomRecursiveAction(String workLoad) { public CustomRecursiveAction(String workLoad) {
this.workLoad = workLoad; this.workLoad = workLoad;
} }
@ -43,7 +45,7 @@ public class CustomRecursiveAction extends RecursiveAction {
private void processing(String work) { private void processing(String work) {
String result = work.toUpperCase(); String result = work.toUpperCase();
logger.info("This result - (" + result + ") - was processed by " + Thread.currentThread() logger.debug("This result - (" + result + ") - was processed by " + Thread.currentThread()
.getName()); .getName());
} }
} }

View File

@ -3,9 +3,9 @@ package com.baeldung.concurrent.prioritytaskexecution;
import org.junit.Test; import org.junit.Test;
public class PriorityJobSchedulerUnitTest { public class PriorityJobSchedulerUnitTest {
private static int POOL_SIZE = 1; private static final int POOL_SIZE = 1;
private static int QUEUE_SIZE = 10; private static final int QUEUE_SIZE = 10;
@Test @Test
public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() { public void whenMultiplePriorityJobsQueued_thenHighestPriorityJobIsPicked() {
Job job1 = new Job("Job1", JobPriority.LOW); Job job1 = new Job("Job1", JobPriority.LOW);
@ -14,19 +14,19 @@ public class PriorityJobSchedulerUnitTest {
Job job4 = new Job("Job4", JobPriority.MEDIUM); Job job4 = new Job("Job4", JobPriority.MEDIUM);
Job job5 = new Job("Job5", JobPriority.LOW); Job job5 = new Job("Job5", JobPriority.LOW);
Job job6 = new Job("Job6", JobPriority.HIGH); Job job6 = new Job("Job6", JobPriority.HIGH);
PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE); PriorityJobScheduler pjs = new PriorityJobScheduler(POOL_SIZE, QUEUE_SIZE);
pjs.scheduleJob(job1); pjs.scheduleJob(job1);
pjs.scheduleJob(job2); pjs.scheduleJob(job2);
pjs.scheduleJob(job3); pjs.scheduleJob(job3);
pjs.scheduleJob(job4); pjs.scheduleJob(job4);
pjs.scheduleJob(job5); pjs.scheduleJob(job5);
pjs.scheduleJob(job6); pjs.scheduleJob(job6);
// ensure no tasks is pending before closing the scheduler // ensure no tasks is pending before closing the scheduler
while (pjs.getQueuedTaskCount() != 0); while (pjs.getQueuedTaskCount() != 0);
// delay to avoid job sleep (added for demo) being interrupted // delay to avoid job sleep (added for demo) being interrupted
try { try {
Thread.sleep(2000); Thread.sleep(2000);
@ -34,7 +34,7 @@ public class PriorityJobSchedulerUnitTest {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new RuntimeException(e); throw new RuntimeException(e);
} }
pjs.closeScheduler(); pjs.closeScheduler();
} }
} }

View File

@ -1,18 +1,15 @@
package com.baeldung.forkjoin; package com.baeldung.forkjoin;
import static org.junit.Assert.assertEquals; import com.baeldung.forkjoin.util.PoolUtil;
import static org.junit.Assert.assertNotNull; import org.junit.Before;
import static org.junit.Assert.assertTrue; import org.junit.Test;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinPool;
import org.junit.Before; import static org.junit.Assert.assertEquals;
import org.junit.Test; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.baeldung.forkjoin.CustomRecursiveAction;
import com.baeldung.forkjoin.CustomRecursiveTask;
import com.baeldung.forkjoin.util.PoolUtil;
public class Java8ForkJoinIntegrationTest { public class Java8ForkJoinIntegrationTest {
@ -63,11 +60,11 @@ public class Java8ForkJoinIntegrationTest {
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
forkJoinPool.execute(customRecursiveTask); forkJoinPool.execute(customRecursiveTask);
int result = customRecursiveTask.join(); customRecursiveTask.join();
assertTrue(customRecursiveTask.isDone()); assertTrue(customRecursiveTask.isDone());
forkJoinPool.submit(customRecursiveTask); forkJoinPool.submit(customRecursiveTask);
int resultTwo = customRecursiveTask.join(); customRecursiveTask.join();
assertTrue(customRecursiveTask.isDone()); assertTrue(customRecursiveTask.isDone());
} }

View File

@ -1,12 +1,12 @@
package com.baeldung.thread.join; package com.baeldung.thread.join;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.logging.Logger;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/** /**
* Demonstrates Thread.join behavior. * Demonstrates Thread.join behavior.
@ -14,55 +14,55 @@ import org.junit.Test;
*/ */
public class ThreadJoinUnitTest { public class ThreadJoinUnitTest {
final static Logger LOGGER = Logger.getLogger(ThreadJoinUnitTest.class.getName()); private static final Logger LOGGER = LoggerFactory.getLogger(ThreadJoinUnitTest.class);
class SampleThread extends Thread { static class SampleThread extends Thread {
public int processingCount = 0; public int processingCount;
SampleThread(int processingCount) { SampleThread(int processingCount) {
this.processingCount = processingCount; this.processingCount = processingCount;
LOGGER.info("Thread " + this.getName() + " created"); LOGGER.debug("Thread " + this.getName() + " created");
} }
@Override @Override
public void run() { public void run() {
LOGGER.info("Thread " + this.getName() + " started"); LOGGER.debug("Thread " + this.getName() + " started");
while (processingCount > 0) { while (processingCount > 0) {
try { try {
Thread.sleep(1000); // Simulate some work being done by thread Thread.sleep(1000); // Simulate some work being done by thread
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOGGER.info("Thread " + this.getName() + " interrupted."); LOGGER.debug("Thread " + this.getName() + " interrupted.");
} }
processingCount--; processingCount--;
LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount); LOGGER.debug("Inside Thread " + this.getName() + ", processingCount = " + processingCount);
} }
LOGGER.info("Thread " + this.getName() + " exiting"); LOGGER.debug("Thread " + this.getName() + " exiting");
} }
} }
@Test @Test
public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException { public void givenNewThread_whenJoinCalled_returnsImmediately() throws InterruptedException {
Thread t1 = new SampleThread(0); Thread t1 = new SampleThread(0);
LOGGER.info("Invoking join."); LOGGER.debug("Invoking join.");
t1.join(); t1.join();
LOGGER.info("Returned from join"); LOGGER.debug("Returned from join");
LOGGER.info("Thread state is" + t1.getState()); LOGGER.debug("Thread state is" + t1.getState());
assertFalse(t1.isAlive()); assertFalse(t1.isAlive());
} }
@Test @Test
public void givenStartedThread_whenJoinCalled_waitsTillCompletion() public void givenStartedThread_whenJoinCalled_waitsTillCompletion()
throws InterruptedException { throws InterruptedException {
Thread t2 = new SampleThread(1); Thread t2 = new SampleThread(1);
t2.start(); t2.start();
LOGGER.info("Invoking join."); LOGGER.debug("Invoking join.");
t2.join(); t2.join();
LOGGER.info("Returned from join"); LOGGER.debug("Returned from join");
assertFalse(t2.isAlive()); assertFalse(t2.isAlive());
} }
@Test @Test
public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout() public void givenStartedThread_whenTimedJoinCalled_waitsUntilTimedout()
throws InterruptedException { throws InterruptedException {
Thread t3 = new SampleThread(10); Thread t3 = new SampleThread(10);
t3.start(); t3.start();
@ -72,18 +72,18 @@ public class ThreadJoinUnitTest {
@Test @Test
@Ignore @Ignore
public void givenThreadTerminated_checkForEffect_notGuaranteed() public void givenThreadTerminated_checkForEffect_notGuaranteed()
throws InterruptedException { throws InterruptedException {
SampleThread t4 = new SampleThread(10); SampleThread t4 = new SampleThread(10);
t4.start(); t4.start();
//not guaranteed to stop even if t4 finishes. //not guaranteed to stop even if t4 finishes.
do { do {
} while (t4.processingCount > 0); } while (t4.processingCount > 0);
} }
@Test @Test
public void givenJoinWithTerminatedThread_checkForEffect_guaranteed() public void givenJoinWithTerminatedThread_checkForEffect_guaranteed()
throws InterruptedException { throws InterruptedException {
SampleThread t4 = new SampleThread(10); SampleThread t4 = new SampleThread(10);
t4.start(); t4.start();

View File

@ -0,0 +1,13 @@
<?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>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -1,6 +0,0 @@
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

View File

@ -1,9 +0,0 @@
# Root logger
log4j.rootLogger=INFO, file, stdout
# Write to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

View File

@ -7,13 +7,8 @@
</encoder> </encoder>
</appender> </appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<logger name="net.sf.jmimemagic" level="WARN" /> <logger name="net.sf.jmimemagic" 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"> <root level="INFO">
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT" />
</root> </root>