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 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   * 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       * 
47       * @param caller The method that created the event.
48       * @param level The logging level.
49       * @param msg The message String.
50       * @param t The Error or Exception that occurred.
51       * @param threadName The thread name
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       * Returns the event's timestamp.
65       * 
66       * @return The event's timestamp.
67       */
68      public long getTimestamp() {
69          return timestamp;
70      }
71  
72      /**
73       * Returns the StackTraceElement for the method that created the event.
74       * 
75       * @return The StackTraceElement.
76       */
77      public StackTraceElement getStackTraceElement() {
78          return caller;
79      }
80  
81      /**
82       * Returns the logging level for the event.
83       * 
84       * @return The logging level.
85       */
86      public Level getLevel() {
87          return level;
88      }
89  
90      /**
91       * Returns the message associated with the event.
92       * 
93       * @return The message associated with the event.
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      * Returns the Throwable associated with the event.
108      * 
109      * @return The Throwable associated with the event.
110      */
111     public Throwable getThrowable() {
112         return throwable;
113     }
114 
115     /**
116      * Formats the StatusData for viewing.
117      * 
118      * @return The formatted status data as a String.
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 }