tags:

views:

28

answers:

2

I am using log4j to log request/response XMLs sent by my application.

The problem is that the xml contents are getting logged with newlines. Any suggestions as to how to format so as to preserve the xml structure as it is received??

Following is my appender:

<appender name="xmlFileAppender" class="org.apache.log4j.RollingFileAppender">
   <param name="File" value="d:/LOG/log4j/appName_${weblogic.Name}_XMLS.log" />
   <param name="Append" value="true" />
   <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
       <param name="ActiveFileName" value="d:/LOG/log4j/appName_${weblogic.Name}_XMLS.log" />
       <param name="FileNamePattern" value="d:/LOG/log4j/appName_${weblogic.Name}_XMLS.%d.log.gz" />
   </rollingPolicy>
   <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="[%d{DATE}][%-p][%c{3}%x][%m]%n"/>
   </layout>
</appender>
A: 

Your conversion pattern appends a newlines. Try changing this:

 <param name="ConversionPattern" value="[%d{DATE}][%-p][%c{3}%x][%m]%n"/>

To this:

<param name="ConversionPattern" value="[%d{DATE}][%-p][%c{3}%x][%m]"/>

because it sounds like removing the %n will do what you need. Of course, note that any log statement requiring a newline AND using this appender, would need to add it explicitly, as in: log.debug(msg + "\n"); which can be made generic, if necessary:

final String newLine = System.getProperty("line.separator");
...
log.debug(msg + newLine);

I hope that helps,

-gMale

gmale
sorry, i think my question wasnt clear enough. I want to remove the new lines appearing within the log msg itself. Your suggestion to modify the conversion pattern would result in the log getting created as a single line (not what i want).
Nirmal Patel
Ok. I'm still not clear on what you're asking. It sounds like, you could either remove the newlines when you make logging calls, or you can override the logger and remove the newlines, internally. The easiest way to implement the first option would be with an Aspect that cross-cuts the code whenever a logging call is made. From there, code along the lines of `logMessage.replaceAll("\\n", "");` would strip the newlines. The question is just, where do you want to put that code: in an aspect, in a subclass of Log4JLogger, or everywhere in your code that you make a logging call?
gmale
A: 

gmale is right by saying that the code of omitting the newline character must exists somewhere.

However, you have to be careful; you only want to remove newlines from certain logged objects, not from all of them (collectively filtering out newlines from all log calls in the system is a bit of an overkill and I'd consider it a handicap).

To achieve this, you have two options:

  1. Manually invoke a method to clear newlines from the XML string before passing it to log4j, using gmale's example (however I'd replace \n with System.getProperty("line.separator") or, if you're using Commons Lang, SystemUtils.LINE_SEPARATOR).
  2. Wrap your XML string with a custom object, and configure Log4J to add an ObjectRenderer that will render your custom object into a String.

Any of these two ways will result in newlines being removed from exactly the type(s) of content you want, rather than collectively for all logging on the planet.

Isaac