View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2009, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.boolex;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.Map;
15  
16  import org.slf4j.Marker;
17  
18  import ch.qos.logback.classic.Level;
19  import ch.qos.logback.classic.spi.LoggerRemoteView;
20  import ch.qos.logback.classic.spi.LoggingEvent;
21  import ch.qos.logback.core.CoreConstants;
22  import ch.qos.logback.core.boolex.JaninoEventEvaluatorBase;
23  import ch.qos.logback.core.boolex.Matcher;
24  
25  
26  
27  public class JaninoEventEvaluator extends JaninoEventEvaluatorBase<LoggingEvent> {
28  
29    
30    public final static String IMPORT_LEVEL = "import ch.qos.logback.classic.Level;\r\n";
31    
32    public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<String>();
33    public final static List<Class> DEFAULT_PARAM_TYPE_LIST = new ArrayList<Class>();
34    
35    static {
36      DEFAULT_PARAM_NAME_LIST.add("DEBUG");
37      DEFAULT_PARAM_NAME_LIST.add("INFO");
38      DEFAULT_PARAM_NAME_LIST.add("WARN");
39      DEFAULT_PARAM_NAME_LIST.add("ERROR");
40      
41      DEFAULT_PARAM_NAME_LIST.add("event");
42      DEFAULT_PARAM_NAME_LIST.add("message");
43      DEFAULT_PARAM_NAME_LIST.add("logger");
44      DEFAULT_PARAM_NAME_LIST.add("level");
45      DEFAULT_PARAM_NAME_LIST.add("timeStamp");
46      DEFAULT_PARAM_NAME_LIST.add("marker");
47      DEFAULT_PARAM_NAME_LIST.add("mdc");
48      DEFAULT_PARAM_NAME_LIST.add("throwable");
49  
50      
51      DEFAULT_PARAM_TYPE_LIST.add(int.class);
52      DEFAULT_PARAM_TYPE_LIST.add(int.class);
53      DEFAULT_PARAM_TYPE_LIST.add(int.class);
54      DEFAULT_PARAM_TYPE_LIST.add(int.class);
55      
56      DEFAULT_PARAM_TYPE_LIST.add(LoggingEvent.class);
57      DEFAULT_PARAM_TYPE_LIST.add(String.class);
58      DEFAULT_PARAM_TYPE_LIST.add(LoggerRemoteView.class);
59      DEFAULT_PARAM_TYPE_LIST.add(int.class);
60      DEFAULT_PARAM_TYPE_LIST.add(long.class);
61      DEFAULT_PARAM_TYPE_LIST.add(Marker.class);
62      DEFAULT_PARAM_TYPE_LIST.add(Map.class);
63      DEFAULT_PARAM_TYPE_LIST.add(Throwable.class);
64    }
65    
66    
67    public JaninoEventEvaluator() {
68      
69    }
70    protected String getDecoratedExpression() {
71      return IMPORT_LEVEL + getExpression();
72    }
73  
74    protected String[] getParameterNames() {
75      List<String> fullNameList = new ArrayList<String>();
76      fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
77  
78      for(int i = 0; i < matcherList.size(); i++) {
79        Matcher m = (Matcher) matcherList.get(i);
80        fullNameList.add(m.getName());
81      }
82      
83      return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
84    }
85  
86    protected Class[] getParameterTypes() {
87      List<Class> fullTypeList = new ArrayList<Class>();
88      fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
89      for(int i = 0; i < matcherList.size(); i++) {
90        fullTypeList.add(Matcher.class);
91      }
92      return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
93    }
94  
95    protected Object[] getParameterValues(LoggingEvent loggingEvent) {
96      final int matcherListSize = matcherList.size();
97      
98      int i = 0;
99      Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size()+matcherListSize];
100 
101     values[i++] = Level.DEBUG_INTEGER;
102     values[i++] = Level.INFO_INTEGER;
103     values[i++] = Level.WARN_INTEGER;
104     values[i++] = Level.ERROR_INTEGER;
105     
106     values[i++] = loggingEvent;
107     values[i++] = loggingEvent.getMessage();    
108     values[i++] = loggingEvent.getLoggerRemoteView();
109     values[i++] = loggingEvent.getLevel().toInteger();
110     values[i++] = new Long(loggingEvent.getTimeStamp());
111     values[i++] = loggingEvent.getMarker();
112     values[i++] = loggingEvent.getMDCPropertyMap();
113     if (loggingEvent.getThrowableProxy() != null) {
114       values[i++] = loggingEvent.getThrowableProxy().getThrowable();
115     } else {
116       values[i++] = null;
117     }
118     
119     for(int j = 0; j < matcherListSize; j++) {
120       values[i++] = (Matcher) matcherList.get(j);
121     }
122     
123     return values;
124   }
125 
126 }