View Javadoc

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