Skip to content

Commit 32259b3

Browse files
committed
add an instance version of StatusPrinter
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
1 parent f8f0ed9 commit 32259b3

File tree

2 files changed

+38
-129
lines changed

2 files changed

+38
-129
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* 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.
44
*
55
* This program and the accompanying materials are dual-licensed under
66
* either the terms of the Eclipse Public License v1.0 as published by
@@ -13,177 +13,89 @@
1313
*/
1414
package ch.qos.logback.core.util;
1515

16-
import java.io.PrintStream;
17-
import java.util.Iterator;
18-
import java.util.List;
19-
2016
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;
2419

25-
import static ch.qos.logback.core.status.StatusUtil.filterStatusListByTimeThreshold;
20+
import java.io.PrintStream;
21+
import java.util.List;
2622

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+
*/
2729
public class StatusPrinter {
2830

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();
3232

3333
public static void setPrintStream(PrintStream printStream) {
34-
ps = printStream;
34+
SINGLETON.setPrintStream(printStream);
3535
}
3636

3737
/**
3838
* Print the contents of the context statuses, but only if they contain warnings
3939
* or errors.
4040
*
41-
* @param context
41+
* @param context a context to print
4242
*/
4343
public static void printInCaseOfErrorsOrWarnings(Context context) {
44-
printInCaseOfErrorsOrWarnings(context, 0);
44+
SINGLETON.printInCaseOfErrorsOrWarnings(context, 0);
4545
}
4646

4747
/**
4848
* Print the contents of the context status, but only if they contain warnings
4949
* or errors occurring later than the threshold.
5050
*
51-
* @param context
51+
* @param context a context to print
52+
* @param threshold filter events later than the threshold
5253
*/
5354
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);
6756
}
6857

6958
/**
7059
* Print the contents of the context statuses, but only if they contain errors.
7160
*
72-
* @param context
61+
* @param context a context to print
7362
*/
7463
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);
8865
}
8966

9067
/**
9168
* Print the contents of the context's status data.
9269
*
93-
* @param context
70+
* @param context a context to print
9471
*/
9572
public static void print(Context context) {
96-
print(context, 0);
73+
SINGLETON.print(context, 0);
9774
}
9875

9976
/**
10077
* Print context's status data with a timestamp higher than the threshold.
10178
*
102-
* @param context
79+
* @param context a context to print
80+
* @param threshold filter events later than the threshold
10381
*/
10482
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);
11584
}
11685

11786
public static void print(StatusManager sm) {
118-
print(sm, 0);
87+
SINGLETON.print(sm, 0);
11988
}
12089

12190
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);
12692
}
12793

12894
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);
16196
}
16297

16398
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);
187100
}
188-
189101
}

logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter2.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
/**
3232
*
33-
* Same as StatusPrinter but with instance methods not static.
33+
* Same as StatusPrinter but with instance methods instead of static.
3434
*
3535
* @since 1.5.4
3636
*/
@@ -49,7 +49,7 @@ public void setPrintStream(PrintStream printStream) {
4949
* Print the contents of the context statuses, but only if they contain warnings
5050
* or errors.
5151
*
52-
* @param context
52+
* @param context a context to print
5353
*/
5454
public void printInCaseOfErrorsOrWarnings(Context context) {
5555
printInCaseOfErrorsOrWarnings(context, 0);
@@ -59,7 +59,8 @@ public void printInCaseOfErrorsOrWarnings(Context context) {
5959
* Print the contents of the context status, but only if they contain warnings
6060
* or errors occurring later than the threshold.
6161
*
62-
* @param context
62+
* @param context a context to print
63+
* @param threshold filter events later than the threshold
6364
*/
6465
public void printInCaseOfErrorsOrWarnings(Context context, long threshold) {
6566
if (context == null) {
@@ -80,7 +81,7 @@ public void printInCaseOfErrorsOrWarnings(Context context, long threshold) {
8081
/**
8182
* Print the contents of the context statuses, but only if they contain errors.
8283
*
83-
* @param context
84+
* @param context a context to print
8485
*/
8586
public void printIfErrorsOccured(Context context) {
8687
if (context == null) {
@@ -101,7 +102,7 @@ public void printIfErrorsOccured(Context context) {
101102
/**
102103
* Print the contents of the context's status data.
103104
*
104-
* @param context
105+
* @param context a context to print
105106
*/
106107
public void print(Context context) {
107108
print(context, 0);
@@ -110,7 +111,7 @@ public void print(Context context) {
110111
/**
111112
* Print context's status data with a timestamp higher than the threshold.
112113
*
113-
* @param context
114+
* @param context a context to print
114115
*/
115116
public void print(Context context, long threshold) {
116117
if (context == null) {
@@ -150,11 +151,7 @@ private void buildStrFromStatusList(StringBuilder sb, List<Status> statusList) {
150151
}
151152
}
152153

153-
// private static void buildStrFromStatusManager(StringBuilder sb, StatusManager
154-
// sm) {
155-
// }
156-
157-
private static void appendThrowable(StringBuilder sb, Throwable t) {
154+
private void appendThrowable(StringBuilder sb, Throwable t) {
158155
String[] stringRep = ThrowableToStringArray.convert(t);
159156

160157
for (String s : stringRep) {
@@ -171,7 +168,7 @@ private static void appendThrowable(StringBuilder sb, Throwable t) {
171168
}
172169
}
173170

174-
public static void buildStr(StringBuilder sb, String indentation, Status s) {
171+
public void buildStr(StringBuilder sb, String indentation, Status s) {
175172
String prefix;
176173
if (s.hasChildren()) {
177174
prefix = indentation + "+ ";

0 commit comments

Comments
 (0)