• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.apache.velocity.test.misc;
2 
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21 
22 import org.slf4j.helpers.FormattingTuple;
23 import org.slf4j.helpers.MarkerIgnoringBase;
24 import org.slf4j.helpers.MessageFormatter;
25 import org.slf4j.spi.LocationAwareLogger;
26 
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 
30 /**
31  * Logger implementation that can easily capture output
32  * or suppress it entirely.  By default, both capture and suppress
33  * are on. To have this behave like a normal Logger,
34  * you must turn it on() and stopCapture().
35  *
36  * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
37  * @author Nathan Bubna
38  * @author <a href="mailto:cbrisson@apache.org">Claude Brisson</a>
39  * @version $Id$
40  */
41 public class TestLogger extends MarkerIgnoringBase
42 {
43     private ByteArrayOutputStream log;
44     private PrintStream systemDotIn;
45 
46     private boolean suppress = true;
47     private boolean capture = true;
48     private int enabledLevel = LOG_LEVEL_INFO;
49 
TestLogger()50     public TestLogger()
51     {
52         this(true, true);
53     }
54 
TestLogger(boolean suppress, boolean capture)55     public TestLogger(boolean suppress, boolean capture)
56     {
57         this.suppress = suppress;
58         this.capture = capture;
59         if (suppress)
60         {
61             off();
62         }
63         else if (capture)
64         {
65             startCapture();
66         }
67     }
68 
69     public static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
70     public static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
71     public static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
72     public static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
73     public static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
74 
stringToLevel(String levelStr)75     private static int stringToLevel(String levelStr)
76     {
77         if ("trace".equalsIgnoreCase(levelStr)) return LOG_LEVEL_TRACE;
78         else if ("debug".equalsIgnoreCase(levelStr)) return LOG_LEVEL_DEBUG;
79         else if ("info".equalsIgnoreCase(levelStr)) return LOG_LEVEL_INFO;
80         else if ("warn".equalsIgnoreCase(levelStr)) return LOG_LEVEL_WARN;
81         else if ("error".equalsIgnoreCase(levelStr)) return LOG_LEVEL_ERROR;
82         // assume INFO by default
83         return LOG_LEVEL_INFO;
84     }
85 
getPrefix(int level)86     private static String getPrefix(int level)
87     {
88         if (level <= LOG_LEVEL_TRACE) return " [trace] ";
89         else if (level <= LOG_LEVEL_DEBUG) return " [debug] ";
90         else if (level <= LOG_LEVEL_INFO) return "  [info] ";
91         else if (level <= LOG_LEVEL_WARN) return "  [warn] ";
92         else return " [error]";
93     }
94 
on()95     public synchronized void on()
96     {
97         if (suppress)
98         {
99             suppress = false;
100             if (capture)
101             {
102                 startCapture();
103             }
104         }
105     }
106 
off()107     public synchronized void off()
108     {
109         suppress = true;
110     }
111 
startCapture()112     public synchronized void startCapture()
113     {
114         capture = true;
115         if (!suppress)
116         {
117             log = new ByteArrayOutputStream();
118             systemDotIn = new PrintStream(log, true);
119         }
120     }
121 
stopCapture()122     public synchronized void stopCapture()
123     {
124         capture = false;
125     }
126 
setEnabledLevel(int level)127     public void setEnabledLevel(int level)
128     {
129         enabledLevel = level;
130     }
131 
isLevelEnabled(int level)132     public boolean isLevelEnabled(int level)
133     {
134         return !suppress && level >= enabledLevel;
135     }
136 
137     /**
138      * Return the captured log messages to date.
139      * @return log messages
140      */
getLog()141     public String getLog()
142     {
143         return log.toString();
144     }
145 
log(int level, String msg, Throwable t)146   private synchronized void log(int level, String msg, Throwable t)
147   {
148       if(!suppress && level >= enabledLevel)
149       {
150           PrintStream writer = capture ? systemDotIn : System.err;
151           writer.print(getPrefix(enabledLevel));
152           writer.println(msg);
153           if (t != null)
154           {
155               writer.println(t.getMessage());
156               t.printStackTrace(writer);
157           }
158           writer.flush();
159       }
160   }
161 
162   /**
163    * Logging API
164    */
165 
166   @Override
isTraceEnabled()167   public boolean isTraceEnabled()
168   {
169       return isLevelEnabled(LOG_LEVEL_TRACE);
170   }
171 
172   @Override
trace(String msg)173   public void trace(String msg)
174   {
175       log(LOG_LEVEL_TRACE, msg, null);
176   }
177 
178   @Override
trace(String format, Object arg)179   public void trace(String format, Object arg)
180   {
181       FormattingTuple ft = MessageFormatter.format(format, arg);
182       log(LOG_LEVEL_TRACE, ft.getMessage(), ft.getThrowable());
183   }
184 
185   @Override
trace(String format, Object arg1, Object arg2)186   public void trace(String format, Object arg1, Object arg2)
187   {
188       FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
189       log(LOG_LEVEL_TRACE, ft.getMessage(), ft.getThrowable());
190   }
191 
192   @Override
trace(String format, Object[] argArray)193   public void trace(String format, Object[] argArray)
194   {
195       FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
196       log(LOG_LEVEL_TRACE, ft.getMessage(), ft.getThrowable());
197   }
198 
199   @Override
trace(String msg, Throwable t)200   public void trace(String msg, Throwable t)
201   {
202       log(LOG_LEVEL_TRACE, msg, t);
203   }
204 
205   @Override
isDebugEnabled()206   public boolean isDebugEnabled()
207   {
208       return isLevelEnabled(LOG_LEVEL_DEBUG);
209   }
210 
211   @Override
debug(String msg)212   public void debug(String msg)
213   {
214       log(LOG_LEVEL_DEBUG, msg, null);
215   }
216 
217   @Override
debug(String format, Object arg)218   public void debug(String format, Object arg)
219   {
220       FormattingTuple ft = MessageFormatter.format(format, arg);
221       log(LOG_LEVEL_DEBUG, ft.getMessage(), ft.getThrowable());
222   }
223 
224   @Override
debug(String format, Object arg1, Object arg2)225   public void debug(String format, Object arg1, Object arg2)
226   {
227       FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
228       log(LOG_LEVEL_DEBUG, ft.getMessage(), ft.getThrowable());
229   }
230 
231   @Override
debug(String format, Object[] argArray)232   public void debug(String format, Object[] argArray)
233   {
234       FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
235       log(LOG_LEVEL_DEBUG, ft.getMessage(), ft.getThrowable());
236   }
237 
238   @Override
debug(String msg, Throwable t)239   public void debug(String msg, Throwable t)
240   {
241       log(LOG_LEVEL_DEBUG, msg, t);
242   }
243 
244   @Override
isInfoEnabled()245   public boolean isInfoEnabled()
246   {
247       return isLevelEnabled(LOG_LEVEL_INFO);
248   }
249 
250   @Override
info(String msg)251   public void info(String msg)
252   {
253       log(LOG_LEVEL_INFO, msg, null);
254   }
255 
256   @Override
info(String format, Object arg)257   public void info(String format, Object arg)
258   {
259       FormattingTuple ft = MessageFormatter.format(format, arg);
260       log(LOG_LEVEL_INFO, ft.getMessage(), ft.getThrowable());
261   }
262 
263   @Override
info(String format, Object arg1, Object arg2)264   public void info(String format, Object arg1, Object arg2)
265   {
266       FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
267       log(LOG_LEVEL_INFO, ft.getMessage(), ft.getThrowable());
268   }
269 
270   @Override
info(String format, Object[] argArray)271   public void info(String format, Object[] argArray)
272   {
273       FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
274       log(LOG_LEVEL_INFO, ft.getMessage(), ft.getThrowable());
275   }
276 
277   @Override
info(String msg, Throwable t)278   public void info(String msg, Throwable t)
279   {
280       log(LOG_LEVEL_INFO, msg, t);
281   }
282 
283   @Override
isWarnEnabled()284   public boolean isWarnEnabled()
285   {
286       return isLevelEnabled(LOG_LEVEL_WARN);
287   }
288 
289   @Override
warn(String msg)290   public void warn(String msg)
291   {
292       log(LOG_LEVEL_WARN, msg, null);
293   }
294 
295   @Override
warn(String format, Object arg)296   public void warn(String format, Object arg)
297   {
298       FormattingTuple ft = MessageFormatter.format(format, arg);
299       log(LOG_LEVEL_WARN, ft.getMessage(), ft.getThrowable());
300   }
301 
302   @Override
warn(String format, Object arg1, Object arg2)303   public void warn(String format, Object arg1, Object arg2)
304   {
305       FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
306       log(LOG_LEVEL_WARN, ft.getMessage(), ft.getThrowable());
307   }
308 
309   @Override
warn(String format, Object[] argArray)310   public void warn(String format, Object[] argArray)
311   {
312       FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
313       log(LOG_LEVEL_WARN, ft.getMessage(), ft.getThrowable());
314   }
315 
316   @Override
warn(String msg, Throwable t)317   public void warn(String msg, Throwable t)
318   {
319       log(LOG_LEVEL_WARN, msg, t);
320   }
321 
322   @Override
isErrorEnabled()323   public boolean isErrorEnabled()
324   {
325       return isLevelEnabled(LOG_LEVEL_ERROR);
326   }
327 
328   @Override
error(String msg)329   public void error(String msg)
330   {
331       log(LOG_LEVEL_ERROR, msg, null);
332   }
333 
334   @Override
error(String format, Object arg)335   public void error(String format, Object arg)
336   {
337       FormattingTuple ft = MessageFormatter.format(format, arg);
338       log(LOG_LEVEL_ERROR, ft.getMessage(), ft.getThrowable());
339   }
340 
341   @Override
error(String format, Object arg1, Object arg2)342   public void error(String format, Object arg1, Object arg2)
343   {
344       FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);
345       log(LOG_LEVEL_ERROR, ft.getMessage(), ft.getThrowable());
346   }
347 
348   @Override
error(String format, Object[] argArray)349   public void error(String format, Object[] argArray)
350   {
351       FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray);
352       log(LOG_LEVEL_ERROR, ft.getMessage(), ft.getThrowable());
353   }
354 
355   @Override
error(String msg, Throwable t)356   public void error(String msg, Throwable t)
357   {
358       log(LOG_LEVEL_ERROR, msg, t);
359   }
360 }
361