View Single Post
  #1 (permalink)  
Old 03-05-2008, 06:09 PM
MJalbert MJalbert is offline
Member
 
Join Date: Mar 2008
Posts: 3
MJalbert is on a distinguished road
How to insert a Linebreak in a log4j message?
Hi all,

I need some help with the log4j pattern. I have to build the message with all the stacktrace aligned and when I try to use the "\n" or Character.LINE_SEPARATOR, it gives me the unknown character (square) in my log file instead of the line break (I use a FileAppender).

Here is my code:
Code:
String date = CalendarUtil.getActualDate(); String time = CalendarUtil.getActualTime(); StringBuffer sb = new StringBuffer(); byte lb = Character.LINE_SEPARATOR; sb.append("Date: "date" "+time+lb); sb.append("Type: "+exceptionType+lb); sb.append("Description: "+description+lb); for (int i = 0; i < stackTraceElements.length; i++) { StackTraceElement stackTraceElement = stackTraceElements; if (i == 0) { sb.append("Error: "+stackTraceElement.toString()+lb); } else { sb.append(" "+stackTraceElement.toString()+lb); } } super.error(sb.toString());

And here is the result (change the # for squares):

Date: 2008-03-04 16:02:49#Type: java.lang.Exception#Description: My hardcoded message.#Error: main.view.ExecuteTask.main(ExecuteTask.java:46)

I can't create a pattern with the line break (%NL) because every stacktrace has a diferent number of line.

Is anyone knows why it's giving me that result and how could I fix that?

Thanks a lot,


MJalbert
Reply With Quote
Sponsored Links