You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Robert Kleemann <ro...@kleemann.org> on 2013/02/15 00:17:20 UTC

Re: how to search and replace message text in outgoing log messages?

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 <ro...@kleemann.org> 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: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: how to search and replace message text in outgoing log messages?

Posted by Tech Mail <yn...@gmail.com>.
Robert,

It looks good, but do not know the answer to your question.

I have a similar need but instead of replacing with a string, I want to add the logging context or the message header so that from the lines we can identify the source process.

Yogi

On Feb 14, 2013, at 3:17 PM, Robert Kleemann <ro...@kleemann.org> wrote:

> 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 <ro...@kleemann.org> 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: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org