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