Novell Home

Appender for Log4j

Novell Cool Solutions: Tip
By Alfredo Luiz Santos

Digg This - Slashdot This

Posted: 15 Feb 2006
 

This article shows you how to develop a new appender to log4j. This is useful in development using Novell APIs.

Steps

1) Import required classes:

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

2) Declare your class:

public class MyAppender extends AppenderSkeleton{

3) If you need extra parameters, declare them:

    private String Text3;
    private int Value1;
    private int Value2;

4) Declare methods to manage your parameters:

    public void setText3(String text3) {
        Text3 = text3;
    }

    public String getText3() {
        return Text3;
    }

    public void setValue1(int value1) {
        Value1 = value1;
    }

    public int getValue1() {
        return Value1;
    }

5) You must declare this method:

    public boolean requiresLayout() {
        return false;
    }

    public void close() {
    }
6) And the main method of your implementation:

    protected void append(LoggingEvent event) {
        String level=event.getLevel().toString();
        String event.getMessage().toString();
    }

The main method enables you to get the log (event.getMessage().toString()) and other data. With this data you can send the log to another application.

7) To use the log in your application, you can use this log4j.properties sample:

log4j.appender.mylog=MyAppender
log4j.appender.mylog.Text2=text2
log4j.appender.mylog.Text3=text3
log4j.appender.mylog.Value1=1
log4j.appender.mylog.Value2=2
log4j.appender.mylog.Value3=3

log4j.rootCategory=info, mylog


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell