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.core.util;
15  
16  import java.io.PrintStream;
17  import java.util.Iterator;
18  import java.util.List;
19  
20  import ch.qos.logback.core.Context;
21  import ch.qos.logback.core.CoreConstants;
22  import ch.qos.logback.core.helpers.ThrowableToStringArray;
23  import ch.qos.logback.core.status.*;
24  
25  import static ch.qos.logback.core.status.StatusUtil.filterStatusListByTimeThreshold;
26  
27  public class StatusPrinter {
28  
29      private static PrintStream ps = System.out;
30  
31      static CachingDateFormatter cachingDateFormat = new CachingDateFormatter("HH:mm:ss,SSS");
32  
33      public static void setPrintStream(PrintStream printStream) {
34          ps = printStream;
35      }
36  
37      /**
38       * Print the contents of the context statuses, but only if they contain
39       * warnings or errors.
40       *
41       * @param context
42       */
43      public static void printInCaseOfErrorsOrWarnings(Context context) {
44          printInCaseOfErrorsOrWarnings(context, 0);
45      }
46  
47      /**
48       * Print the contents of the context status, but only if they contain
49       * warnings or errors occurring later then the threshold.
50       *
51       * @param context
52       */
53      public static void printInCaseOfErrorsOrWarnings(Context context, long threshold) {
54          if (context == null) {
55              throw new IllegalArgumentException("Context argument cannot be null");
56          }
57  
58          StatusManager sm = context.getStatusManager();
59          if (sm == null) {
60              ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager");
61          } else {
62              StatusUtil statusUtil = new StatusUtil(context);
63              if (statusUtil.getHighestLevel(threshold) >= ErrorStatus.WARN) {
64                  print(sm, threshold);
65              }
66          }
67      }
68  
69      /**
70       * Print the contents of the context statuses, but only if they contain
71       * errors.
72       *
73       * @param context
74       */
75      public static void printIfErrorsOccured(Context context) {
76          if (context == null) {
77              throw new IllegalArgumentException("Context argument cannot be null");
78          }
79  
80          StatusManager sm = context.getStatusManager();
81          if (sm == null) {
82              ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager");
83          } else {
84              StatusUtil statusUtil = new StatusUtil(context);
85              if (statusUtil.getHighestLevel(0) == ErrorStatus.ERROR) {
86                  print(sm);
87              }
88          }
89      }
90  
91      /**
92       * Print the contents of the context's status data.
93       *
94       * @param context
95       */
96      public static void print(Context context) {
97          print(context, 0);
98      }
99  
100     /**
101     * Print context's status data with a timestamp higher than the threshold.
102     * @param context
103     */
104     public static void print(Context context, long threshold) {
105         if (context == null) {
106             throw new IllegalArgumentException("Context argument cannot be null");
107         }
108 
109         StatusManager sm = context.getStatusManager();
110         if (sm == null) {
111             ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager");
112         } else {
113             print(sm, threshold);
114         }
115     }
116 
117     public static void print(StatusManager sm) {
118         print(sm, 0);
119     }
120 
121     public static void print(StatusManager sm, long threshold) {
122         StringBuilder sb = new StringBuilder();
123         List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
124         buildStrFromStatusList(sb, filteredList);
125         ps.println(sb.toString());
126     }
127 
128     public static void print(List<Status> statusList) {
129         StringBuilder sb = new StringBuilder();
130         buildStrFromStatusList(sb, statusList);
131         ps.println(sb.toString());
132     }
133 
134     private static void buildStrFromStatusList(StringBuilder sb, List<Status> statusList) {
135         if (statusList == null)
136             return;
137         for (Status s : statusList) {
138             buildStr(sb, "", s);
139         }
140     }
141 
142     // private static void buildStrFromStatusManager(StringBuilder sb, StatusManager sm) {
143     // }
144 
145     private static void appendThrowable(StringBuilder sb, Throwable t) {
146         String[] stringRep = ThrowableToStringArray.convert(t);
147 
148         for (String s : stringRep) {
149             if (s.startsWith(CoreConstants.CAUSED_BY)) {
150                 // nothing
151             } else if (Character.isDigit(s.charAt(0))) {
152                 // if line resembles "48 common frames omitted"
153                 sb.append("\t... ");
154             } else {
155                 // most of the time. just add a tab+"at"
156                 sb.append("\tat ");
157             }
158             sb.append(s).append(CoreConstants.LINE_SEPARATOR);
159         }
160     }
161 
162     public static void buildStr(StringBuilder sb, String indentation, Status s) {
163         String prefix;
164         if (s.hasChildren()) {
165             prefix = indentation + "+ ";
166         } else {
167             prefix = indentation + "|-";
168         }
169 
170         if (cachingDateFormat != null) {
171             String dateStr = cachingDateFormat.format(s.getDate());
172             sb.append(dateStr).append(" ");
173         }
174         sb.append(prefix).append(s).append(CoreConstants.LINE_SEPARATOR);
175 
176         if (s.getThrowable() != null) {
177             appendThrowable(sb, s.getThrowable());
178         }
179         if (s.hasChildren()) {
180             Iterator<Status> ite = s.iterator();
181             while (ite.hasNext()) {
182                 Status child = ite.next();
183                 buildStr(sb, indentation + "  ", child);
184             }
185         }
186     }
187 
188 }