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 028/** 029 * The Status data. 030 */ 031public class StatusData implements Serializable { 032 private static final long serialVersionUID = -4341916115118014017L; 033 034 private final long timestamp; 035 private final StackTraceElement caller; 036 private final Level level; 037 private final Message msg; 038 private final Throwable throwable; 039 040 /** 041 * Creates the StatusData object. 042 * @param caller The method that created the event. 043 * @param level The logging level. 044 * @param msg The message String. 045 * @param t The Error or Exception that occurred. 046 */ 047 public StatusData(final StackTraceElement caller, final Level level, final Message msg, final Throwable t) { 048 this.timestamp = System.currentTimeMillis(); 049 this.caller = caller; 050 this.level = level; 051 this.msg = msg; 052 this.throwable = t; 053 } 054 055 /** 056 * Returns the event's timestamp. 057 * @return The event's timestamp. 058 */ 059 public long getTimestamp() { 060 return timestamp; 061 } 062 063 /** 064 * Returns the StackTraceElement for the method that created the event. 065 * @return The StackTraceElement. 066 */ 067 public StackTraceElement getStackTraceElement() { 068 return caller; 069 } 070 071 /** 072 * Returns the logging level for the event. 073 * @return The logging level. 074 */ 075 public Level getLevel() { 076 return level; 077 } 078 079 /** 080 * Returns the message associated with the event. 081 * @return The message associated with the event. 082 */ 083 public Message getMessage() { 084 return msg; 085 } 086 087 /** 088 * Returns the Throwable associated with the event. 089 * @return The Throwable associated with the event. 090 */ 091 public Throwable getThrowable() { 092 return throwable; 093 } 094 095 /** 096 * Formats the StatusData for viewing. 097 * @return The formatted status data as a String. 098 */ 099 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}