View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.boolex;
15  
16  import java.util.ArrayList;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.slf4j.Marker;
21  
22  import ch.qos.logback.classic.Level;
23  import ch.qos.logback.classic.spi.ILoggingEvent;
24  import ch.qos.logback.classic.spi.IThrowableProxy;
25  import ch.qos.logback.classic.spi.LoggerContextVO;
26  import ch.qos.logback.classic.spi.ThrowableProxy;
27  import ch.qos.logback.core.CoreConstants;
28  import ch.qos.logback.core.boolex.JaninoEventEvaluatorBase;
29  import ch.qos.logback.core.boolex.Matcher;
30  
31  public class JaninoEventEvaluator extends
32      JaninoEventEvaluatorBase<ILoggingEvent> {
33  
34    public final static String IMPORT_LEVEL = "import ch.qos.logback.classic.Level;\r\n";
35  
36    public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<String>();
37    public final static List<Class> DEFAULT_PARAM_TYPE_LIST = new ArrayList<Class>();
38  
39    static {
40      DEFAULT_PARAM_NAME_LIST.add("DEBUG");
41      DEFAULT_PARAM_NAME_LIST.add("INFO");
42      DEFAULT_PARAM_NAME_LIST.add("WARN");
43      DEFAULT_PARAM_NAME_LIST.add("ERROR");
44  
45      DEFAULT_PARAM_NAME_LIST.add("event");
46      DEFAULT_PARAM_NAME_LIST.add("message");
47  
48      DEFAULT_PARAM_NAME_LIST.add("formattedMessage");
49      DEFAULT_PARAM_NAME_LIST.add("logger");
50      DEFAULT_PARAM_NAME_LIST.add("loggerContext");
51      DEFAULT_PARAM_NAME_LIST.add("level");
52      DEFAULT_PARAM_NAME_LIST.add("timeStamp");
53      DEFAULT_PARAM_NAME_LIST.add("marker");
54      DEFAULT_PARAM_NAME_LIST.add("mdc");
55      DEFAULT_PARAM_NAME_LIST.add("throwableProxy");
56      DEFAULT_PARAM_NAME_LIST.add("throwable");
57  
58      DEFAULT_PARAM_TYPE_LIST.add(int.class);
59      DEFAULT_PARAM_TYPE_LIST.add(int.class);
60      DEFAULT_PARAM_TYPE_LIST.add(int.class);
61      DEFAULT_PARAM_TYPE_LIST.add(int.class);
62  
63      DEFAULT_PARAM_TYPE_LIST.add(ILoggingEvent.class);
64      DEFAULT_PARAM_TYPE_LIST.add(String.class);
65      DEFAULT_PARAM_TYPE_LIST.add(String.class);
66      DEFAULT_PARAM_TYPE_LIST.add(String.class);
67      DEFAULT_PARAM_TYPE_LIST.add(LoggerContextVO.class);
68      DEFAULT_PARAM_TYPE_LIST.add(int.class);
69      DEFAULT_PARAM_TYPE_LIST.add(long.class);
70      DEFAULT_PARAM_TYPE_LIST.add(Marker.class);
71      DEFAULT_PARAM_TYPE_LIST.add(Map.class);
72      DEFAULT_PARAM_TYPE_LIST.add(IThrowableProxy.class);
73      DEFAULT_PARAM_TYPE_LIST.add(Throwable.class);
74    }
75  
76    public JaninoEventEvaluator() {
77  
78    }
79  
80    protected String getDecoratedExpression() {
81      String expression = getExpression();
82      if(!expression.contains("return")) {
83        expression = "return "+expression +";";
84        addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes ["+expression+"]");
85        addInfo("See also "+CoreConstants.CODES_URL+"#block");
86  
87      }
88      return IMPORT_LEVEL + expression;
89    }
90  
91    protected String[] getParameterNames() {
92      List<String> fullNameList = new ArrayList<String>();
93      fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
94  
95      for (int i = 0; i < matcherList.size(); i++) {
96        Matcher m = (Matcher) matcherList.get(i);
97        fullNameList.add(m.getName());
98      }
99  
100     return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
101   }
102 
103   protected Class[] getParameterTypes() {
104     List<Class> fullTypeList = new ArrayList<Class>();
105     fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
106     for (int i = 0; i < matcherList.size(); i++) {
107       fullTypeList.add(Matcher.class);
108     }
109     return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
110   }
111 
112   protected Object[] getParameterValues(ILoggingEvent loggingEvent) {
113     final int matcherListSize = matcherList.size();
114 
115     int i = 0;
116     Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size()
117         + matcherListSize];
118 
119     values[i++] = Level.DEBUG_INTEGER;
120     values[i++] = Level.INFO_INTEGER;
121     values[i++] = Level.WARN_INTEGER;
122     values[i++] = Level.ERROR_INTEGER;
123 
124     values[i++] = loggingEvent;
125     values[i++] = loggingEvent.getMessage();
126     values[i++] = loggingEvent.getFormattedMessage();
127     values[i++] = loggingEvent.getLoggerName();
128     values[i++] = loggingEvent.getLoggerContextVO();
129     values[i++] = loggingEvent.getLevel().toInteger();
130     values[i++] = new Long(loggingEvent.getTimeStamp());
131     // In order to avoid NullPointerException, we could push a dummy marker if
132     // the event's marker is null. However, this would surprise user who
133     // expect to see a null marker instead of a dummy one.
134     values[i++] = loggingEvent.getMarker();
135     values[i++] = loggingEvent.getMDCPropertyMap();
136 
137     IThrowableProxy iThrowableProxy = loggingEvent.getThrowableProxy();
138 
139     if (iThrowableProxy != null) {
140       values[i++] = iThrowableProxy;
141       if (iThrowableProxy instanceof ThrowableProxy) {
142         values[i++] = ((ThrowableProxy) iThrowableProxy).getThrowable();
143       } else {
144         values[i++] = null;
145       }
146     } else {
147       values[i++] = null;
148       values[i++] = null;
149     }
150 
151     for (int j = 0; j < matcherListSize; j++) {
152       values[i++] = (Matcher) matcherList.get(j);
153     }
154 
155     return values;
156   }
157 
158 }