nihilvex
nihilvex

Reputation: 231

Logger output to string

I'm pretty new to java and my question is probably very noobish, but I can't figure it out.

I'm having a Logger logger = Logger.getLogger(MyClass.class.getName());

And I want to get the output of that logger as a string. For example when I do

logger.info("Some message"); 
logger.warning(" Some warning");

I want to get a String someString = "Some message SomeWarning"

I'm looking for something similar to

ByteArrayOutputStream loggerContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(loggerContent));
TextView tv = (TextView)findViewById(R.id.txt_1);
tv.setText(loggerContent.toString());

But I'm failing to achieve it. Is it possible and how?

EDIT: I want that string to be used in another activity to show it on screen.

Upvotes: 3

Views: 4399

Answers (1)

nihilvex
nihilvex

Reputation: 231

ByteArrayOutputStream loggerContent = new ByteArrayOutputStream(); 
PrintStream prStr = new PrintStream(loggerContent); 
StreamHandler streamHandler = new StreamHandler(prStr, new SimpleFormatter());

I tried that before I posted here and couldn't get the result. It actually works, but I had to .flush() the StreamHandler before trying loggerContent.toString(). Thanks for the responses guys. :)

Upvotes: 1

Related Questions