这就是我想要做的:
Logger logger = LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Handler handler = new StreamHandler(stream, new SimpleFormatter());
logger.addHandler(handler);
org.slf4j.LoggerFactory.getLogger("com.example").info("foo");
logger.removeHandler(handler);
assert stream.toString().contains("foo");
它是 SLF4J 和 JUL 的混合体。stream
在脚本末尾为空。为什么?