View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.status;
18  
19  import static org.apache.logging.log4j.util.Chars.SPACE;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.PrintStream;
23  import java.io.Serializable;
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  
27  import org.apache.logging.log4j.Level;
28  import org.apache.logging.log4j.message.Message;
29  
30  /**
31   * The Status data.
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       * Creates the StatusData object.
46       * @param caller The method that created the event.
47       * @param level The logging level.
48       * @param msg The message String.
49       * @param t The Error or Exception that occurred.
50       * @param threadName The thread name
51       */
52      public StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t, String threadName) {
53          this.timestamp = System.currentTimeMillis();
54          this.caller = caller;
55          this.level = level;
56          this.msg = msg;
57          this.throwable = t;
58          this.threadName = threadName;
59      }
60  
61      /**
62       * Returns the event's timestamp.
63       * @return The event's timestamp.
64       */
65      public long getTimestamp() {
66          return timestamp;
67      }
68  
69      /**
70       * Returns the StackTraceElement for the method that created the event.
71       * @return The StackTraceElement.
72       */
73      public StackTraceElement getStackTraceElement() {
74          return caller;
75      }
76  
77      /**
78       * Returns the logging level for the event.
79       * @return The logging level.
80       */
81      public Level getLevel() {
82          return level;
83      }
84  
85      /**
86       * Returns the message associated with the event.
87       * @return The message associated with the event.
88       */
89      public Message getMessage() {
90          return msg;
91      }
92  
93      public String getThreadName() {
94          if (threadName == null) {
95              threadName = Thread.currentThread().getName();
96          }
97          return threadName;
98      }
99  
100     /**
101      * Returns the Throwable associated with the event.
102      * @return The Throwable associated with the event.
103      */
104     public Throwable getThrowable() {
105         return throwable;
106     }
107 
108     /**
109      * Formats the StatusData for viewing.
110      * @return The formatted status data as a String.
111      */
112     public String getFormattedStatus() {
113         final StringBuilder sb = new StringBuilder();
114         final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
115         sb.append(format.format(new Date(timestamp)));
116         sb.append(SPACE);
117         sb.append(getThreadName());
118         sb.append(SPACE);
119         sb.append(level.toString());
120         sb.append(SPACE);
121         sb.append(msg.getFormattedMessage());
122         final Object[] params = msg.getParameters();
123         Throwable t;
124         if (throwable == null && params != null && params[params.length - 1] instanceof Throwable) {
125             t = (Throwable) params[params.length - 1];
126         } else {
127             t = throwable;
128         }
129         if (t != null) {
130             sb.append(SPACE);
131             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
132             t.printStackTrace(new PrintStream(baos));
133             sb.append(baos.toString());
134         }
135         return sb.toString();
136     }
137 }