Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-05-2008, 06:09 PM
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-06-2008, 02:03 PM
Member
 
Join Date: Mar 2008
Posts: 3
MJalbert is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Insert Help!!!! shaungoater New To Java 0 03-20-2008 12:04 PM
How to insert graph in java valery Advanced Java 1 08-06-2007 09:38 PM
How do insert a Graphic carl New To Java 1 08-01-2007 06:30 AM
how to insert tables into JTextPane osval AWT / Swing 1 07-29-2007 10:11 AM
How can i insert a char into a string Jamie New To Java 2 05-20-2007 10:01 PM


All times are GMT +3. The time now is 09:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org