logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "DE BENEDICTIS DAVIDE" <ddebenedic...@sogei.it>
Subject RE: Suggestions... (modifying logging messages on the fly)
Date Tue, 01 Mar 2005 13:57:52 GMT
import org.apache.log4j.spi.*;

Finally I solved with just this trivial SMSLayout.
For me it's enough.

Bye

Davide



import org.apache.log4j.*;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/**
 *
 */
public class SMSLayout extends Layout
{
  /**
   * two chars less, just in case for newline...
   */
  private static final int MAX_SMS_LEN = 158;

  private static final int HEADER_LEN = 160;

  private SimpleDateFormat df = new SimpleDateFormat
                                ("dd-MM-yyyy HH:mm:ss");

  GregorianCalendar date = new GregorianCalendar();

  /**
   *
   */
  public SMSLayout ()
  {
    date.setLenient(false);
  }

  /**
   *
   */
  public void activateOptions ()
  {
    // EMPTY
  }

  /**
   *
   * @param event
   * @return
   */
  public String format (LoggingEvent event)
  {
    StringBuffer buffer = new StringBuffer(MAX_SMS_LEN);

    date.setTimeInMillis(event.timeStamp);

    buffer.append(df.format(date.getTime()));

    buffer.append(" ");

    buffer.append(event.getLevel().toString());

    buffer.append(" [");

    buffer.append(event.getThreadName());

    buffer.append("] - ");

    String message = event.getRenderedMessage();

    if (message.length() + buffer.length() > MAX_SMS_LEN)
    {
      message = message.substring(0, (MAX_SMS_LEN
                                      - buffer.length()));
    }

    buffer.append(message);

    buffer.append(LINE_SEP);

    return buffer.toString();
  }

  /**
   *
   * @return
   */
  public boolean ignoresThrowable ()
  {
    return false;
  }


}

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message