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 org.apache.logging.log4j.Level;
20 import org.apache.logging.log4j.message.Message;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.PrintStream;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26
27
28
29
30 public class StatusData {
31
32 private final long timestamp;
33
34 private final StackTraceElement caller;
35
36 private final Level level;
37
38 private final Message msg;
39
40 private final Throwable throwable;
41
42
43
44
45
46
47
48
49 public StatusData(StackTraceElement caller, Level level, Message msg, Throwable t) {
50 this.timestamp = System.currentTimeMillis();
51 this.caller = caller;
52 this.level = level;
53 this.msg = msg;
54 this.throwable = t;
55 }
56
57
58
59
60
61 public long getTimestamp() {
62 return timestamp;
63 }
64
65
66
67
68
69 public StackTraceElement getStackTraceElement() {
70 return caller;
71 }
72
73
74
75
76
77 public Level getLevel() {
78 return level;
79 }
80
81
82
83
84
85 public Message getMessage() {
86 return msg;
87 }
88
89
90
91
92
93 public Throwable getThrowable() {
94 return throwable;
95 }
96
97
98
99
100
101 public String getFormattedStatus() {
102 StringBuilder sb = new StringBuilder();
103 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
104 sb.append(format.format(new Date(timestamp)));
105 sb.append(" ");
106 sb.append(level.toString());
107 sb.append(" ");
108 sb.append(msg.getFormattedMessage());
109 Object[] params = msg.getParameters();
110 Throwable t;
111 if (throwable == null && params != null && params[params.length - 1] instanceof Throwable) {
112 t = (Throwable) params[params.length - 1];
113 } else {
114 t = throwable;
115 }
116 if (t != null) {
117 sb.append(" ");
118 ByteArrayOutputStream baos = new ByteArrayOutputStream();
119 t.printStackTrace(new PrintStream(baos));
120 sb.append(baos.toString());
121 }
122 return sb.toString();
123 }
124 }