Hello,

I'm an happy user of Log4J. I didn't understand if in new 1.6b version
is avaible a "Priority Appender". I mean: an appender able to dispatch
received messages to several destinations, one per each priority level. 

I already have developed classes to do that (one generic and one child,
specific for files), which you find attached to this message, but still
I have to develop the XML configuration part, which is very important
for the context where I use them. If someone already has something
alike, would be welcome ;-)

Please, answer me privately too, not only in the mailing list, otherwise
I could forget it... ;-)

Greetings

=================================================================
Marco Brandizi <[EMAIL PROTECTED]>

WARNING: My email address is in "NO SPAM" form.
To decode it, remove _NOSPAM_ . DO NOT REPLY TO THIS MESSAGE, 
unless you want back  a "recipient not found" feedback :-))






-----------------------------------------------------------
PriorityAppender.java ------------------------------

import org.log4j.*;
import org.log4j.spi.*;
import java.util.HashMap;
import java.util.Iterator;


/**
 * (2000) Marco Brandizi
 *
 * This code is free and to be intended as covered by the principles
stated in
 * GNU LGPL
 *
 * An appender used for associate an appender to each priority type, in
the same
 * logging category, i.e.: You may send messages of cat C to
C-errors.log
 * C-warn.logs, etc
 *
 */
public class PriorityAppender implements Appender {

  private HashMap appenders = new HashMap();
  private String name = "PriorityAppender";

  /** Adds an appender, associated to a priority, if p already had one,
      throws it away */
  public void addAppender ( Priority p, Appender a )
  {

    Appender old = (Appender) appenders.get ( p );

    if ( old == a )
      // Hey, it's the same!
      return;
    else if (old != null )
      // Release the old one
      old.close();
    else
      // Append new
      appenders.put (p, a);
  }

  /** null if p isn't correct */
  public Appender getAppender ( Priority p )
  {
    return (Appender) appenders.get (p);
  }

  /** Appenders are removed at end */
  protected void removeAppenders ()
  {
    Iterator iA = appenders.values().iterator();
    Appender a;
    while ( iA.hasNext() )
        ((Appender)iA.next()).close();
    appenders = null;
  }


  /** Does nothing, I don't need them here... */
  public void addFilter(Filter newFilter) {
  }

  /** Does nothing, I don't need them here... */
  public void clearFilters() {
  }

  public void close() {
    removeAppenders();
  }

  /** Switch the priority and forward to the proper appender */
  public void doAppend(LoggingEvent event) {
    Appender a = getAppender ( event.priority );
    a.doAppend(event);
  }

  /** Default is "PriorityAppender" */
  public String getName() {
    return name;
  }

  /** Does nothing, I don't need them here... */
  public void setErrorHandler(ErrorHandler errorHandler) {
  }

  /** Does nothing, I don't need them here... */
  public void setLayout(Layout layout) {
  }

  public void setName(String name) {
    this.name = name;
  }

  /** FALSE! */
  public boolean requiresLayout() {
    return false;
  }

}






















-----------------------------------------------------------
PriorityFileAppender.java ------------------------------
import org.log4j.*;
import org.log4j.spi.*;
import java.util.HashMap;
import java.util.Iterator;
import java.io.IOException;


/**
 * (2000) Marco Brandizi
 *
 * This code is free and to be intended as covered by the principles
stated in
 * GNU LGPL
 *
 * It's like PriorityAppender, but specific for file appenders.
 * <P>Note: to assign different kind of appenders, start by this class,
copyng its
 * code and redefining another one
 * (ex: public class PriorityRollingFileAppender extends
PriorityAppender ...)
 */

public class PriorityFileAppender extends PriorityAppender {

  /**
    Adds a FileAppender for standard priorities ( err, warn, nfo, debug
),
    setting prefix + priority.getString().toLowerCase() + postfix as
file name
  */
  public PriorityFileAppender (Layout l, String prefix, String postfix,
boolean append )
  throws IOException
  {
    Priority[] prs = new Priority []
      { Priority.ERROR, Priority.WARN, Priority.INFO, Priority.DEBUG };

    for ( int i = 0; i < prs.length; i++ )
    {
      Appender a =
        new FileAppender ( l, prefix + prs[i].toString().toLowerCase() +
postfix, append);
      addAppender ( prs[i], a );
    }
  }

  /**
    Create the appender, without appenders, to be set manually
  */
  public PriorityFileAppender ()
  throws IOException
  {
  }

  /**
    Adds a FileAppender for all priorities returned by
    Priority.getAllPossiblePriorities(),
    setting prefix + priority.getString().toLowerCase + postfix as file
name
  */
  public void addAllAppenders (Layout l, String prefix, String postfix,
boolean append)
  throws IOException
  {
    Priority[] prs = Priority.getAllPossiblePriorities();
    for ( int i = 0; i < prs.length; i++ )
    {
      Appender a =
        new FileAppender ( l, prefix + prs[i].toString().toLowerCase() +
postfix, append );
      addAppender ( prs[i], a );
    }
  }
}














-----------------------------------------------------------
testPriorityFileAppender.java ------------------------------
import org.log4j.*;
import java.io.*;

/**
 * (2000) Marco Brandizi
 *
 * This code is free and to be intended as covered by the principles
stated in
 * GNU LGPL
 *
 * A test with PriorityFileAppender
 */

public class testPriorityFileAppender {

  public static void main(String[] args) {
    Category cat = Category.getRoot();

    try
    {

      cat.addAppender(
        new PriorityFileAppender (
          new PatternLayout( "%-5p [%t]: %m%n" ) , "/temp/testPri-",
".log", false )
      );

      cat.info ("Hello!");
      for ( int i = 0; i < 10; i++ )
          cat.warn("Doing the " + i + " iteration of my stupid
loop...");
      cat.info ( "Done, bye-bye" );
    }
    catch ( IOException ex )
    {
        ex.printStackTrace();
    }
  }

}

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

Reply via email to