logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sde...@apache.org
Subject cvs commit: jakarta-log4j/src/java/org/apache/log4j/chainsaw LoggingEventFieldResolver.java
Date Wed, 17 Sep 2003 06:24:38 GMT
sdeboy      2003/09/16 23:24:38

  Modified:    src/java/org/apache/log4j/chainsaw/rule RuleTest.java
               src/java/org/apache/log4j/chainsaw
                        LoggingEventFieldResolver.java
  Log:
  Updated loggingeventfieldresolver and ruletest to actually process logging events.
  
  NOTE: spaces in event fields aren't being handled correctly
  
  Revision  Changes    Path
  1.4       +81 -83    jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/RuleTest.java
  
  Index: RuleTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/RuleTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RuleTest.java	15 Sep 2003 02:20:26 -0000	1.3
  +++ RuleTest.java	17 Sep 2003 06:24:38 -0000	1.4
  @@ -52,9 +52,10 @@
   import java.awt.BorderLayout;
   import java.awt.GridLayout;
   import java.awt.event.ActionEvent;
  -
  +import java.util.ArrayList;
   import java.util.Enumeration;
   import java.util.HashMap;
  +import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
   import java.util.Stack;
  @@ -66,11 +67,14 @@
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
  -import javax.swing.JTable;
  +import javax.swing.JTextArea;
   import javax.swing.JTextField;
   import javax.swing.WindowConstants;
  -import javax.swing.table.DefaultTableModel;
  -import javax.swing.table.TableModel;
  +
  +import org.apache.log4j.Level;
  +import org.apache.log4j.Logger;
  +import org.apache.log4j.chainsaw.LoggingEventFieldResolver;
  +import org.apache.log4j.spi.LoggingEvent;
   
   
   public class RuleTest extends JFrame {
  @@ -86,14 +90,40 @@
      * @author Scott Deboy <sdeboy@apache.org>
      *
      */
  -  private final Vector data = new Vector();
  -  private final Vector colnames = new Vector();
  -
     public RuleTest(String booleanPostFixExpression, String inFixExpression) {
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       getContentPane().setLayout(new BorderLayout());
   
  -    JPanel fieldPanel = new JPanel(new GridLayout(8, 1));
  +	final LoggingEventFieldResolver resolver = LoggingEventFieldResolver.getInstance();
  +
  +    final List eventList = new ArrayList();
  +    
  +    eventList.add(new LoggingEvent("org.apache.log4j.chainsaw", Logger.getLogger("logger1"),
System.currentTimeMillis(), Level.DEBUG, "message1", new Exception("test")));
  +	eventList.add(new LoggingEvent("org.apache.log4j.chainsaw", Logger.getLogger("logger2"),
System.currentTimeMillis(), Level.DEBUG, "message2", new Exception("test2")));
  +	eventList.add(new LoggingEvent("org.apache.log4j.net", Logger.getLogger("logger3"), System.currentTimeMillis(),
Level.DEBUG, "message3", new Exception("test3")));
  +	eventList.add(new LoggingEvent("org.apache.log4j.chainsaw", Logger.getLogger("logger4"),
System.currentTimeMillis(), Level.WARN, "message4", new Exception("test4")));
  +
  +    JPanel fieldPanel = new JPanel(new GridLayout(12, 1));
  +
  +	fieldPanel.add(
  +	  new JLabel("Enter infix expression to convert to postfix: "));
  +
  +	final JTextField inFixTextField = new JTextField(inFixExpression);
  +	fieldPanel.add(inFixTextField);
  +
  +	JButton inFixButton = new JButton("Convert InFix to PostFix");
  +	fieldPanel.add(inFixButton);
  +
  +	final JTextField inFixResult = new JTextField();
  +	fieldPanel.add(inFixResult);
  +	inFixButton.addActionListener(
  +	  new AbstractAction() {
  +		public void actionPerformed(ActionEvent evt) {
  +		  InFixToPostFix inFixConverter = new InFixToPostFix();
  +		  inFixResult.setText(
  +			inFixConverter.convert(inFixTextField.getText()));
  +		}
  +	  });
   
       fieldPanel.add(
         new JLabel("Enter boolean postfix expression to evaluate: "));
  @@ -118,64 +148,56 @@
           }
         });
   
  -    fieldPanel.add(
  -      new JLabel("Enter infix expression to convert to postfix: "));
   
  -    final JTextField inFixTextField = new JTextField(inFixExpression);
  -    fieldPanel.add(inFixTextField);
  +	JPanel resultsPanel = new JPanel(new BorderLayout());
   
  -    JButton inFixButton = new JButton("Convert InFix to PostFix");
  -    fieldPanel.add(inFixButton);
  +	JButton resultsButton = new JButton("Replace fields (processes expression in 'Enter boolean
postfix expression to evaluate' box)");
  +	resultsPanel.add(resultsButton, BorderLayout.NORTH);
   
  -    final JTextField inFixResult = new JTextField();
  -    fieldPanel.add(inFixResult);
  -    inFixButton.addActionListener(
  -      new AbstractAction() {
  -        public void actionPerformed(ActionEvent evt) {
  -          InFixToPostFix inFixConverter = new InFixToPostFix();
  -          inFixResult.setText(
  -            inFixConverter.convert(inFixTextField.getText()));
  -        }
  -      });
  +	final JTextArea results = new JTextArea(5, 50);
  +	resultsPanel.add(results, BorderLayout.CENTER);
  +	JPanel copyNotePanel = new JPanel(new GridLayout(2, 1));
  +	copyNotePanel.add(new JLabel("Copy one of the lines in the replace fields box into the
"));
  +	copyNotePanel.add(new JLabel("'Enter boolean postfix expression to evaluate' box and re-evaluate"));
  +	resultsPanel.add(copyNotePanel, BorderLayout.SOUTH);
  +
  +	resultsButton.addActionListener(
  +	  new AbstractAction() {
  +		public void actionPerformed(ActionEvent evt) {
  +			results.setText("");
  +			String originalPostFix = booleanPostFixTextField.getText();
  +			System.out.println("postfix is " + originalPostFix);
  +			Iterator iter = eventList.iterator();
  +			while (iter.hasNext()) {
  +				LoggingEvent event = (LoggingEvent)iter.next();
  +				StringTokenizer tokenizer = new StringTokenizer(originalPostFix);
  +				StringBuffer newPostFix = new StringBuffer();
  +				String nextToken = null;
  +				while (tokenizer.hasMoreElements()) {
  +					nextToken = tokenizer.nextToken();
  +					if (resolver.isField(nextToken)) {
  +						newPostFix.append(resolver.getValue(nextToken, event) + " ");
  +					} else {
  +						newPostFix.append(nextToken + " ");
  +					} 										
  +				}
  +				results.setText(results.getText() + (results.getText().length()==0?"":"\n") + newPostFix.toString());
  +			}
  +		}
  +	  });
   
  -    colnames.add("level");
  -    colnames.add("logger");
  -    colnames.add("message");
  -    colnames.add("result");
  -
  -    data.add(
  -      createEvent("DEBUG", "org.apache.log4j.chainsaw", "TEST MESSAGE 1"));
  -    data.add(createEvent("DEBUG", "test logger", "TEST MESSAGE 2"));
  -    data.add(
  -      createEvent("INFO", "org.apache.log4j.chainsaw", "TEST MESSAGE 3"));
  -    data.add(
  -      createEvent("INFO", "org.aache.log4j.chainsaw", "TEST MESSAGE 4"));
  -    data.add(createEvent("WARN", "test logger", "TEST MESSAGE 5"));
  -    data.add(createEvent("WARN", "test logger 2", "TEST MESSAGE 6"));
  -    data.add(createEvent("WARN", "test logger 2", "TEST MESSAGE 7"));
  -
  -    TableModel tm = new EventTableModel(data, colnames);
  -    JPanel tablePanel = new JPanel();
  -    JTable table = new JTable(tm);
  -    tablePanel.add(table);
       getContentPane().add(fieldPanel, BorderLayout.NORTH);
  -    getContentPane().add(tablePanel, BorderLayout.CENTER);
  -  }
  -
  -  private Vector createEvent(String level, String logger, String message) {
  -    Vector v = new Vector();
  -    v.add(level);
  -    v.add(logger);
  -    v.add(message);
  -
  -    return v;
  +    getContentPane().add(resultsPanel, BorderLayout.CENTER);
     }
  +  
  +  
  +  
   
     public static void main(String[] args) {
       RuleTest test =
         new RuleTest(
  -        "level debug == BLAH test == logger org.apache == && ||",
  -        "( ( level == debug ) || ( BLAH == test ) ) && logger == org.apache");
  +        "level debug ~= BLAH test == logger logger1 == && ||",
  +        "( ( level ~= debug ) || ( BLAH == test ) ) && logger == logger1");
       test.pack();
       test.setVisible(true);
     }
  @@ -223,8 +245,8 @@
         System.out.println(
           "part text match op " + firstParam + ".." + secondParam);
   
  -      return ((secondParam != null) && (secondParam.indexOf(firstParam) > -1))
  -      ? true : false;
  +      return ((secondParam != null && firstParam != null) && 
  +      	(secondParam.toLowerCase().indexOf(firstParam.toLowerCase()) > -1));
       }
     }
   
  @@ -232,23 +254,10 @@
       boolean evaluate(String firstParam, String secondParam) {
         System.out.println("text match op " + firstParam + ".." + secondParam);
   
  -      boolean result = false;
  -
         //second parameter is field name
         //first parameter is value
         //fake out logic here to examine passed in parameters and value retrieval from table
  -      if (
  -        (secondParam.equalsIgnoreCase("level")
  -          && firstParam.equalsIgnoreCase("debug"))
  -          || (secondParam.equalsIgnoreCase("logger")
  -          && firstParam.equalsIgnoreCase("org.apache"))) {
  -        result = true;
  -      } else {
  -        result = (((secondParam != null) && secondParam.equals(firstParam))
  -          ? true : false);
  -      }
  -
  -      return result;
  +        return ((secondParam != null) && secondParam.equals(firstParam));
       }
     }
   
  @@ -421,17 +430,6 @@
         stack.clear();
   
         return postfix.toString();
  -    }
  -  }
  -
  -  class EventTableModel extends DefaultTableModel {
  -    Vector data;
  -    Vector colnames;
  -
  -    EventTableModel(Vector data, Vector colnames) {
  -      super(data, colnames);
  -      this.data = data;
  -      this.colnames = colnames;
       }
     }
   }
  
  
  
  1.4       +34 -18    jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggingEventFieldResolver.java
  
  Index: LoggingEventFieldResolver.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggingEventFieldResolver.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- LoggingEventFieldResolver.java	17 Sep 2003 04:03:22 -0000	1.3
  +++ LoggingEventFieldResolver.java	17 Sep 2003 06:24:38 -0000	1.4
  @@ -84,8 +84,9 @@
    * NOTE:  the values for the 'keyName' portion of the MDC and PROP mappings must
    * be an exact match to the key in the hashTable (case sensitive).
    *
  - * If the passed-in field is null or does not match an entry in the above-described
  - * mapping, an empty string is returned.
  + * If the passed-in field is null, an empty string is returned.
  + * If the passed-in field doesn't match an entry in the above-described
  + * mapping, the passed-in field is returned.
    *
    * @author Scott Deboy <sdeboy@apache.org>
    * @author Paul Smith <psmith@apache.org>
  @@ -102,41 +103,56 @@
       return resolver;
     }
   
  +  public boolean isField(String fieldName) {
  +  	if (fieldName == null) {
  +  		return false;
  +  	}
  +  	String upperField = fieldName.toUpperCase();
  +  	
  +  	return (upperField != null && ("LOGGER".equals(upperField) || "LEVEL".equals(upperField)
|| 
  +  	"CLASS".equals(upperField) || "FILE".equals(upperField) || "LINE".equals(upperField)
||
  +  	"METHOD".equals(upperField) || "MSG".equals(upperField) || "NDC".equals(upperField)
|| 
  +  	"EXCEPTION".equals(upperField) || "TIMESTAMP".equals(upperField) || "THREAD".equals(upperField)
||
  +	upperField.startsWith("MDC.") || upperField.startsWith("PROP.")));
  +  }
  +  
     public Object getValue(String fieldName, LoggingEvent event) {
       if (fieldName == null) {
         return "";
       }
   
  -    String lowerProp = fieldName.toUpperCase();
  +    String upperField = fieldName.toUpperCase();
   
  -    if ("LOGGER".equals(fieldName)) {
  +    if ("LOGGER".equals(upperField)) {
         return event.getLoggerName();
  -    } else if ("LEVEL".equals(fieldName)) {
  +    } else if ("LEVEL".equals(upperField)) {
         return event.getLevel();
  -    } else if ("CLASS".equals(fieldName)) {
  +    } else if ("CLASS".equals(upperField)) {
         return event.getLocationInformation().getClassName();
  -    } else if ("FILE".equals(fieldName)) {
  +    } else if ("FILE".equals(upperField)) {
         return event.getLocationInformation().getFileName();
  -    } else if ("LINE".equals(fieldName)) {
  +    } else if ("LINE".equals(upperField)) {
         return event.getLocationInformation().getLineNumber();
  -    } else if ("METHOD".equals(fieldName)) {
  +    } else if ("METHOD".equals(upperField)) {
         return event.getLocationInformation().getMethodName();
  -    } else if ("MSG".equals(fieldName)) {
  +    } else if ("MSG".equals(upperField)) {
         return event.getMessage();
  -    } else if ("NDC".equals(fieldName)) {
  +    } else if ("NDC".equals(upperField)) {
         return event.getNDC();
  -    } else if ("EXCEPTION".equals(fieldName)) {
  +    } else if ("EXCEPTION".equals(upperField)) {
   	  return event.getThrowableInformation();
  -    } else if ("TIMESTAMP".equals(fieldName)) {
  +    } else if ("TIMESTAMP".equals(upperField)) {
         return new Long(event.timeStamp);
  -    } else if ("THREAD".equals(fieldName)) {
  +    } else if ("THREAD".equals(upperField)) {
         return event.getThreadName();
  -    } else if (fieldName.startsWith("MDC.")) {
  +    } else if (upperField.startsWith("MDC.")) {
  +      //note: need to use actual fieldname since case matters
         return event.getMDC(fieldName.substring(4));
  -    } else if (fieldName.startsWith("PROP.")) {
  +    } else if (upperField.startsWith("PROP.")) {
  +		//note: need to use actual fieldname since case matters
         return event.getProperty(fieldName.substring(5));
       }
  -
  -    return "";
  +	//there wasn't a match, so just return the passed-in name
  +    return fieldName;
     }
   }
  
  
  

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


Mime
View raw message