KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > lf5 > LF5LogTarget


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

17 package org.apache.log.output.lf5;
18
19 import org.apache.log.LogEvent;
20 import org.apache.log.LogTarget;
21 import org.apache.log.format.Formatter;
22 import org.apache.log.format.PatternFormatter;
23 import org.apache.log4j.lf5.viewer.LogBrokerMonitor;
24
25 /**
26  * A {@link LogTarget} that displays log events using the
27  * <a HREF="http://jakarta.apache.org/log4j/docs/lf5/overview.html">LogFactor5</a>
28  * Swing GUI.
29  *
30  * @author <a HREF="sylvain@apache.org">Sylvain Wallez</a>
31  * @version CVS $Revision: 1.8 $ $Date: 2004/02/28 11:31:25 $
32  */

33 public class LF5LogTarget implements LogTarget
34 {
35     /** Common monitor */
36     private static LogBrokerMonitor c_defaultLogMonitor;
37
38     /** Default context map formatter */
39     private static Formatter c_defaultContextFormatter = new PatternFormatter( "" );
40
41     /** Monitor for this LogTarget */
42     private LogBrokerMonitor m_monitor;
43
44     /** Format for context maps */
45     private Formatter m_contextFormatter = c_defaultContextFormatter;
46
47     /**
48      * Create a <code>LogFactorLogTarget</code> on a given <code>LogBrokerMonitor</code>.
49      * @param monitor the monitor
50      */

51     public LF5LogTarget( final LogBrokerMonitor monitor )
52     {
53         m_monitor = monitor;
54     }
55
56     /**
57      * Create <code>LogFactorLogTarget</code> on the default <code>LogBrokerMonitor</code>.
58      */

59     public LF5LogTarget()
60     {
61         // Creation of m_monitor is deferred up to the first call to processEvent().
62
// This allows the Swing window to pop up only if this target is actually used.
63
}
64
65     /**
66      * Sets the {@link Formatter} that will be used to produce the "NDC" (nested diagnostic
67      * context) text on the GUI.
68      * @param formatter the message formatter
69      */

70     public void setNDCFormatter( final Formatter formatter )
71     {
72         m_contextFormatter = formatter;
73     }
74
75     /**
76      * Get the default <code>LogBrokerMonitor</code> instance.
77      *
78      * @return the monitor
79      */

80     public static synchronized LogBrokerMonitor getDefaultMonitor()
81     {
82         if( null == c_defaultLogMonitor )
83         {
84             c_defaultLogMonitor = new LogBrokerMonitor( LogKitLogRecord.LOGKIT_LOGLEVELS );
85             c_defaultLogMonitor.setFontSize( 12 );
86             c_defaultLogMonitor.show();
87         }
88
89         return c_defaultLogMonitor;
90     }
91
92     /**
93      * Process a log event.
94      *
95      * @param event the log event
96      */

97     public void processEvent( final LogEvent event )
98     {
99         if( null == m_monitor )
100         {
101             m_monitor = getDefaultMonitor();
102         }
103
104         m_monitor.addMessage( new LogKitLogRecord( event, m_contextFormatter ) );
105     }
106 }
107
Popular Tags