Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1999-2005 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.log4j.lf5;
 
 
 import java.awt.*;

LF5Appender logs events to a swing based logging console. The swing console supports turning categories on and off, multiple detail level views, as well as full text searching and many other capabilties.

Author(s):
Brent Sprecher
 
 
 // Contributed by ThoughtWorks Inc.
 
 public class LF5Appender extends AppenderSkeleton {
   //--------------------------------------------------------------------------
   // Constants:
   //--------------------------------------------------------------------------
 
   //--------------------------------------------------------------------------
   // Protected Variables:
   //--------------------------------------------------------------------------
 
   protected LogBrokerMonitor _logMonitor;
   protected static LogBrokerMonitor _defaultLogMonitor;
   protected static AppenderFinalizer _finalizer;
 
   //--------------------------------------------------------------------------
   // Private Variables:
   //--------------------------------------------------------------------------
 
   //--------------------------------------------------------------------------
   // Constructors:
   //--------------------------------------------------------------------------
 
  
Constructs a LF5Appender using the default instance of the LogBrokerMonitor. This constructor should <bold>always </bold> be preferred over the LF5Appender(LogBrokerMonitor monitor) constructor, unless you need to spawn additional log monitoring windows.
 
   public LF5Appender() {
     this(getDefaultInstance());
   }

  
Constructs a LF5Appender using an instance of a LogBrokerMonitor supplied by the user. This constructor should only be used when you need to spawn additional log monitoring windows.

Parameters:
monitor An instance of a LogBrokerMonitor created by the user.
 
   public LF5Appender(LogBrokerMonitor monitor) {
 
     if (monitor != null) {
        = monitor;
     }
   }
 
   //--------------------------------------------------------------------------
   // Public Methods:
   //--------------------------------------------------------------------------
 
  
Appends a LoggingEvent record to the LF5Appender.

Parameters:
event The LoggingEvent to be appended.
 
   public void append(LoggingEvent event) {
     // Retrieve the information from the log4j LoggingEvent.
     String category = event.getLoggerName();
    String logMessage = event.getRenderedMessage();
    String nestedDiagnosticContext = event.getNDC();
    String threadDescription = event.getThreadName();
    String level = event.getLevel().toString();
    long time = event.timeStamp;
    LocationInfo locationInfo = event.getLocationInformation();
    // Add the logging event information to a LogRecord
    Log4JLogRecord record = new Log4JLogRecord();
    record.setCategory(category);
    record.setMessage(logMessage);
    record.setLocation(locationInfo.fullInfo);
    record.setMillis(time);
    record.setThreadDescription(threadDescription);
    if (nestedDiagnosticContext != null) {
      record.setNDC(nestedDiagnosticContext);
    } else {
      record.setNDC("");
    }
    if (event.getThrowableInformation() != null) {
      record.setThrownStackTrace(event.getThrowableInformation());
    }
    try {
      record.setLevel(LogLevel.valueOf(level));
    } catch (LogLevelFormatException e) {
      // If the priority level doesn't match one of the predefined
      // log levels, then set the level to warning.
      record.setLevel(.);
    }
    if ( != null) {
      .addMessage(record);
    }
  }

  
This method is an empty implementation of the close() method inherited from the org.apache.log4j.Appender interface.
  public void close() {
  }

  
Returns a value that indicates whether this appender requires a Layout. This method always returns false. No layout is required for the LF5Appender.
  public boolean requiresLayout() {
    return false;
  }

  
This method is used to set the property that controls whether the LogBrokerMonitor is hidden or closed when a user exits the monitor. By default, the LogBrokerMonitor will hide itself when the log window is exited, and the swing thread will continue to run in the background. If this property is set to true, the LogBrokerMonitor will call System.exit(0) and will shut down swing thread and the virtual machine.

Parameters:
callSystemExitOnClose A boolean value indicating whether to call System.exit(0) when closing the log window.
  public void setCallSystemExitOnClose(boolean callSystemExitOnClose) {
    .setCallSystemExitOnClose(callSystemExitOnClose);
  }

  
The equals method compares two LF5Appenders and determines whether they are equal. Two Appenders will be considered equal if, and only if, they both contain references to the same LogBrokerMonitor.

Parameters:
compareTo A boolean value indicating whether the two LF5Appenders are equal.
  public boolean equals(LF5Appender compareTo) {
    // If both reference the same LogBrokerMonitor, they are equal.
    return  == compareTo.getLogBrokerMonitor();
  }
    return ;
  }
  public static void main(String[] args) {
    new LF5Appender();
  }
  public void setMaxNumberOfRecords(int maxNumberOfRecords) {
    .setMaxNumberOfLogRecords(maxNumberOfRecords);
  }
  //--------------------------------------------------------------------------
  // Protected Methods:
  //--------------------------------------------------------------------------

  

Returns:
The default instance of the LogBrokerMonitor.
  protected static synchronized LogBrokerMonitor getDefaultInstance() {
    if ( == null) {
      try {
         =
            new LogBrokerMonitor(LogLevel.getLog4JLevels());
            getDefaultMonitorHeight());
        .setFontSize(12);
        .show();
      } catch (SecurityException e) {
         = null;
      }
    }
    return ;
  }

  

Returns:
the screen width from Toolkit.getScreenSize() if possible, otherwise returns 800
See also:
java.awt.Toolkit
  protected static int getScreenWidth() {
    try {
      return Toolkit.getDefaultToolkit().getScreenSize().;
    } catch (Throwable t) {
      return 800;
    }
  }

  

Returns:
the screen height from Toolkit.getScreenSize() if possible, otherwise returns 600
See also:
java.awt.Toolkit
  protected static int getScreenHeight() {
    try {
      return Toolkit.getDefaultToolkit().getScreenSize().;
    } catch (Throwable t) {
      return 600;
    }
  }
  protected static int getDefaultMonitorWidth() {
    return (3 * getScreenWidth()) / 4;
  }
  protected static int getDefaultMonitorHeight() {
    return (3 * getScreenHeight()) / 4;
  }
  //--------------------------------------------------------------------------
  // Private Methods:
  //--------------------------------------------------------------------------
  //--------------------------------------------------------------------------
  // Nested Top-Level Classes or Interfaces:
  //--------------------------------------------------------------------------
New to GrepCode? Check out our FAQ X