001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.status;
018    
019    import org.apache.logging.log4j.Level;
020    import org.apache.logging.log4j.message.Message;
021    
022    import java.io.ByteArrayOutputStream;
023    import java.io.PrintStream;
024    import java.text.SimpleDateFormat;
025    import java.util.Date;
026    
027    /**
028     * The Status data.
029     */
030    public class StatusData {
031    
032        private final long timestamp;
033    
034        private final StackTraceElement caller;
035    
036        private final Level level;
037    
038        private final Message msg;
039    
040        private final Throwable throwable;
041    
042        /**
043         * Creates the StatusData object.
044         * @param caller The method that created the event.
045         * @param level The logging level.
046         * @param msg The message String.
047         * @param t The Error or Exception that occurred.
048         */
049        public StatusData(StackTraceElement caller, Level level, Message msg, Throwable t) {
050            this.timestamp = System.currentTimeMillis();
051            this.caller = caller;
052            this.level = level;
053            this.msg = msg;
054            this.throwable = t;
055        }
056    
057        /**
058         * Returns the event's timestamp.
059         * @return The event's timestamp.
060         */
061        public long getTimestamp() {
062            return timestamp;
063        }
064    
065        /**
066         * Returns the StackTraceElement for the method that created the event.
067         * @return The StackTraceElement.
068         */
069        public StackTraceElement getStackTraceElement() {
070            return caller;
071        }
072    
073        /**
074         * Returns the logging level for the event.
075         * @return The logging level.
076         */
077        public Level getLevel() {
078            return level;
079        }
080    
081        /**
082         * Returns the message associated with the event.
083         * @return The message associated with the event.
084         */
085        public Message getMessage() {
086            return msg;
087        }
088    
089        /**
090         * Returns the Throwable associated with the event.
091         * @return The Throwable associated with the event.
092         */
093        public Throwable getThrowable() {
094            return throwable;
095        }
096    
097        /**
098         * Formats the StatusData for viewing.
099         * @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    }