View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 JaninoEventEvaluatorBase<ILoggingEvent> {
32  
33      public final static String IMPORT_LEVEL = "import ch.qos.logback.classic.Level;\r\n";
34  
35      public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<String>();
36      public final static List<Class> DEFAULT_PARAM_TYPE_LIST = new ArrayList<Class>();
37  
38      static {
39          DEFAULT_PARAM_NAME_LIST.add("DEBUG");
40          DEFAULT_PARAM_NAME_LIST.add("INFO");
41          DEFAULT_PARAM_NAME_LIST.add("WARN");
42          DEFAULT_PARAM_NAME_LIST.add("ERROR");
43  
44          DEFAULT_PARAM_NAME_LIST.add("event");
45          DEFAULT_PARAM_NAME_LIST.add("message");
46  
47          DEFAULT_PARAM_NAME_LIST.add("formattedMessage");
48          DEFAULT_PARAM_NAME_LIST.add("logger");
49          DEFAULT_PARAM_NAME_LIST.add("loggerContext");
50          DEFAULT_PARAM_NAME_LIST.add("level");
51          DEFAULT_PARAM_NAME_LIST.add("timeStamp");
52          DEFAULT_PARAM_NAME_LIST.add("marker");
53          DEFAULT_PARAM_NAME_LIST.add("mdc");
54          DEFAULT_PARAM_NAME_LIST.add("throwableProxy");
55          DEFAULT_PARAM_NAME_LIST.add("throwable");
56  
57          DEFAULT_PARAM_TYPE_LIST.add(int.class);
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  
62          DEFAULT_PARAM_TYPE_LIST.add(ILoggingEvent.class);
63          DEFAULT_PARAM_TYPE_LIST.add(String.class);
64          DEFAULT_PARAM_TYPE_LIST.add(String.class);
65          DEFAULT_PARAM_TYPE_LIST.add(String.class);
66          DEFAULT_PARAM_TYPE_LIST.add(LoggerContextVO.class);
67          DEFAULT_PARAM_TYPE_LIST.add(int.class);
68          DEFAULT_PARAM_TYPE_LIST.add(long.class);
69          DEFAULT_PARAM_TYPE_LIST.add(Marker.class);
70          DEFAULT_PARAM_TYPE_LIST.add(Map.class);
71          DEFAULT_PARAM_TYPE_LIST.add(IThrowableProxy.class);
72          DEFAULT_PARAM_TYPE_LIST.add(Throwable.class);
73      }
74  
75      protected String getDecoratedExpression() {
76          String expression = getExpression();
77          if (!expression.contains("return")) {
78              expression = "return " + expression + ";";
79              addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes [" + expression + "]");
80              addInfo("See also " + CoreConstants.CODES_URL + "#block");
81  
82          }
83          return IMPORT_LEVEL + expression;
84      }
85  
86      protected String[] getParameterNames() {
87          List<String> fullNameList = new ArrayList<String>();
88          fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
89  
90          for (int i = 0; i < matcherList.size(); i++) {
91              Matcher m = (Matcher) matcherList.get(i);
92              fullNameList.add(m.getName());
93          }
94  
95          return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
96      }
97  
98      protected Class[] getParameterTypes() {
99          List<Class> fullTypeList = new ArrayList<Class>();
100         fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
101         for (int i = 0; i < matcherList.size(); i++) {
102             fullTypeList.add(Matcher.class);
103         }
104         return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
105     }
106 
107     protected Object[] getParameterValues(ILoggingEvent loggingEvent) {
108         final int matcherListSize = matcherList.size();
109 
110         int i = 0;
111         Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size() + matcherListSize];
112 
113         values[i++] = Level.DEBUG_INTEGER;
114         values[i++] = Level.INFO_INTEGER;
115         values[i++] = Level.WARN_INTEGER;
116         values[i++] = Level.ERROR_INTEGER;
117 
118         values[i++] = loggingEvent;
119         values[i++] = loggingEvent.getMessage();
120         values[i++] = loggingEvent.getFormattedMessage();
121         values[i++] = loggingEvent.getLoggerName();
122         values[i++] = loggingEvent.getLoggerContextVO();
123         values[i++] = loggingEvent.getLevel().toInteger();
124         values[i++] = loggingEvent.getTimeStamp();
125         // In order to avoid NullPointerException, we could push a dummy marker if
126         // the event's marker is null. However, this would surprise user who
127         // expect to see a null marker instead of a dummy one.
128         values[i++] = loggingEvent.getMarker();
129         values[i++] = loggingEvent.getMDCPropertyMap();
130 
131         IThrowableProxy iThrowableProxy = loggingEvent.getThrowableProxy();
132 
133         if (iThrowableProxy != null) {
134             values[i++] = iThrowableProxy;
135             if (iThrowableProxy instanceof ThrowableProxy) {
136                 values[i++] = ((ThrowableProxy) iThrowableProxy).getThrowable();
137             } else {
138                 values[i++] = null;
139             }
140         } else {
141             values[i++] = null;
142             values[i++] = null;
143         }
144 
145         for (int j = 0; j < matcherListSize; j++) {
146             values[i++] = (Matcher) matcherList.get(j);
147         }
148 
149         return values;
150     }
151 
152 }