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  import org.slf4j.Marker;
29  
30  public class JaninoEventEvaluator extends JaninoEventEvaluatorBase<ILoggingEvent> {
31  
32      public final static String IMPORT_LEVEL = "import ch.qos.logback.classic.Level;\r\n";
33  
34      public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<>();
35      public final static List<Class<?>> DEFAULT_PARAM_TYPE_LIST = new ArrayList<>();
36  
37      static {
38          DEFAULT_PARAM_NAME_LIST.add("DEBUG");
39          DEFAULT_PARAM_NAME_LIST.add("INFO");
40          DEFAULT_PARAM_NAME_LIST.add("WARN");
41          DEFAULT_PARAM_NAME_LIST.add("ERROR");
42  
43          DEFAULT_PARAM_NAME_LIST.add("event");
44          DEFAULT_PARAM_NAME_LIST.add("message");
45  
46          DEFAULT_PARAM_NAME_LIST.add("formattedMessage");
47          DEFAULT_PARAM_NAME_LIST.add("logger");
48          DEFAULT_PARAM_NAME_LIST.add("loggerContext");
49          DEFAULT_PARAM_NAME_LIST.add("level");
50          DEFAULT_PARAM_NAME_LIST.add("timeStamp");
51          DEFAULT_PARAM_NAME_LIST.add("marker");
52          DEFAULT_PARAM_NAME_LIST.add("markerList");
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(MarkerList.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      protected String getDecoratedExpression() {
77          String expression = getExpression();
78          if (!expression.contains("return")) {
79              expression = "return " + expression + ";";
80              addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes [" + expression + "]");
81              addInfo("See also " + CoreConstants.CODES_URL + "#block");
82  
83          }
84          return IMPORT_LEVEL + expression;
85      }
86  
87      protected String[] getParameterNames() {
88          List<String> fullNameList = new ArrayList<String>();
89          fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
90  
91          for (int i = 0; i < matcherList.size(); i++) {
92              Matcher m = (Matcher) matcherList.get(i);
93              fullNameList.add(m.getName());
94          }
95  
96          return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
97      }
98  
99      protected Class<?>[] getParameterTypes() {
100         List<Class<?>> fullTypeList = new ArrayList<>();
101         fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
102         for (int i = 0; i < matcherList.size(); i++) {
103             fullTypeList.add(Matcher.class);
104         }
105         return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
106     }
107 
108     protected Object[] getParameterValues(ILoggingEvent loggingEvent) {
109         final int matcherListSize = matcherList.size();
110 
111         int i = 0;
112         Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size() + matcherListSize];
113 
114         values[i++] = Level.DEBUG_INTEGER;
115         values[i++] = Level.INFO_INTEGER;
116         values[i++] = Level.WARN_INTEGER;
117         values[i++] = Level.ERROR_INTEGER;
118 
119         values[i++] = loggingEvent;
120         values[i++] = loggingEvent.getMessage();
121         values[i++] = loggingEvent.getFormattedMessage();
122         values[i++] = loggingEvent.getLoggerName();
123         values[i++] = loggingEvent.getLoggerContextVO();
124         values[i++] = loggingEvent.getLevel().toInteger();
125         values[i++] = loggingEvent.getTimeStamp();
126 //        // In order to avoid NullPointerException, we could push a dummy marker if
127 //        // the event's marker is null. However, this would surprise user who
128 //        // expect to see a null marker instead of a dummy one.
129 
130         MarkerList markerList = new MarkerList(loggingEvent.getMarkerList());
131         Marker marker = markerList.getFirstMarker();
132         values[i++] = marker;
133         values[i++] = markerList;
134 
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 }