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 */
017package org.apache.logging.log4j.status;
018
019import static org.apache.logging.log4j.util.Chars.SPACE;
020
021import java.io.ByteArrayOutputStream;
022import java.io.PrintStream;
023import java.io.Serializable;
024import java.text.SimpleDateFormat;
025import java.util.Date;
026
027import org.apache.logging.log4j.Level;
028import org.apache.logging.log4j.message.Message;
029
030/**
031 * The Status data.
032 */
033public class StatusData implements Serializable {
034
035    private static final long serialVersionUID = -4341916115118014017L;
036
037    private final long timestamp;
038    private final StackTraceElement caller;
039    private final Level level;
040    private final Message msg;
041    private String threadName;
042    private final Throwable throwable;
043
044    /**
045     * Creates the StatusData object.
046     * @param caller The method that created the event.
047     * @param level The logging level.
048     * @param msg The message String.
049     * @param t The Error or Exception that occurred.
050     * @param threadName The thread name
051     */
052    public StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t, String threadName) {
053        this.timestamp = System.currentTimeMillis();
054        this.caller = caller;
055        this.level = level;
056        this.msg = msg;
057        this.throwable = t;
058        this.threadName = threadName;
059    }
060
061    /**
062     * Returns the event's timestamp.
063     * @return The event's timestamp.
064     */
065    public long getTimestamp() {
066        return timestamp;
067    }
068
069    /**
070     * Returns the StackTraceElement for the method that created the event.
071     * @return The StackTraceElement.
072     */
073    public StackTraceElement getStackTraceElement() {
074        return caller;
075    }
076
077    /**
078     * Returns the logging level for the event.
079     * @return The logging level.
080     */
081    public Level getLevel() {
082        return level;
083    }
084
085    /**
086     * Returns the message associated with the event.
087     * @return The message associated with the event.
088     */
089    public Message getMessage() {
090        return msg;
091    }
092
093    public String getThreadName() {
094        if (threadName == null) {
095            threadName = Thread.currentThread().getName();
096        }
097        return threadName;
098    }
099
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}