logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sde...@apache.org
Subject cvs commit: logging-log4j/src/java/org/apache/log4j/net SocketHubAppender.java
Date Thu, 15 Jan 2004 07:04:51 GMT
sdeboy      2004/01/14 23:04:51

  Modified:    src/java/org/apache/log4j/chainsaw
                        ChainsawCyclicBufferTableModel.java LogPanel.java
                        ChainsawAppenderHandler.java
               src/java/org/apache/log4j/rule PartialTextMatchRule.java
                        LevelInequalityRule.java EqualsRule.java
                        AndRule.java NotEqualsRule.java ExpressionRule.java
                        ExistsRule.java InequalityRule.java OrRule.java
                        NotRule.java LikeRule.java
               src/java/org/apache/log4j/spi LoggingEventFieldResolver.java
               src/java/org/apache/log4j/net SocketHubAppender.java
  Log:
  - added optional 'bufferSize' param to SocketHubAppender, implemented as a cyclic buffer.
 As new clients attach to the appender, they receive the events in the cyclic buffer.  Suggested
by Paul Smith.
  - improved loggingeventfieldresolver and rules to avoid null pointers
  - improved support for evaluation of expressions (focus-on works and fieldresolver builds
exception string).  
  - example: the EXISTS operator can be used in conjunction with the EXCEPTION keyword (for
example, EXCEPTION EXISTS) to display or colorize all exceptions
  - chainsawappenderhandler now waits to process events until the identifier expression has
