View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.log4j.lf5.util;
18  
19  import java.awt.Toolkit;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.apache.log4j.lf5.LogLevel;
24  import org.apache.log4j.lf5.LogRecord;
25  import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
26  
27  /**
28   * <p>LogMonitorAdapter facilitates the usage of the LogMonitor</p>
29   *
30   * @author Richard Hurst
31   */
32  
33  // Contributed by ThoughtWorks Inc.
34  
35  public class LogMonitorAdapter {
36    //--------------------------------------------------------------------------
37    //   Constants:
38    //--------------------------------------------------------------------------
39    public static final int LOG4J_LOG_LEVELS = 0;
40    public static final int JDK14_LOG_LEVELS = 1;
41    //--------------------------------------------------------------------------
42    //   Protected Variables:
43    //--------------------------------------------------------------------------
44  
45    //--------------------------------------------------------------------------
46    //   Private Variables:
47    //--------------------------------------------------------------------------
48    private LogBrokerMonitor _logMonitor;
49    private LogLevel _defaultLevel = null;
50  
51    //--------------------------------------------------------------------------
52    //   Constructors:
53    //--------------------------------------------------------------------------
54    private LogMonitorAdapter(List userDefinedLevels) {
55      super();
56      // set the default level to be the first entry in the list
57      _defaultLevel = (LogLevel) userDefinedLevels.get(0);
58      _logMonitor = new LogBrokerMonitor(userDefinedLevels);
59  
60      _logMonitor.setFrameSize(getDefaultMonitorWidth(),
61          getDefaultMonitorHeight());
62      _logMonitor.setFontSize(12);
63      _logMonitor.show();
64    }
65    //--------------------------------------------------------------------------
66    //   Public Methods:
67    //--------------------------------------------------------------------------
68    /**
69     * <p>Creates an instance of LogMonitorAdapter using the
70     * log levels inticated by the parameter. Log4J and JDK1.4 both have default
71     * LogLevels which are set but these levels can be overriden.<p>
72     *
73     * @param loglevels An integer representing either Log4J or JDK1.4 logging levels
74     * @return LogMonitorAdapter
75     */
76    public static LogMonitorAdapter newInstance(int loglevels) {
77      LogMonitorAdapter adapter;
78      if (loglevels == JDK14_LOG_LEVELS) {
79        adapter = newInstance(LogLevel.getJdk14Levels());
80        adapter.setDefaultLevel(LogLevel.FINEST);
81        adapter.setSevereLevel(LogLevel.SEVERE);
82      } else {
83        adapter = newInstance(LogLevel.getLog4JLevels());
84        adapter.setDefaultLevel(LogLevel.DEBUG);
85        adapter.setSevereLevel(LogLevel.FATAL);
86      }
87      return adapter;
88    }
89  
90    /**
91     * <p>Creates an instance of LogMonitorAdapter using the specified LogLevels.
92     * The first LogLevel in the array is used as the default LogLevel unless
93     * changed using the setDefaultLevel method.<p>
94     *
95     * @param userDefined An array of user defined LogLevel objects.
96     * @return LogMonitorAdapter
97     */
98    public static LogMonitorAdapter newInstance(LogLevel[] userDefined) {
99      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