You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Marco Brandizi <br...@libero.it> on 2001/05/11 09:16:23 UTC

Dispatching messages to different files on the basis of priorities

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 <br...@libero_NOSPAM_.it>

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: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Where can I find more info on Appenders.

Posted by Wilmer Hernandez <wi...@kinek.com>.
Hi there,

I would like to now if there is place where I can find more information on how
to extend appenders. I'm trying to create an appender that writes to a database
table. If there is an appender that already does that, I would be interested in
checking it out.

Thanks in advance.

Re: Dispatching messages to different files on the basis of priorities

Posted by Ceki Gülcü <cg...@qos.ch>.
At 09:16 11.05.2001 +0200, Marco Brandizi wrote:
>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. 

You can attach multiple appenders to a category. In addition, filters can be added to appenders. 

>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 ;-)

Assuming you errors to go error.log and debug output to go debug.log, you could add two file appenders to the root category and attach a filter allowing only errors in the first and only debugs in the second. See org.apache.log4j.varia.PriorityMatchFilter and org.apache.log4j.varia.PriorityRangeFilter.


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

I have a few comments on your code. They are prefixed by \\\.


>-----------------------------------------------------------
>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
> *
> */

\\\ We can only accept contributions that are under the Apache 
\\\ Software License. LGPL is a no-no.


>public class PriorityAppender implements Appender {

\\\ You should have implemented the AppanderAttachable interface. 
\\\ See for example the AsyncAppender.

>  private HashMap appenders = new HashMap();

\\\ HashMap was introduced in JDK 1.2. Log4j requires JDK 1.1 compatibility.

>  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;
>  }
>
>}

\\\ That's it.

Regards, Ceki

--
Ceki Gülcü


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