Results 1 to 2 of 2
  1. #1
    MJalbert is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Question 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:
    Java 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

  2. #2
    MJalbert is offline Member
    Join Date
    Mar 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi all,

    The solution is so simple that it's almost stupid (like usual). The trick is to create a log for every line of your error message. Like this:

    Java Code:
    super.error("Date: "+date+" "+time);
    super.error("Type: "+exceptionType);
    super.error("Description: "+description);
    
    for (int i = 0; i < stackTraceElements.length; i++) {
    	StackTraceElement stackTraceElement = stackTraceElements[i];
    	if (i == 0) {
    		super.error("Error: "+stackTraceElement.toString());
    	} else {
    		super.error("      "+stackTraceElement.toString());
    	}
    }
    super.error("");


    I hope it will help someone one day!

    Thanks,

    MJalbert

Similar Threads

  1. How can i insert a char into a string
    By Jamie in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 08:59 PM
  2. SQL Insert Help!!!!
    By shaungoater in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 03:14 AM
  3. How to insert graph in java
    By valery in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:38 PM
  4. How do insert a Graphic
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:30 AM
  5. how to insert tables into JTextPane
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •