I ended up writing a class that extends PatternLayout and it seems to
work. Here it is in case anyone else needs it.
package org.scharp.util;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;
/**
* <p>
* This class is a drop in replacement for a log4j PatternLayout
class. It can be referenced via a log4j properties file
* just like an ordinary PatternLayout.
*
* <p>
* The only difference between this class and the parent class is that
newlines are replace by a replacement string.
* This allows downstream log viewers to receive a single log message
that contains multiple lines and correctly
* reconstruct it as a single, viewable message.
*
* @author kleemann
*/
public class NewLinePatternLayout extends PatternLayout {
public static final String NL_REPLACEMENT = "!!!NL!!!";
public NewLinePatternLayout() {
}
public NewLinePatternLayout(String pattern) {
super(pattern);
}
/**
* If we don't render the throwable then it will be rendered by
the Appender which will likely display it on
* separate lines.
*/
public boolean ignoresThrowable() {
return false;
}
public String format(LoggingEvent event) {
String original = super.format(event);
StringBuilder sb = new StringBuilder(original.replace("\n",
NL_REPLACEMENT));
String[] s = event.getThrowableStrRep();
if (s != null) {
for (int i = 0; i < s.length; i++) {
sb.append(s[i]);
sb.append(NL_REPLACEMENT);
}
}
return sb.toString();
}
}
On Sat, Jan 19, 2013 at 9:35 AM, Robert Kleemann <[email protected]> wrote:
> I have have a need to replace newlines with a different character
> sequence on messages going to my SyslogAppender. I have quite a few
> programs sending log info to syslog so I was hoping that I could do
> this completely within the xml configuration file but I can't seem to
> find a way to make this happen. Is this correct? Is there a way to
> perform a search and replace within the message field via the xml
> config file?
>
> The fallback is to write some code to do the configuration. I'm
> thinking that the best way to do this in 1.2.17 is to write a class
> that extends PatternLayout and then reference that from the xml file.
> Is this the cleanest way to implement this?
>
> thanks!
> Robert.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]