import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
//import org.apache.avalon.framework.logger.AvalonFormatter;
import org.apache.log.format.PatternFormatter;
import org.apache.avalon.framework.ExceptionUtil;

public class MyFormatter extends PatternFormatter {

    private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

    private DateFormat                      m_simpleDateFormatRel;
    private final Date                      m_daterel = new Date();
    private final Calendar                  m_cal = Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT"));

    public MyFormatter(final String pattern) {
        super(pattern);
    }


    //TAKEN FROM AvalonFormatter because AvalonFormatter overrides getTime()!!!!
    /**
     * Utility method to format stack trace.
     *
     * @param throwable the throwable instance
     * @param format ancilliary format parameter - allowed to be null
     * @return the formatted string
     */
    protected String getStackTrace(final Throwable throwable, final String format) {
        if (null == throwable) return "";
        return ExceptionUtil.printStackTrace(throwable, 8, true);
    }


    /**
     * Utility method to format time.
     *
     * @param time the time
     * @param format ancilliary format parameter - allowed to be null
     * @return the formatted string
     */
    protected String getRTime(final long time, final String format) {
        if (null == format) {
            return Long.toString(time);
        } else {
            synchronized(m_daterel) {
                if (null == m_simpleDateFormatRel) {
                    m_simpleDateFormatRel = new java.text.SimpleDateFormat(format);
                    m_simpleDateFormatRel.setCalendar(m_cal);
                }
                m_daterel.setTime(time);
                return m_simpleDateFormatRel.format(m_daterel);
            }
        }
    }


    /**
     * Utility method to format time.
     *
     * @param time the time
     * @param format ancilliary format parameter - allowed to be null
     * @return the formatted string
     */
    protected String getTime(final long time, final String format) {
        return super.getTime(time, format);
    }

}