been set
  - empty quoted strings in expressions now supported and evaluate correctly
  
  Revision  Changes    Path
  1.19      +3 -0      logging-log4j/src/java/org/apache/log4j/chainsaw/ChainsawCyclicBufferTableModel.java
  
  Index: ChainsawCyclicBufferTableModel.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/chainsaw/ChainsawCyclicBufferTableModel.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- ChainsawCyclicBufferTableModel.java	7 Jan 2004 01:08:05 -0000	1.18
  +++ ChainsawCyclicBufferTableModel.java	15 Jan 2004 07:04:50 -0000	1.19
  @@ -418,6 +418,9 @@
     }
     
     private String getMDC(LoggingEvent event) {
  +      if (event.getMDCKeySet().size() == 0) {
  +          return "";
  +      }
         Iterator iter = event.getMDCKeySet().iterator();
         StringBuffer mdc = new StringBuffer("{");
         
  
  
  
  1.48      +20 -4     logging-log4j/src/java/org/apache/log4j/chainsaw/LogPanel.java
  
  Index: LogPanel.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/chainsaw/LogPanel.java,v
  retrieving revision 1.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- LogPanel.java	8 Jan 2004 07:08:59 -0000	1.47
  +++ LogPanel.java	15 Jan 2004 07:04:50 -0000	1.48
  @@ -864,6 +864,7 @@
           menuItemFocusOn.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                       if (currentPoint != null) {
  +                        String operator = "==";
                           int column = table.columnAtPoint(currentPoint);
                           int row = table.rowAtPoint(currentPoint);
                           String colName = table.getColumnName(column);
  @@ -878,13 +879,21 @@
                                   value = ((JLabel) comp).getText();
                               }
                           } else {
  -                            value = table.getValueAt(row, column).toString();
  +                            Object o = table.getValueAt(row, column);
  +                            if (o != null) {
  +                                if (o instanceof String[]) {
  +                                    value = ((String[])o)[0];
  +                                    operator = "~=";                                  
 
  +                                } else {
  +                                    value = o.toString();                             
      
  +                                }
  +                            }
                           }
   
                           if (columnNameKeywordMap.containsKey(colName)) {
                               filterText.setText(columnNameKeywordMap.get(colName)
                                                                      .toString() +
  -                                " == '" + value + "'");
  +                                " " + operator + " '" + value + "'");
                           }
                       }
                   }
  @@ -895,6 +904,7 @@
           menuDefineAddCustomFilter.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent evt) {
                       if (currentPoint != null) {
  +                        String operator = "==";
                           int column = table.columnAtPoint(currentPoint);
                           int row = table.rowAtPoint(currentPoint);
                           String colName = table.getColumnName(column);
  @@ -909,13 +919,19 @@
                                   value = ((JLabel) comp).getText();
                               }
                           } else {
  -                            value = table.getValueAt(row, column).toString();
  +                            Object o = table.getValueAt(row, column).toString();
  +                            if (o instanceof String[]) {
  +                                value = ((String[])o)[0];
  +                                operator = "~=";                                    
  +                            } else {
  +                                value = o.toString();                                 
  
  +                            }
                           }
   
                           if (columnNameKeywordMap.containsKey(colName)) {
                               filterText.setText(filterText.getText() + " && " +
                                   columnNameKeywordMap.get(colName).toString() +
  -                                " == '" + value + "'");
  +                                " " + operator + " '" + value + "'");
                           }
                       }
                   }
  
  
  
  1.15      +5 -2      logging-log4j/src/java/org/apache/log4j/chainsaw/ChainsawAppenderHandler.java
  
  Index: ChainsawAppenderHandler.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/chainsaw/ChainsawAppenderHandler.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- ChainsawAppenderHandler.java	5 Jan 2004 04:37:33 -0000	1.14
  +++ ChainsawAppenderHandler.java	15 Jan 2004 07:04:50 -0000	1.15
  @@ -100,7 +100,10 @@
     }
   
     public void setIdentifierExpression(String identifierExpression) {
  -    this.identifierExpression = identifierExpression;
  +    synchronized(mutex) {
  +        this.identifierExpression = identifierExpression;
  +        mutex.notify();
  +    }
     }
   
     public String getIdentifierExpression() {
  @@ -290,7 +293,7 @@
   
             synchronized (mutex) {
               try {
  -              while (queue.size() == 0) {
  +              while (queue.size() == 0 || identifierExpression == null) {
                   setDataRate(0);
                   mutex.wait();
                 }
  
  
  
  1.3       +4 -4      logging-log4j/src/java/org/apache/log4j/rule/PartialTextMatchRule.java
  
  Index: PartialTextMatchRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/PartialTextMatchRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PartialTextMatchRule.java	31 Dec 2003 09:54:40 -0000	1.2
  +++ PartialTextMatchRule.java	15 Jan 2004 07:04:50 -0000	1.3
  @@ -75,7 +75,7 @@
   
     public static Rule getRule(Stack stack) {
         if (stack.size() < 2) {
  -          throw new IllegalArgumentException("invalid partial text rule - expected two
entries but " + stack.size() + " were provided");
  +          throw new IllegalArgumentException("invalid partial text rule - expected two
parameters but received " + stack.size());
         }
       String p2 = stack.pop().toString();
       String p1 = stack.pop().toString();
  @@ -84,9 +84,9 @@
     }
   
     public boolean evaluate(LoggingEvent event) {
  -    String p2 = resolver.getValue(field, event).toString();
  +    Object p2 = resolver.getValue(field, event);
   
  -    return (((p2 != null) && (value != null))
  -      && (p2.toLowerCase().indexOf(value.toLowerCase()) > -1));
  +    return ((p2 != null) && (value != null)
  +      && (p2.toString().toLowerCase().indexOf(value.toLowerCase()) > -1));
     }
   }
  
  
  
  1.3       +3 -12     logging-log4j/src/java/org/apache/log4j/rule/LevelInequalityRule.java
  
  Index: LevelInequalityRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/LevelInequalityRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LevelInequalityRule.java	31 Dec 2003 09:54:40 -0000	1.2
  +++ LevelInequalityRule.java	15 Jan 2004 07:04:50 -0000	1.3
  @@ -57,8 +57,6 @@
   import java.util.Iterator;
   import java.util.LinkedList;
   import java.util.List;
  -import java.util.Stack;
  -
   
   /**
    * A Rule class implementing inequality evaluation for Levels (log4j and util.logging)
using the toInt method.
  @@ -73,7 +71,7 @@
     private final String inequalitySymbol;
   
     private LevelInequalityRule(
  -    String inequalitySymbol, String field, String value) {
  +    String inequalitySymbol, String value) {
       levelList.add(Level.FATAL.toString());
       levelList.add(Level.ERROR.toString());
       levelList.add(Level.WARN.toString());
  @@ -95,17 +93,10 @@
       this.inequalitySymbol = inequalitySymbol;
     }
   
  -  public static Rule getRule(String inequalitySymbol, String field, String value) {
  -      return new LevelInequalityRule(inequalitySymbol, field, value);
  +  public static Rule getRule(String inequalitySymbol, String value) {
  +      return new LevelInequalityRule(inequalitySymbol, value);
     }
     
  -  public static Rule getRule(String inequalitySymbol, Stack stack) {
  -    String p2 = stack.pop().toString();
  -    String p1 = stack.pop().toString();
  -
  -    return new LevelInequalityRule(inequalitySymbol, p1, p2);
  -  }
  -
     public boolean evaluate(LoggingEvent event) {
       //use the type of the first level to access the static toLevel method on the second
param
       Level level2 = null;
  
  
  
  1.3       +3 -3      logging-log4j/src/java/org/apache/log4j/rule/EqualsRule.java
  
  Index: EqualsRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/EqualsRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- EqualsRule.java	31 Dec 2003 09:54:40 -0000	1.2
  +++ EqualsRule.java	15 Jan 2004 07:04:50 -0000	1.3
  @@ -72,7 +72,7 @@
   
     public static Rule getRule(Stack stack) {
       if (stack.size() < 2) {
  -        throw new IllegalArgumentException("Invalid EQUALS rule - expected two rules but
provided " + stack.size());
  +        throw new IllegalArgumentException("Invalid EQUALS rule - expected two parameters
but received " + stack.size());
       }  
       String p2 = stack.pop().toString();
       String p1 = stack.pop().toString();
  @@ -85,7 +85,7 @@
     }
   
     public boolean evaluate(LoggingEvent event) {
  -    String p2 = resolver.getValue(field, event).toString();
  -    return ((p2 != null) && p2.equals(value));
  +    Object p2 = resolver.getValue(field, event);
  +    return ((p2 != null) && p2.toString().equals(value));
     }
   }
  
  
  
  1.2       +1 -1      logging-log4j/src/java/org/apache/log4j/rule/AndRule.java
  
  Index: AndRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/AndRule.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AndRule.java	14 Dec 2003 20:35:08 -0000	1.1
  +++ AndRule.java	15 Jan 2004 07:04:50 -0000	1.2
  @@ -70,7 +70,7 @@
   
     public static Rule getRule(Stack stack) {
       if (stack.size() < 2) {
  -        throw new IllegalArgumentException("Invalid AND rule - expected two rules but provided
" + stack.size());
  +        throw new IllegalArgumentException("Invalid AND rule - expected two rules but received
" + stack.size());
       }  
       Object o2 = stack.pop();
       Object o1 = stack.pop();
  
  
  
  1.3       +3 -3      logging-log4j/src/java/org/apache/log4j/rule/NotEqualsRule.java
  
  Index: NotEqualsRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/NotEqualsRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NotEqualsRule.java	31 Dec 2003 09:54:40 -0000	1.2
  +++ NotEqualsRule.java	15 Jan 2004 07:04:51 -0000	1.3
  @@ -75,7 +75,7 @@
     
     public static Rule getRule(Stack stack) {
       if (stack.size() < 2) {
  -        throw new IllegalArgumentException("Invalid NOT EQUALS rule - expected two rules
but provided " + stack.size());
  +        throw new IllegalArgumentException("Invalid NOT EQUALS rule - expected two parameters
but received " + stack.size());
       }  
       String p2 = stack.pop().toString();
       String p1 = stack.pop().toString();
  @@ -83,8 +83,8 @@
     }
   
     public boolean evaluate(LoggingEvent event) {
  -    String p2 = resolver.getValue(field, event).toString();
  +    Object p2 = resolver.getValue(field, event);
   
  -    return ((p2 != null) && !(p2.equals(value)));
  +    return ((p2 != null) && !(p2.toString().equals(value)));
     }
   }
  
  
  
  1.3       +3 -0      logging-log4j/src/java/org/apache/log4j/rule/ExpressionRule.java
  
  Index: ExpressionRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/ExpressionRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ExpressionRule.java	8 Jan 2004 01:06:52 -0000	1.2
  +++ ExpressionRule.java	15 Jan 2004 07:04:51 -0000	1.3
  @@ -121,6 +121,9 @@
           if ((token.startsWith("'")) && (token.endsWith("'") && (token.length()
> 2))) {
               token = token.substring(1, token.length() - 1);
           }
  +        if ((token.startsWith("'")) && (token.endsWith("'") && (token.length()
== 2))) {
  +            token = "";
  +        }
   
           boolean inText = token.startsWith("'");
           if (inText) {
  
  
  
  1.3       +3 -3      logging-log4j/src/java/org/apache/log4j/rule/ExistsRule.java
  
  Index: ExistsRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/ExistsRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ExistsRule.java	31 Dec 2003 09:54:40 -0000	1.2
  +++ ExistsRule.java	15 Jan 2004 07:04:51 -0000	1.3
  @@ -73,15 +73,15 @@
     
     public static Rule getRule(Stack stack) {
         if (stack.size() < 1) {
  -          throw new IllegalArgumentException("Invalid EXISTS rule - expected one rule but
provided " + stack.size());
  +          throw new IllegalArgumentException("Invalid EXISTS rule - expected one parameter
but received " + stack.size());
         }  
   
       return new ExistsRule(stack.pop().toString());
     }
   
     public boolean evaluate(LoggingEvent event) {
  -    String p2 = resolver.getValue(field, event).toString();
  +    Object p2 = resolver.getValue(field, event);
   
  -    return (!(p2.equals("")));
  +    return (!(p2 == null || (p2 != null && p2.toString().equals(""))));
     }
   }
  
  
  
  1.3       +2 -2      logging-log4j/src/java/org/apache/log4j/rule/InequalityRule.java
  
  Index: InequalityRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/InequalityRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- InequalityRule.java	31 Dec 2003 09:54:40 -0000	1.2
  +++ InequalityRule.java	15 Jan 2004 07:04:51 -0000	1.3
  @@ -78,7 +78,7 @@
     
     public static Rule getRule(String inequalitySymbol, Stack stack) {
         if (stack.size() < 2) {
  -          throw new IllegalArgumentException("Invalid " + inequalitySymbol + " rule - expected
two rules but provided " + stack.size());
  +          throw new IllegalArgumentException("Invalid " + inequalitySymbol + " rule - expected
two parameters but received " + stack.size());
         }  
   
         String p2 = stack.pop().toString();
  @@ -89,7 +89,7 @@
     public static Rule getRule(String inequalitySymbol, String field, String value) {
       if (field.equalsIgnoreCase(LEVEL)) {
         //push the value back on the stack and allow the level-specific rule pop values
  -      return LevelInequalityRule.getRule(inequalitySymbol, field, value);
  +      return LevelInequalityRule.getRule(inequalitySymbol, value);
       } else {
         return new InequalityRule(inequalitySymbol, field, value);
       }
  
  
  
  1.2       +1 -1      logging-log4j/src/java/org/apache/log4j/rule/OrRule.java
  
  Index: OrRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/OrRule.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- OrRule.java	14 Dec 2003 20:35:08 -0000	1.1
  +++ OrRule.java	15 Jan 2004 07:04:51 -0000	1.2
  @@ -73,7 +73,7 @@
     
     public static Rule getRule(Stack stack) {
         if (stack.size() < 2) {
  -          throw new IllegalArgumentException("Invalid OR rule - expected two rules but
provided " + stack.size());
  +          throw new IllegalArgumentException("Invalid OR rule - expected two rules but
received " + stack.size());
         }  
         Object o2 = stack.pop();
         Object o1 = stack.pop();
  
  
  
  1.2       +1 -1      logging-log4j/src/java/org/apache/log4j/rule/NotRule.java
  
  Index: NotRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/NotRule.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- NotRule.java	14 Dec 2003 20:35:08 -0000	1.1
  +++ NotRule.java	15 Jan 2004 07:04:51 -0000	1.2
  @@ -71,7 +71,7 @@
     
     public static Rule getRule(Stack stack) {
         if (stack.size() < 1) {
  -          throw new IllegalArgumentException("Invalid NOT rule - expected one rule but
provided " + stack.size());
  +          throw new IllegalArgumentException("Invalid NOT rule - expected one rule but
received " + stack.size());
         }  
         Object o1 = stack.pop();
         if (o1 instanceof Rule) {
  
  
  
  1.3       +3 -3      logging-log4j/src/java/org/apache/log4j/rule/LikeRule.java
  
  Index: LikeRule.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/rule/LikeRule.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- LikeRule.java	31 Dec 2003 09:54:40 -0000	1.2
  +++ LikeRule.java	15 Jan 2004 07:04:51 -0000	1.3
  @@ -77,7 +77,7 @@
   
     public static Rule getRule(Stack stack) {
         if (stack.size() < 2) {
  -          throw new IllegalArgumentException("Invalid LIKE rule - expected two rules but
provided " + stack.size());
  +          throw new IllegalArgumentException("Invalid LIKE rule - expected two parameters
but received " + stack.size());
         }  
         
         String p2 = stack.pop().toString();
  @@ -98,7 +98,7 @@
     }
   
     public boolean evaluate(LoggingEvent event) {
  -    String input = resolver.getValue(field, event).toString();
  -    return ((pattern != null) && matcher.matches(input, pattern));
  +    Object input = resolver.getValue(field, event);
  +    return ((input != null) && (pattern != null) && (matcher.matches(input.toString(),
pattern)));
     }
   }
  
  
  
  1.2       +14 -7     logging-log4j/src/java/org/apache/log4j/spi/LoggingEventFieldResolver.java
  
  Index: LoggingEventFieldResolver.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/spi/LoggingEventFieldResolver.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LoggingEventFieldResolver.java	31 Dec 2003 09:54:40 -0000	1.1
  +++ LoggingEventFieldResolver.java	15 Jan 2004 07:04:51 -0000	1.2
  @@ -166,20 +166,20 @@
       } else if (LEVEL_FIELD.equals(upperField)) {
         return event.getLevel();
       } else if (CLASS_FIELD.equals(upperField)) {
  -      return ((info == null) ? "" : info.getClassName());
  +      return ((info == null) ? EMPTY_STRING : info.getClassName());
       } else if (FILE_FIELD.equals(upperField)) {
  -      return ((info == null) ? "" : info.getFileName());
  +      return ((info == null) ? EMPTY_STRING : info.getFileName());
       } else if (LINE_FIELD.equals(upperField)) {
  -      return ((info == null) ? "" : info.getLineNumber());
  +      return ((info == null) ? EMPTY_STRING : info.getLineNumber());
       } else if (METHOD_FIELD.equals(upperField)) {
  -      return ((info == null) ? "" : info.getMethodName());
  +      return ((info == null) ? EMPTY_STRING : info.getMethodName());
       } else if (MSG_FIELD.equals(upperField)) {
         return event.getMessage();
       } else if (NDC_FIELD.equals(upperField)) {
         String ndcValue = event.getNDC();
  -      return ((ndcValue == null) ? "" : ndcValue);
  +      return ((ndcValue == null) ? EMPTY_STRING : ndcValue);
       } else if (EXCEPTION_FIELD.equals(upperField)) {
  -      return event.getThrowableInformation();
  +      return (event.getThrowableStrRep() == null ? EMPTY_STRING : getExceptionMessage(event.getThrowableStrRep()));
       } else if (TIMESTAMP_FIELD.equals(upperField)) {
         return new Long(event.timeStamp);
       } else if (THREAD_FIELD.equals(upperField)) {
  @@ -187,7 +187,6 @@
       } else if (upperField.startsWith(MDC_FIELD)) {
         //note: need to use actual fieldname since case matters
         Object mdcValue = event.getMDC(fieldName.substring(4));
  -
         return ((mdcValue == null) ? EMPTY_STRING : mdcValue.toString());
       } else if (upperField.startsWith(PROP_FIELD)) {
         //note: need to use actual fieldname since case matters
  @@ -198,4 +197,12 @@
       //there wasn't a match, so throw a runtime exception
       throw new RuntimeException("Unsupported field name: " + fieldName);
     }
  +
  +    private String getExceptionMessage(String[] exception) {
  +        StringBuffer buff = new StringBuffer();
  +        for (int i=0;i<exception.length;i++) {
  +            buff.append(exception[i]);
  +        }
  +    	return buff.toString();
  +    }
   }
  
  
  
  1.8       +50 -11    logging-log4j/src/java/org/apache/log4j/net/SocketHubAppender.java
  
  Index: SocketHubAppender.java
  ===================================================================
  RCS file: /home/cvs/logging-log4j/src/java/org/apache/log4j/net/SocketHubAppender.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SocketHubAppender.java	10 Dec 2003 21:35:06 -0000	1.7
  +++ SocketHubAppender.java	15 Jan 2004 07:04:51 -0000	1.8
  @@ -49,21 +49,20 @@
   
   package org.apache.log4j.net;
   
  -import org.apache.log4j.AppenderSkeleton;
  -import org.apache.log4j.helpers.LogLog;
  -import org.apache.log4j.spi.LoggingEvent;
  -
   import java.io.IOException;
   import java.io.InterruptedIOException;
   import java.io.ObjectOutputStream;
  -
   import java.net.InetAddress;
   import java.net.ServerSocket;
   import java.net.Socket;
   import java.net.SocketException;
  -
   import java.util.Vector;
   
  +import org.apache.log4j.AppenderSkeleton;
  +import org.apache.log4j.helpers.CyclicBuffer;
  +import org.apache.log4j.helpers.LogLog;
  +import org.apache.log4j.spi.LoggingEvent;
  +
   
   /**
     Sends {@link LoggingEvent} objects to a set of remote log servers,
  @@ -124,6 +123,10 @@
     connections. The rate of logging will be determined by the slowest
     link.
   
  +  <p>The BufferSize param provides a cyclic buffer of recently appended events.
  +  As new clients attach to the SocketHubAppender, the clients also receive the buffered
  +  events.
  +  
     <p><li>If the JVM hosting the <code>SocketHubAppender</code>
exits
     before the <code>SocketHubAppender</code> is closed either
     explicitly or subsequent to garbage collection, then there might
  @@ -146,6 +149,7 @@
     private Vector oosList = new Vector();
     private ServerMonitor serverMonitor = null;
     private boolean locationInfo = false;
  +  private CyclicBuffer buffer = null;
   
     public SocketHubAppender() {
     }
  @@ -208,16 +212,21 @@
     /**
       Append an event to all of current connections. */
     public void append(LoggingEvent event) {
  +    if (event != null) {
  +        // set up location info if requested
  +        if (locationInfo) {
  +          event.getLocationInformation();
  +        }
  +        if (buffer != null) {
  +            buffer.add(event);
  +        }
  +    }
  +    
       // if no event or no open connections, exit now
       if ((event == null) || (oosList.size() == 0)) {
         return;
       }
   
  -    // set up location info if requested
  -    if (locationInfo) {
  -      event.getLocationInformation();
  -    }
  -
       // loop through the current set of open connections, appending the event to each
       for (int streamCount = 0; streamCount < oosList.size(); streamCount++) {
         ObjectOutputStream oos = null;
  @@ -275,6 +284,23 @@
     }
   
     /**
  +     The <b>BufferSize</b> option takes a positive integer representing
  +     the number of events this appender will buffer and send to newly connected clients.*/
  +  public void setBufferSize(int _bufferSize) {
  +    buffer = new CyclicBuffer(_bufferSize);
  +  }
  +
  +  /**
  +     Returns value of the <b>bufferSize</b> option. */
  +  public int getBufferSize() {
  +    if (buffer == null) {
  +        return 0;
  +    } else {
  +        return buffer.getMaxSize();
  +    }
  +  }
  +
  +  /**
        The <b>LocationInfo</b> option takes a boolean value. If true,
        the information sent to the remote host will include location
        information. By default no location information is sent to the server. */
  @@ -334,6 +360,16 @@
           LogLog.debug("server monitor thread shut down");
         }
       }
  +    
  +    private void sendCachedEvents(ObjectOutputStream stream) throws IOException {
  +        if (buffer != null) {
  +            for (int i=0;i<buffer.length();i++) {
  +                stream.writeObject(buffer.get(i));
  +            }
  +            stream.flush();
  +            stream.reset();
  +        }
  +    }
   
       /**
         Method that runs, monitoring the ServerSocket and adding connections as
  @@ -388,6 +424,9 @@
                 // create an ObjectOutputStream
                 ObjectOutputStream oos =
                   new ObjectOutputStream(socket.getOutputStream());
  +              if (buffer != null && buffer.length() > 0) {
  +                sendCachedEvents(oos);
  +              }
   
                 // add it to the oosList.  OK since Vector is synchronized.
                 oosList.addElement(oos);
  
  
  

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


Mime
View raw message