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  /**
29   * The Status data.
30   */
31  public class StatusData implements Serializable {
32      private static final long serialVersionUID = -4341916115118014017L;
33  
34      private final long timestamp;
35      private final StackTraceElement caller;
36      private final Level level;
37      private final Message msg;
38      private final Throwable throwable;
39  
40      /**
41       * Creates the StatusData object.
42       * @param caller The method that created the event.
43       * @param level The logging level.
44       * @param msg The message String.
45       * @param t The Error or Exception that occurred.
46       */
47      public StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t) {
48          this.timestamp = System.currentTimeMillis();
49          this.caller = caller;
50          this.level = level;
51          this.msg = msg;
52          this.throwable = t;
53      }
54  
55      /**
56       * Returns the event's timestamp.
57       * @return The event's timestamp.
58       */
59      public long getTimestamp() {
60          return timestamp;
61      }
62  
63      /**
64       * Returns the StackTraceElement for the method that created the event.
65       * @return The StackTraceElement.
66       */
67      public StackTraceElement getStackTraceElement() {
68          return caller;
69      }
70  
71      /**
72       * Returns the logging level for the event.
73       * @return The logging level.
74       */
75      public Level getLevel() {
76          return level;
77      }
78  
79      /**
80       * Returns the message associated with the event.
81       * @return The message associated with the event.
82       */
83      public Message getMessage() {
84          return msg;
85      }
86  
87      /**
88       * Returns the Throwable associated with the event.
89       * @return The Throwable associated with the event.
90       */
91      public Throwable getThrowable() {
92          return throwable;
93      }
94  
95      /**
96       * Formats the StatusData for viewing.
97       * @return The formatted status data as a String.
98       */
99      public String getFormattedStatus() {
100         final StringBuilder sb = new StringBuilder();
101         final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
102         sb.append(format.format(new Date(timestamp)));
103         sb.append(' ');
104         sb.append(level.toString());
105         sb.append(' ');
106         sb.append(msg.getFormattedMessage());
107         final Object[] params = msg.getParameters();
108         Throwable t;
109         if (throwable == null && params != null && params[params.length - 1] instanceof Throwable) {
110             t = (Throwable) params[params.length - 1];
111         } else {
112             t = throwable;
113         }
114         if (t != null) {
115             sb.append(' ');
116             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
117             t.printStackTrace(new PrintStream(baos));
118             sb.append(baos.toString());
119         }
120         return sb.toString();
121     }
122 }