|
1 | 1 | /** |
2 | 2 | * Logback: the reliable, generic, fast and flexible logging framework. |
3 | | - * Copyright (C) 1999-2015, QOS.ch. All rights reserved. |
| 3 | + * Copyright (C) 1999-2024, QOS.ch. All rights reserved. |
4 | 4 | * |
5 | 5 | * This program and the accompanying materials are dual-licensed under |
6 | 6 | * either the terms of the Eclipse Public License v1.0 as published by |
|
13 | 13 | */ |
14 | 14 | package ch.qos.logback.core.util; |
15 | 15 |
|
16 | | -import java.io.PrintStream; |
17 | | -import java.util.Iterator; |
18 | | -import java.util.List; |
19 | | - |
20 | 16 | 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.*; |
| 17 | +import ch.qos.logback.core.status.Status; |
| 18 | +import ch.qos.logback.core.status.StatusManager; |
24 | 19 |
|
25 | | -import static ch.qos.logback.core.status.StatusUtil.filterStatusListByTimeThreshold; |
| 20 | +import java.io.PrintStream; |
| 21 | +import java.util.List; |
26 | 22 |
|
| 23 | +/** |
| 24 | + * This class print status messages of a given {@link Context}. However, all its methods are |
| 25 | + * static. Use {@link StatusPrinter2} instead |
| 26 | + * |
| 27 | + * @deprecated replaced by {@link StatusPrinter2} |
| 28 | + */ |
27 | 29 | public class StatusPrinter { |
28 | 30 |
|
29 | | - private static PrintStream ps = System.out; |
30 | | - |
31 | | - static CachingDateFormatter cachingDateFormat = new CachingDateFormatter("HH:mm:ss,SSS"); |
| 31 | + private final static StatusPrinter2 SINGLETON = new StatusPrinter2(); |
32 | 32 |
|
33 | 33 | public static void setPrintStream(PrintStream printStream) { |
34 | | - ps = printStream; |
| 34 | + SINGLETON.setPrintStream(printStream); |
35 | 35 | } |
36 | 36 |
|
37 | 37 | /** |
38 | 38 | * Print the contents of the context statuses, but only if they contain warnings |
39 | 39 | * or errors. |
40 | 40 | * |
41 | | - * @param context |
| 41 | + * @param context a context to print |
42 | 42 | */ |
43 | 43 | public static void printInCaseOfErrorsOrWarnings(Context context) { |
44 | | - printInCaseOfErrorsOrWarnings(context, 0); |
| 44 | + SINGLETON.printInCaseOfErrorsOrWarnings(context, 0); |
45 | 45 | } |
46 | 46 |
|
47 | 47 | /** |
48 | 48 | * Print the contents of the context status, but only if they contain warnings |
49 | 49 | * or errors occurring later than the threshold. |
50 | 50 | * |
51 | | - * @param context |
| 51 | + * @param context a context to print |
| 52 | + * @param threshold filter events later than the threshold |
52 | 53 | */ |
53 | 54 | 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 | | - } |
| 55 | + SINGLETON.printInCaseOfErrorsOrWarnings(context, threshold); |
67 | 56 | } |
68 | 57 |
|
69 | 58 | /** |
70 | 59 | * Print the contents of the context statuses, but only if they contain errors. |
71 | 60 | * |
72 | | - * @param context |
| 61 | + * @param context a context to print |
73 | 62 | */ |
74 | 63 | public static void printIfErrorsOccured(Context context) { |
75 | | - if (context == null) { |
76 | | - throw new IllegalArgumentException("Context argument cannot be null"); |
77 | | - } |
78 | | - |
79 | | - StatusManager sm = context.getStatusManager(); |
80 | | - if (sm == null) { |
81 | | - ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager"); |
82 | | - } else { |
83 | | - StatusUtil statusUtil = new StatusUtil(context); |
84 | | - if (statusUtil.getHighestLevel(0) == ErrorStatus.ERROR) { |
85 | | - print(sm); |
86 | | - } |
87 | | - } |
| 64 | + SINGLETON.printIfErrorsOccured(context); |
88 | 65 | } |
89 | 66 |
|
90 | 67 | /** |
91 | 68 | * Print the contents of the context's status data. |
92 | 69 | * |
93 | | - * @param context |
| 70 | + * @param context a context to print |
94 | 71 | */ |
95 | 72 | public static void print(Context context) { |
96 | | - print(context, 0); |
| 73 | + SINGLETON.print(context, 0); |
97 | 74 | } |
98 | 75 |
|
99 | 76 | /** |
100 | 77 | * Print context's status data with a timestamp higher than the threshold. |
101 | 78 | * |
102 | | - * @param context |
| 79 | + * @param context a context to print |
| 80 | + * @param threshold filter events later than the threshold |
103 | 81 | */ |
104 | 82 | 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 | | - } |
| 83 | + SINGLETON.print(context, threshold); |
115 | 84 | } |
116 | 85 |
|
117 | 86 | public static void print(StatusManager sm) { |
118 | | - print(sm, 0); |
| 87 | + SINGLETON.print(sm, 0); |
119 | 88 | } |
120 | 89 |
|
121 | 90 | 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()); |
| 91 | + SINGLETON.print(sm, threshold); |
126 | 92 | } |
127 | 93 |
|
128 | 94 | 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 |
143 | | - // sm) { |
144 | | - // } |
145 | | - |
146 | | - private static void appendThrowable(StringBuilder sb, Throwable t) { |
147 | | - String[] stringRep = ThrowableToStringArray.convert(t); |
148 | | - |
149 | | - for (String s : stringRep) { |
150 | | - if (s.startsWith(CoreConstants.CAUSED_BY)) { |
151 | | - // nothing |
152 | | - } else if (Character.isDigit(s.charAt(0))) { |
153 | | - // if line resembles "48 common frames omitted" |
154 | | - sb.append("\t... "); |
155 | | - } else { |
156 | | - // most of the time. just add a tab+"at" |
157 | | - sb.append("\tat "); |
158 | | - } |
159 | | - sb.append(s).append(CoreConstants.LINE_SEPARATOR); |
160 | | - } |
| 95 | + SINGLETON.print(statusList); |
161 | 96 | } |
162 | 97 |
|
163 | 98 | public static void buildStr(StringBuilder sb, String indentation, Status s) { |
164 | | - String prefix; |
165 | | - if (s.hasChildren()) { |
166 | | - prefix = indentation + "+ "; |
167 | | - } else { |
168 | | - prefix = indentation + "|-"; |
169 | | - } |
170 | | - |
171 | | - if (cachingDateFormat != null) { |
172 | | - String dateStr = cachingDateFormat.format(s.getTimestamp()); |
173 | | - sb.append(dateStr).append(" "); |
174 | | - } |
175 | | - sb.append(prefix).append(s).append(CoreConstants.LINE_SEPARATOR); |
176 | | - |
177 | | - if (s.getThrowable() != null) { |
178 | | - appendThrowable(sb, s.getThrowable()); |
179 | | - } |
180 | | - if (s.hasChildren()) { |
181 | | - Iterator<Status> ite = s.iterator(); |
182 | | - while (ite.hasNext()) { |
183 | | - Status child = ite.next(); |
184 | | - buildStr(sb, indentation + " ", child); |
185 | | - } |
186 | | - } |
| 99 | + SINGLETON.buildStr(sb, indentation, s); |
187 | 100 | } |
188 | | - |
189 | 101 | } |
0 commit comments