1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47 public static void printInCaseOfErrorsOrWarnings(Context context) {
48 printInCaseOfErrorsOrWarnings(context, 0);
49 }
50
51
52
53
54
55
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
76
77
78
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
99
100
101
102 public static void print(Context context) {
103 print(context, 0);
104 }
105
106
107
108
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
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
161 } else if (Character.isDigit(s.charAt(0))) {
162
163 sb.append("\t... ");
164 } else {
165
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 }