alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software
 * License version 1.1, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 */
package org.apache.log4j.lf5.util;

import org.apache.log4j.lf5.LogLevel;
import org.apache.log4j.lf5.LogRecord;
import org.apache.log4j.lf5.viewer.LogBrokerMonitor;

import java.awt.*;
import java.util.Arrays;
import java.util.List;

/**
 * 

LogMonitorAdapter facilitates the usage of the LogMonitor

* * @author Richard Hurst */ // Contributed by ThoughtWorks Inc. public class LogMonitorAdapter { //-------------------------------------------------------------------------- // Constants: //-------------------------------------------------------------------------- public static final int LOG4J_LOG_LEVELS = 0; public static final int JDK14_LOG_LEVELS = 1; //-------------------------------------------------------------------------- // Protected Variables: //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- // Private Variables: //-------------------------------------------------------------------------- private LogBrokerMonitor _logMonitor; private LogLevel _defaultLevel = null; //-------------------------------------------------------------------------- // Constructors: //-------------------------------------------------------------------------- private LogMonitorAdapter(List userDefinedLevels) { super(); // set the default level to be the first entry in the list _defaultLevel = (LogLevel) userDefinedLevels.get(0); _logMonitor = new LogBrokerMonitor(userDefinedLevels); _logMonitor.setFrameSize(getDefaultMonitorWidth(), getDefaultMonitorHeight()); _logMonitor.setFontSize(12); _logMonitor.show(); } //-------------------------------------------------------------------------- // Public Methods: //-------------------------------------------------------------------------- /** *

Creates an instance of LogMonitorAdapter using the * log levels inticated by the parameter. Log4J and JDK1.4 both have default * LogLevels which are set but these levels can be overriden.

* * @param loglevels An integer representing either Log4J or JDK1.4 logging levels * @return LogMonitorAdapter */ public static LogMonitorAdapter newInstance(int loglevels) { LogMonitorAdapter adapter; if (loglevels == JDK14_LOG_LEVELS) { adapter = newInstance(LogLevel.getJdk14Levels()); adapter.setDefaultLevel(LogLevel.FINEST); adapter.setSevereLevel(LogLevel.SEVERE); } else { adapter = newInstance(LogLevel.getLog4JLevels()); adapter.setDefaultLevel(LogLevel.DEBUG); adapter.setSevereLevel(LogLevel.FATAL); } return adapter; } /** *

Creates an instance of LogMonitorAdapter using the specified LogLevels. * The first LogLevel in the array is used as the default LogLevel unless * changed using the setDefaultLevel method.

* * @param userDefined An array of user defined LogLevel objects. * @return LogMonitorAdapter */ public static LogMonitorAdapter newInstance(LogLevel[] userDefined) { if (userDefined == null) { return null; } return newInstance(Arrays.asList(userDefined)); } /** *

Creates an instance of LogMonitorAdapter using the specified LogLevels. * The first LogLevel in the List is used as the default LogLevel unless * changed using the setDefaultLevel method.

* * @param userDefined A list of user defined LogLevel objects. * @return LogMonitorAdapter */ public static LogMonitorAdapter newInstance(List userDefinedLevels) { return new LogMonitorAdapter(userDefinedLevels); } /** *

Adds a LogRecord to the LogMonitor.

* * @param record The LogRecord object to be logged in the logging monitor. */ public void addMessage(LogRecord record) { _logMonitor.addMessage(record); } /** *

Set the maximum number of records to be displayed in the monitor

* * @param maxNumberOfRecords */ public void setMaxNumberOfRecords(int maxNumberOfRecords) { _logMonitor.setMaxNumberOfLogRecords(maxNumberOfRecords); } /** *

Set the default log level to be used when logging messages without * specifying a LogLevel.

* * @param level */ public void setDefaultLevel(LogLevel level) { _defaultLevel = level; } /** *

Gets the default LogLevel for the Adapter.

* * @return LogLevel */ public LogLevel getDefaultLevel() { return _defaultLevel; } /** *

Sets the Severe LogLevel.

* * @param level */ public void setSevereLevel(LogLevel level) { AdapterLogRecord.setSevereLevel(level); } /** *

Gets the current Severe LogLevel

* * @return LogLevel */ public LogLevel getSevereLevel() { return AdapterLogRecord.getSevereLevel(); } /** *

Log a complete message to the Monitor.

* * @param category The category to be used * @param level The log level to apply to the message * @param message The message * @param t The throwable content of the message * @param NDC The NDC really only applies to Log4J and the parameter can * usually be ignored. */ public void log(String category, LogLevel level, String message, Throwable t, String NDC) { AdapterLogRecord record = new AdapterLogRecord(); record.setCategory(category); record.setMessage(message); record.setNDC(NDC); record.setThrown(t); if (level == null) { record.setLevel(getDefaultLevel()); } else { record.setLevel(level); } addMessage(record); } /** *

Log a message to the Monitor and use the default LogLevel.

* * @param category The category to be used * @param message The message */ public void log(String category, String message) { log(category, null, message); } /** *

Log a message to the Monitor.

* * @param category The category to be used * @param level The log level to apply to the message * @param message The message * @param NDC */ public void log(String category, LogLevel level, String message, String NDC) { log(category, level, message, null, NDC); } /** *

Log a message to the Monitor.

* * @param category The category to be used * @param level The log level to apply to the message * @param message The message * @param t The throwable content of the message */ public void log(String category, LogLevel level, String message, Throwable t) { log(category, level, message, t, null); } /** *

Log a message to the Monitor.

* * @param category The category to be used * @param level The log level to apply to the message * @param message The message */ public void log(String category, LogLevel level, String message) { log(category, level, message, null, null); } //-------------------------------------------------------------------------- // Protected Methods: //-------------------------------------------------------------------------- /** * @return the screen width from Toolkit.getScreenSize() * if possible, otherwise returns 800 * @see java.awt.Toolkit */ protected static int getScreenWidth() { try { return Toolkit.getDefaultToolkit().getScreenSize().width; } catch (Throwable t) { return 800; } } /** * @return the screen height from Toolkit.getScreenSize() * if possible, otherwise returns 600 * @see java.awt.Toolkit */ protected static int getScreenHeight() { try { return Toolkit.getDefaultToolkit().getScreenSize().height; } 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 //-------------------------------------------------------------------------- }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.