Slf4j logs [FAILED toString()] when a StackOverflowError occurs.
Except if it occurs in addKeyValue, then the exception is fatal:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Scratch {
private static final Logger log = LoggerFactory.getLogger(Scratch.class);
public static void main(String[] args) {
log.atInfo().log("{}", new ToStringOverflow());
System.out.println("slf4j could log");
log.atInfo().setMessage("msg with key/value").addKeyValue("key", new ToStringOverflow()).log();
System.out.println("slf4j could log with addKeyValue");
}
}
class ToStringOverflow {
@Override
public String toString() {
return super.toString() + this.toString(); // deliberately causes StackOverflowError
}
}
14:28:10.399 [main] INFO pkg -- [FAILED toString()]
slf4j could log
Exception in thread "main" java.lang.StackOverflowError
Slf4j logs
[FAILED toString()]when a StackOverflowError occurs.Except if it occurs in
addKeyValue, then the exception is fatal: