improvements to SLF4J-515

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
This commit is contained in:
Ceki Gulcu 2021-07-09 09:59:48 +02:00
parent b6fb6e3b1d
commit e4fd949462
3 changed files with 19 additions and 16 deletions

View File

@ -26,8 +26,6 @@ package org.slf4j.impl;
import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.event.LoggingEvent;
@ -314,6 +312,13 @@ public class SimpleLogger extends MarkerIgnoringBase {
throw new IllegalStateException("Unrecognized level [" + level + "]");
}
/**
* To avoid intermingling of log messages and associated stack traces, the two
* operations are done in a synchronized block.
*
* @param buf
* @param t
*/
void write(StringBuilder buf, Throwable t) {
PrintStream targetStream = CONFIG_PARAMS.outputChoice.getTargetPrintStream();

View File

@ -40,6 +40,8 @@ import org.slf4j.LoggerFactory;
public class MultithereadedExecutionTest {
private static int THREAD_COUNT = 2;
private static long TEST_DURATION_IN_MILLIS = 100;
private Thread[] threads = new Thread[THREAD_COUNT];
private final PrintStream oldOut = System.out;
@ -69,7 +71,7 @@ public class MultithereadedExecutionTest {
threads[1] = new Thread(other);
threads[0].start();
threads[1].start();
Thread.sleep(100);
Thread.sleep(TEST_DURATION_IN_MILLIS);
signal = true;
threads[0].join();
threads[1].join();
@ -86,10 +88,9 @@ public class MultithereadedExecutionTest {
class WithException implements Runnable {
Throwable throwable;
volatile Throwable throwable;
Logger logger = LoggerFactory.getLogger(WithException.class);
@Override
public void run() {
int i = 0;
@ -108,10 +109,9 @@ public class MultithereadedExecutionTest {
}
class Other implements Runnable {
Throwable throwable;
volatile Throwable throwable;
Logger logger = LoggerFactory.getLogger(Other.class);
@Override
public void run() {
int i = 0;
while (!signal) {
@ -125,5 +125,4 @@ public class MultithereadedExecutionTest {
}
}
}
}

View File

@ -25,20 +25,20 @@
package org.slf4j.simple.multiThreadedExecution;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
/**
* This PrintStream checks that output lines are in an expected order.
*
* @author ceki
*/
public class StateCheckingPrintStream extends PrintStream {
enum State {
INITIAL, UNKNOWN, HELLO, THROWABLE, AT1, AT2, OTHER;
}
List<String> stringList = Collections.synchronizedList(new ArrayList<String>());
State currentState = State.INITIAL;
volatile State currentState = State.INITIAL;
public StateCheckingPrintStream(PrintStream ps) {
super(ps);
@ -57,6 +57,7 @@ public class StateCheckingPrintStream extends PrintStream {
break;
case UNKNOWN:
// ignore garbage
currentState = next;
break;
@ -98,8 +99,6 @@ public class StateCheckingPrintStream extends PrintStream {
default:
throw new IllegalStateException("Unreachable code");
}
stringList.add(s);
}
private IllegalStateException badState(String s, State currentState2, State next) {