3

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.

6
  • I don't quite get it. Do you want to have that String inside the app you are logging from? Or do you want to get the log output of let's call it "app#1" in "app#2"? Commented May 21, 2013 at 12:38
  • You can change your log4j to print log to the file as well. Commented May 21, 2013 at 12:38
  • I want that string to be used in another activity to show it on screen. Commented May 21, 2013 at 12:43
  • You could create your own Handler and use that to build the string. Commented May 21, 2013 at 12:49
  • Can you give me example? I tried earlier with: ByteArrayOutputStream loggerContent = new ByteArrayOutputStream(); PrintStream prStr = new PrintStream(loggerContent); StreamHandler streamHandler = new StreamHandler(prStr, new SimpleFormatter()); Didn't work. Commented May 21, 2013 at 12:52

1 Answer 1

1
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. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.