Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why StreamHandler doesn't catch log messages?

What is wrong with this code?

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.logging.SimpleFormatter;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;

Logger log = Logger.getLogger("foo");
log.setUseParentHandlers(false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
log.addHandler(new StreamHandler(baos, new SimpleFormatter()));
log.info("test");
assert baos.toByteArray().length > 0;

It fails since baos is empty. Why it’s empty?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It requires flushing

Handler handler = new StreamHandler(outputStream, new SimpleFormatter());
log.addHandler(handler);
log.info("test");
handler.flush();

Now it’s 44 bytes

By the way, enjoyed the books

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading