Gilbert --

You can accomplish what you want using the components that come packaged
w/ log4j.  Here's a log4j configuration that should accomplish your
goals.  Explanations follow:

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

        <appender name="AllOutput"
class="org.apache.log4j.FileAppender">
                <param name="file" value="${user.dir}/AllOutput.log" />
                <layout class="org.apache.log4j.PatternLayout">
                        <param name="ConversionPattern"
        
value="[%-5p][%c][%d{HH:mm:ss.SSSSSS}]%m%n" />
                </layout>
        </appender>

        <appender name="SpecialOutput"
                class="org.apache.log4j.FileAppender">
                <param name="file" value="${user.dir}/SpecialOutput.log"
/>
                <layout class="org.apache.log4j.PatternLayout">
                        <param name="ConversionPattern"
        
value="[%-5p][%c][%d{HH:mm:ss.SSSSSS}]%m%n" />
                </layout>
                <filter
class="org.apache.log4j.varia.StringMatchFilter">
                        <param name="AcceptOnMatch" value="true" />
                        <param name="StringToMatch" value="ERR-10234" />
                </filter>
                <filter
class="org.apache.log4j.varia.StringMatchFilter">
                        <param name="AcceptOnMatch" value="true" />
                        <param name="StringToMatch" value="ERR-30857" />
                </filter>
                <filter class="org.apache.log4j.varia.DenyAllFilter" />
        </appender>

        <logger name="com.3rdparty.program" additivity="false">
                <level value="debug" />
                <appender-ref ref="AllOutput" />
                <appender-ref ref="SpecialOutput" />
        </logger>

        <root>
                <level value="debug" />
                <appender-ref ref="AllOutput" />
                <appender-ref ref="SpecialOutput" />
        </root>

</log4j:configuration>

1) The root logger is setup w/ 2 appenders: AllOutput and SpecialOutput.

2) The AllOutput appender is a simple file appender that accepts every
logging request and writes the rendered output for each message out to
the file ${user.dir}/AllOutput.log.

3) The SpecialOutput appender is also a simple file appender.  However,
this appender only writes logging message if the attached filters accept
it. There are three filters attached to the SpecialOutput appender:

The first filter will accept any logging requests where the rendered
message contains the string "ERR-10234".  If the rendered message
doesn't contain the string "ERR-10234", then the logging request will be
evaluated by the remaining filters.

The second is basically the same as the first except that it accepts
logging requests only if the rendered message contains "ERR-30857".

The third filter is the 'catch all' filter that will reject any logging
request that wasn't accepted by any of the previous filters. 

Assume that the SpecialOutput appender in place as outlined above and
that the following code is run: 

Logger logger = LogManager.getLogger(MyClass.class);
logger.debug("Greetings from Initech"); // msg11
logger.debug("ERR-10234: TPS report is invalid!"); // msg2
logger.debug("ERR-30857: TPS report has incorrect cover page!"); // msg3
logger.debug("ERR-43821: Bill Lumbergh needs a new tie"); // msg4

The ${user.dir}/AllOutput.log file would contain the following since
there are no filters on the AllOutput appender

Greetings from Initech
ERR-10234: TPS report is invalid!
ERR-30857: TPS report has incorrect cover page!
ERR-43821: Bill Lumbergh needs a new tie

The ${user.dir}/SpecialOutput.log file would only contain the following
two lines since these messages are the only ones that were accepted by
the filters that are attached to the SpecialOutput appender:

ERR-10234: TPS report is invalid!
ERR-30857: TPS report has incorrect cover page!

My example above used the FileAppender that come packaged w/ log4j, but
the same concepts could be applied to any log4j appender.


Ron Gallagher, AT&T Mobility

-----Original Message-----
From: Roger Varley [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, May 23, 2007 7:37 AM
To: [email protected]
Subject: Third Party Appenders

Hi

Is there an appender that will allow me to monitor for a specific
message or list of specific messages and allow me to trigger an action
against those messages. I may need a different action for each
message.

Background.

I'm supporting a third party java web application. I don't have access
to the source code nor am I allowed to de-compile and modify the
source code. However, the application is using log4j for it's logging.
At the moment I'm manually monitoring the logfiles for the appearance
of specific messages and manually taking action based on those
messages.

I've tried writing a unix like "tail" program to monitor the logfile
directly but this fails overnight when the logfile rolls over to a new
logfile. So I thought that adding my own appender might be a more
elegant solution. I'm quite prepared to have a go at doing this myself
but I thought that I would check first in case this has already been
done. I'd also be grateful for alternate suggestions if it is
generally felt that this is not a good idea.

Regards
Gilbert

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to