KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > util > LogMonitorAdapter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.lf5.util;
17
18 import org.apache.log4j.lf5.LogLevel;
19 import org.apache.log4j.lf5.LogRecord;
20 import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
21
22 import java.awt.*;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * <p>LogMonitorAdapter facilitates the usage of the LogMonitor</p>
28  *
29  * @author Richard Hurst
30  */

31
32 // Contributed by ThoughtWorks Inc.
33

34 public class LogMonitorAdapter {
35   //--------------------------------------------------------------------------
36
// Constants:
37
//--------------------------------------------------------------------------
38
public static final int LOG4J_LOG_LEVELS = 0;
39   public static final int JDK14_LOG_LEVELS = 1;
40   //--------------------------------------------------------------------------
41
// Protected Variables:
42
//--------------------------------------------------------------------------
43

44   //--------------------------------------------------------------------------
45
// Private Variables:
46
//--------------------------------------------------------------------------
47
private LogBrokerMonitor _logMonitor;
48   private LogLevel _defaultLevel = null;
49
50   //--------------------------------------------------------------------------
51
// Constructors:
52
//--------------------------------------------------------------------------
53
private LogMonitorAdapter(List JavaDoc userDefinedLevels) {
54     super();
55     // set the default level to be the first entry in the list
56
_defaultLevel = (LogLevel) userDefinedLevels.get(0);
57     _logMonitor = new LogBrokerMonitor(userDefinedLevels);
58
59     _logMonitor.setFrameSize(getDefaultMonitorWidth(),
60         getDefaultMonitorHeight());
61     _logMonitor.setFontSize(12);
62     _logMonitor.show();
63   }
64   //--------------------------------------------------------------------------
65
// Public Methods:
66
//--------------------------------------------------------------------------
67
/**
68    * <p>Creates an instance of LogMonitorAdapter using the
69    * log levels inticated by the parameter. Log4J and JDK1.4 both have default
70    * LogLevels which are set but these levels can be overriden.<p>
71    *
72    * @param loglevels An integer representing either Log4J or JDK1.4 logging levels
73    * @return LogMonitorAdapter
74    */

75   public static LogMonitorAdapter newInstance(int loglevels) {
76     LogMonitorAdapter adapter;
77     if (loglevels == JDK14_LOG_LEVELS) {
78       adapter = newInstance(LogLevel.getJdk14Levels());
79       adapter.setDefaultLevel(LogLevel.FINEST);
80       adapter.setSevereLevel(LogLevel.SEVERE);
81     } else {
82       adapter = newInstance(LogLevel.getLog4JLevels());
83       adapter.setDefaultLevel(LogLevel.DEBUG);
84       adapter.setSevereLevel(LogLevel.FATAL);
85     }
86     return adapter;
87   }
88
89   /**
90    * <p>Creates an instance of LogMonitorAdapter using the specified LogLevels.
91    * The first LogLevel in the array is used as the default LogLevel unless
92    * changed using the setDefaultLevel method.<p>
93    *
94    * @param userDefined An array of user defined LogLevel objects.
95    * @return LogMonitorAdapter
96    */

97   public static LogMonitorAdapter newInstance(LogLevel[] userDefined) {
98     if (userDefined == null) {
99       return null;
100     }
101     return newInstance(Arrays.asList(userDefined));
102   }
103
104   /**
105    * <p>Creates an instance of LogMonitorAdapter using the specified LogLevels.
106    * The first LogLevel in the List is used as the default LogLevel unless
107    * changed using the setDefaultLevel method.<p>
108    *
109    * @param userDefined A list of user defined LogLevel objects.
110    * @return LogMonitorAdapter
111    */

112   public static LogMonitorAdapter newInstance(List JavaDoc userDefinedLevels) {
113     return new LogMonitorAdapter(userDefinedLevels);
114   }
115
116   /**
117    * <p>Adds a LogRecord to the LogMonitor.<p>
118    *
119    * @param record The LogRecord object to be logged in the logging monitor.
120    */

121   public void addMessage(LogRecord record) {
122     _logMonitor.addMessage(record);
123   }
124
125   /**
126    * <p>Set the maximum number of records to be displayed in the monitor<p>
127    *
128    * @param maxNumberOfRecords
129    */

130   public void setMaxNumberOfRecords(int maxNumberOfRecords) {
131     _logMonitor.setMaxNumberOfLogRecords(maxNumberOfRecords);
132   }
133
134   /**
135    * <p>Set the default log level to be used when logging messages without
136    * specifying a LogLevel.<p>
137    *
138    * @param level
139    */

140   public void setDefaultLevel(LogLevel level) {
141     _defaultLevel = level;
142   }
143
144   /**
145    * <p>Gets the default LogLevel for the Adapter.<p>
146    *
147    * @return LogLevel
148    */

149   public LogLevel getDefaultLevel() {
150     return _defaultLevel;
151   }
152
153   /**
154    * <p>Sets the Severe LogLevel.</p>
155    *
156    * @param level
157    */

158   public void setSevereLevel(LogLevel level) {
159     AdapterLogRecord.setSevereLevel(level);
160   }
161
162   /**
163    * <p>Gets the current Severe LogLevel <p>
164    *
165    * @return LogLevel
166    */

167   public LogLevel getSevereLevel() {
168     return AdapterLogRecord.getSevereLevel();
169   }
170
171   /**
172    * <p>Log a complete message to the Monitor.<p>
173    *
174    * @param category The category to be used
175    * @param level The log level to apply to the message
176    * @param message The message
177    * @param t The throwable content of the message
178    * @param NDC The NDC really only applies to Log4J and the parameter can
179    * usually be ignored.
180    */

181   public void log(String JavaDoc category, LogLevel level, String JavaDoc message,
182       Throwable JavaDoc t, String JavaDoc NDC) {
183     AdapterLogRecord record = new AdapterLogRecord();
184     record.setCategory(category);
185     record.setMessage(message);
186     record.setNDC(NDC);
187     record.setThrown(t);
188
189     if (level == null) {
190       record.setLevel(getDefaultLevel());
191     } else {
192       record.setLevel(level);
193     }
194
195     addMessage(record);
196   }
197
198   /**
199    * <p>Log a message to the Monitor and use the default LogLevel.<p>
200    *
201    * @param category The category to be used
202    * @param message The message
203    */

204   public void log(String JavaDoc category, String JavaDoc message) {
205     log(category, null, message);
206   }
207
208   /**
209    * <p>Log a message to the Monitor.<p>
210    *
211    * @param category The category to be used
212    * @param level The log level to apply to the message
213    * @param message The message
214    * @param NDC
215    */

216   public void log(String JavaDoc category, LogLevel level, String JavaDoc message, String JavaDoc NDC) {
217     log(category, level, message, null, NDC);
218   }
219
220   /**
221    * <p>Log a message to the Monitor.<p>
222    *
223    * @param category The category to be used
224    * @param level The log level to apply to the message
225    * @param message The message
226    * @param t The throwable content of the message
227    */

228   public void log(String JavaDoc category, LogLevel level, String JavaDoc message,
229       Throwable JavaDoc t) {
230     log(category, level, message, t, null);
231   }
232
233   /**
234    * <p>Log a message to the Monitor.<p>
235    *
236    * @param category The category to be used
237    * @param level The log level to apply to the message
238    * @param message The message
239    */

240   public void log(String JavaDoc category, LogLevel level, String JavaDoc message) {
241     log(category, level, message, null, null);
242   }
243
244   //--------------------------------------------------------------------------
245
// Protected Methods:
246
//--------------------------------------------------------------------------
247
/**
248    * @return the screen width from Toolkit.getScreenSize()
249    * if possible, otherwise returns 800
250    * @see java.awt.Toolkit
251    */

252   protected static int getScreenWidth() {
253     try {
254       return Toolkit.getDefaultToolkit().getScreenSize().width;
255     } catch (Throwable JavaDoc t) {
256       return 800;
257     }
258   }
259
260   /**
261    * @return the screen height from Toolkit.getScreenSize()
262    * if possible, otherwise returns 600
263    * @see java.awt.Toolkit
264    */

265   protected static int getScreenHeight() {
266     try {
267       return Toolkit.getDefaultToolkit().getScreenSize().height;
268     } catch (Throwable JavaDoc t) {
269       return 600;
270     }
271   }
272
273   protected static int getDefaultMonitorWidth() {
274     return (3 * getScreenWidth()) / 4;
275   }
276
277   protected static int getDefaultMonitorHeight() {
278     return (3 * getScreenHeight()) / 4;
279   }
280   //--------------------------------------------------------------------------
281
// Private Methods:
282
//--------------------------------------------------------------------------
283

284   //--------------------------------------------------------------------------
285
// Nested Top-Level Classes or Interfaces
286
//--------------------------------------------------------------------------
287
}
288
289
Popular Tags