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 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   * The Status data.
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       * Creates the StatusData object.
44       * @param caller The method that created the event.
45       * @param level The logging level.
46       * @param msg The message String.
47       * @param t The Error or Exception that occurred.
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       * Returns the event's timestamp.
59       * @return The event's timestamp.
60       */
61      public long getTimestamp() {
62          return timestamp;
63      }
64  
65      /**
66       * Returns the StackTraceElement for the method that created the event.
67       * @return The StackTraceElement.
68       */
69      public StackTraceElement getStackTraceElement() {
70          return caller;
71      }
72  
73      /**
74       * Returns the logging level for the event.
75       * @return The logging level.
76       */
77      public Level getLevel() {
78          return level;
79      }
80  
81      /**
82       * Returns the message associated with the event.
83       * @return The message associated with the event.
84       */
85      public Message getMessage() {
86          return msg;
87      }
88  
89      /**
90       * Returns the Throwable associated with the event.
91       * @return The Throwable associated with the event.
92       */
93      public Throwable getThrowable() {
94          return throwable;
95      }
96  
97      /**
98       * Formats the StatusData for viewing.
99       * @return The formatted status data as a String.
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 }