Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Logger value in a string array

I used the log4j.Logger and I have written the log value in to a file using the FileAppender.

Now I want to show the log value in a textarea. How can I assign the logger value to an array of Strings?

like image 508
praba Avatar asked Sep 13 '12 09:09

praba


People also ask

How do I print a string array in logs?

Read a line of the csv file and call String. split(",") on the line which will return you an array of each seperate string in the line you just read then you can simply loop through the array and print them out as you need.

How do you log a string in Java?

log(Level level, String msg, Object[] params): This method is used to Log a message, with an array of object arguments. Output: log(Level level, String msg, Throwable thrown): This method is used to Log a message, with associated Throwable information.

How do I check logger information?

The info() method of a Logger class is used to Log an INFO message. This method is used to forward logs to all the registered output Handler objects.

How do I print multiple values in logger?

In case, you are using logger, then you can use log.info() to print multiple variables. Obviously, you can choose logging level based on your preference.


1 Answers

You can add an additional Appender to your Logger. Use a WriterAppender to write to a StringWriter then you change the content of your TextArea to the value of StringWriter#toString()

Example Code:

public static void main(String[] args) {
    Logger logger = Logger.getLogger("logger");
    Layout layout = new PatternLayout();
    StringWriter stringWriter = new StringWriter();
    WriterAppender writerAppender = new WriterAppender(layout, stringWriter);
    logger.addAppender(writerAppender);
    TextArea textArea = new TextArea();

    logger.error("test");
    //if(stringWriter changed){// compare to old TextArea-content?
      textArea.setText(stringWriter.toString());
    //}
    //System.out.println(stringWriter.toString());
  }

you need to find a smart way update your TextArea depending on the logLevel and if the content of the StringWriter changed.

Update: A comparable solution is presented here.

like image 103
Simulant Avatar answered Oct 07 '22 10:10

Simulant