1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.status;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.PrintStream;
21 import java.io.Serializable;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24
25 import org.apache.logging.log4j.Level;
26 import org.apache.logging.log4j.message.Message;
27
28 import static org.apache.logging.log4j.util.Chars.*;
29
30
31
32
33 public class StatusData implements Serializable {
34
35 private static final long serialVersionUID = -4341916115118014017L;
36
37 private final long timestamp;
38 private final StackTraceElement caller;
39 private final Level level;
40 private final Message msg;
41 private String threadName;
42 private final Throwable throwable;
43
44
45
46
47
48
49
50
51
52
53 public StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t,
54 final String threadName) {
55 this.timestamp = System.currentTimeMillis();
56 this.caller = caller;
57 this.level = level;
58 this.msg = msg;
59 this.throwable = t;
60 this.threadName = threadName;
61 }
62
63
64
65
66
67
68 public long getTimestamp() {
69 return timestamp;
70 }
71
72
73
74
75
76
77 public StackTraceElement getStackTraceElement() {
78 return caller;
79 }
80
81
82
83
84
85
86 public Level getLevel() {
87 return level;
88 }
89
90
91
92
93
94
95 public Message getMessage() {
96 return msg;
97 }
98
99 public String getThreadName() {
100 if (threadName == null) {
101 threadName = Thread.currentThread().getName();
102 }
103 return threadName;
104 }
105
106
107
108
109
110
111 public Throwable getThrowable() {
112 return throwable;
113 }
114
115
116
117
118
119
120 public String getFormattedStatus() {
121 final StringBuilder sb = new StringBuilder();
122 final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
123 sb.append(format.format(new Date(timestamp)));
124 sb.append(SPACE);
125 sb.append(getThreadName());
126 sb.append(SPACE);
127 sb.append(level.toString());
128 sb.append(SPACE);
129 sb.append(msg.getFormattedMessage());
130 final Object[] params = msg.getParameters();
131 Throwable t;
132 if (throwable == null && params != null && params[params.length - 1] instanceof Throwable) {
133 t = (Throwable) params[params.length - 1];
134 } else {
135 t = throwable;
136 }
137 if (t != null) {
138 sb.append(SPACE);
139 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
140 t.printStackTrace(new PrintStream(baos));
141 sb.append(baos.toString());
142 }
143 return sb.toString();
144 }
145 }