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 java.io.ByteArrayOutputStream;
020import java.io.PrintStream;
021import java.io.Serializable;
022import java.text.SimpleDateFormat;
023import java.util.Date;
024
025import org.apache.logging.log4j.Level;
026import org.apache.logging.log4j.message.Message;
027
028import static org.apache.logging.log4j.util.Chars.*;
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     * 
047     * @param caller The method that created the event.
048     * @param level The logging level.
049     * @param msg The message String.
050     * @param t The Error or Exception that occurred.
051     * @param threadName The thread name
052     */
053    public StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t,
054            final String threadName) {
055        this.timestamp = System.currentTimeMillis();
056        this.caller = caller;
057        this.level = level;
058        this.msg = msg;
059        this.throwable = t;
060        this.threadName = threadName;
061    }
062
063    /**
064     * Returns the event's timestamp.
065     * 
066     * @return The event's timestamp.
067     */
068    public long getTimestamp() {
069        return timestamp;
070    }
071
072    /**
073     * Returns the StackTraceElement for the method that created the event.
074     * 
075     * @return The StackTraceElement.
076     */
077    public StackTraceElement getStackTraceElement() {
078        return caller;
079    }
080
081    /**
082     * Returns the logging level for the event.
083     * 
084     * @return The logging level.
085     */
086    public Level getLevel() {
087        return level;
088    }
089
090    /**
091     * Returns the message associated with the event.
092     * 
093     * @return The message associated with the event.
094     */
095    public Message getMessage() {
096        return msg;
097    }
098
099    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}