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.log4j.lf5.util; 018 019import java.awt.Toolkit; 020import java.util.Arrays; 021import java.util.List; 022 023import org.apache.log4j.lf5.LogLevel; 024import org.apache.log4j.lf5.LogRecord; 025import org.apache.log4j.lf5.viewer.LogBrokerMonitor; 026 027/** 028 * <p>LogMonitorAdapter facilitates the usage of the LogMonitor</p> 029 * 030 * @author Richard Hurst 031 */ 032 033// Contributed by ThoughtWorks Inc. 034 035public class LogMonitorAdapter { 036 //-------------------------------------------------------------------------- 037 // Constants: 038 //-------------------------------------------------------------------------- 039 public static final int LOG4J_LOG_LEVELS = 0; 040 public static final int JDK14_LOG_LEVELS = 1; 041 //-------------------------------------------------------------------------- 042 // Protected Variables: 043 //-------------------------------------------------------------------------- 044 045 //-------------------------------------------------------------------------- 046 // Private Variables: 047 //-------------------------------------------------------------------------- 048 private LogBrokerMonitor _logMonitor; 049 private LogLevel _defaultLevel = null; 050 051 //-------------------------------------------------------------------------- 052 // Constructors: 053 //-------------------------------------------------------------------------- 054 private LogMonitorAdapter(List userDefinedLevels) { 055 super(); 056 // set the default level to be the first entry in the list 057 _defaultLevel = (LogLevel) userDefinedLevels.get(0); 058 _logMonitor = new LogBrokerMonitor(userDefinedLevels); 059 060 _logMonitor.setFrameSize(getDefaultMonitorWidth(), 061 getDefaultMonitorHeight()); 062 _logMonitor.setFontSize(12); 063 _logMonitor.show(); 064 } 065 //-------------------------------------------------------------------------- 066 // Public Methods: 067 //-------------------------------------------------------------------------- 068 /** 069 * <p>Creates an instance of LogMonitorAdapter using the 070 * log levels inticated by the parameter. Log4J and JDK1.4 both have default 071 * LogLevels which are set but these levels can be overriden.<p> 072 * 073 * @param loglevels An integer representing either Log4J or JDK1.4 logging levels 074 * @return LogMonitorAdapter 075 */ 076 public static LogMonitorAdapter newInstance(int loglevels) { 077 LogMonitorAdapter adapter; 078 if (loglevels == JDK14_LOG_LEVELS) { 079 adapter = newInstance(LogLevel.getJdk14Levels()); 080 adapter.setDefaultLevel(LogLevel.FINEST); 081 adapter.setSevereLevel(LogLevel.SEVERE); 082 } else { 083 adapter = newInstance(LogLevel.getLog4JLevels()); 084 adapter.setDefaultLevel(LogLevel.DEBUG); 085 adapter.setSevereLevel(LogLevel.FATAL); 086 } 087 return adapter; 088 } 089 090 /** 091 * <p>Creates an instance of LogMonitorAdapter using the specified LogLevels. 092 * The first LogLevel in the array is used as the default LogLevel unless 093 * changed using the setDefaultLevel method.<p> 094 * 095 * @param userDefined An array of user defined LogLevel objects. 096 * @return LogMonitorAdapter 097 */ 098 public static LogMonitorAdapter newInstance(LogLevel[] userDefined) { 099 if (userDefined == null) { 100 return null; 101 } 102 return newInstance(Arrays.asList(userDefined)); 103 } 104 105 /** 106 * <p>Creates an instance of LogMonitorAdapter using the specified LogLevels. 107 * The first LogLevel in the List is used as the default LogLevel unless 108 * changed using the setDefaultLevel method.<p> 109 * 110 * @param userDefinedLevels A list of user defined LogLevel objects. 111 * @return LogMonitorAdapter 112 */ 113 public static LogMonitorAdapter newInstance(List userDefinedLevels) { 114 return new LogMonitorAdapter(userDefinedLevels); 115 } 116 117 /** 118 * <p>Adds a LogRecord to the LogMonitor.<p> 119 * 120 * @param record The LogRecord object to be logged in the logging monitor. 121 */ 122 public void addMessage(LogRecord record) { 123 _logMonitor.addMessage(record); 124 } 125 126 /** 127 * <p>Set the maximum number of records to be displayed in the monitor<p> 128 * 129 * @param maxNumberOfRecords 130 */ 131 public void setMaxNumberOfRecords(int maxNumberOfRecords) { 132 _logMonitor.setMaxNumberOfLogRecords(maxNumberOfRecords); 133 } 134 135 /** 136 * <p>Set the default log level to be used when logging messages without 137 * specifying a LogLevel.<p> 138 * 139 * @param level 140 */ 141 public void setDefaultLevel(LogLevel level) { 142 _defaultLevel = level; 143 } 144 145 /** 146 * <p>Gets the default LogLevel for the Adapter.<p> 147 * 148 * @return LogLevel 149 */ 150 public LogLevel getDefaultLevel() { 151 return _defaultLevel; 152 } 153 154 /** 155 * <p>Sets the Severe LogLevel.</p> 156 * 157 * @param level 158 */ 159 public void setSevereLevel(LogLevel level) { 160 AdapterLogRecord.setSevereLevel(level); 161 } 162 163 /** 164 * <p>Gets the current Severe LogLevel <p> 165 * 166 * @return LogLevel 167 */ 168 public LogLevel getSevereLevel() { 169 return AdapterLogRecord.getSevereLevel(); 170 } 171 172 /** 173 * <p>Log a complete message to the Monitor.<p> 174 * 175 * @param category The category to be used 176 * @param level The log level to apply to the message 177 * @param message The message 178 * @param t The throwable content of the message 179 * @param NDC The NDC really only applies to Log4J and the parameter can 180 * usually be ignored. 181 */ 182 public void log(String category, LogLevel level, String message, 183 Throwable t, String NDC) { 184 AdapterLogRecord record = new AdapterLogRecord(); 185 record.setCategory(category); 186 record.setMessage(message); 187 record.setNDC(NDC); 188 record.setThrown(t); 189 190 if (level == null) { 191 record.setLevel(getDefaultLevel()); 192 } else { 193 record.setLevel(level); 194 } 195 196 addMessage(record); 197 } 198 199 /** 200 * <p>Log a message to the Monitor and use the default LogLevel.<p> 201 * 202 * @param category The category to be used 203 * @param message The message 204 */ 205 public void log(String category, String message) { 206 log(category, null, message); 207 } 208 209 /** 210 * <p>Log a message to the Monitor.<p> 211 * 212 * @param category The category to be used 213 * @param level The log level to apply to the message 214 * @param message The message 215 * @param NDC 216 */ 217 public void log(String category, LogLevel level, String message, String NDC) { 218 log(category, level, message, null, NDC); 219 } 220 221 /** 222 * <p>Log a message to the Monitor.<p> 223 * 224 * @param category The category to be used 225 * @param level The log level to apply to the message 226 * @param message The message 227 * @param t The throwable content of the message 228 */ 229 public void log(String category, LogLevel level, String message, 230 Throwable t) { 231 log(category, level, message, t, null); 232 } 233 234 /** 235 * <p>Log a message to the Monitor.<p> 236 * 237 * @param category The category to be used 238 * @param level The log level to apply to the message 239 * @param message The message 240 */ 241 public void log(String category, LogLevel level, String message) { 242 log(category, level, message, null, null); 243 } 244 245 //-------------------------------------------------------------------------- 246 // Protected Methods: 247 //-------------------------------------------------------------------------- 248 /** 249 * @return the screen width from Toolkit.getScreenSize() 250 * if possible, otherwise returns 800 251 * @see java.awt.Toolkit 252 */ 253 protected static int getScreenWidth() { 254 try { 255 return Toolkit.getDefaultToolkit().getScreenSize().width; 256 } catch (Throwable t) { 257 return 800; 258 } 259 } 260 261 /** 262 * @return the screen height from Toolkit.getScreenSize() 263 * if possible, otherwise returns 600 264 * @see java.awt.Toolkit 265 */ 266 protected static int getScreenHeight() { 267 try { 268 return Toolkit.getDefaultToolkit().getScreenSize().height; 269 } catch (Throwable t) { 270 return 600; 271 } 272 } 273 274 protected static int getDefaultMonitorWidth() { 275 return (3 * getScreenWidth()) / 4; 276 } 277 278 protected static int getDefaultMonitorHeight() { 279 return (3 * getScreenHeight()) / 4; 280 } 281 //-------------------------------------------------------------------------- 282 // Private Methods: 283 //-------------------------------------------------------------------------- 284 285 //-------------------------------------------------------------------------- 286 // Nested Top-Level Classes or Interfaces 287 //-------------------------------------------------------------------------- 288} 289