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.chainsaw;
18  
19  import java.net.InetAddress;
20  import java.net.UnknownHostException;
21  
22  import org.apache.log4j.Appender;
23  import org.apache.log4j.AppenderSkeleton;
24  import org.apache.log4j.helpers.Constants;
25  import org.apache.log4j.helpers.OptionConverter;
26  import org.apache.log4j.spi.LoggingEvent;
27  
28  
29  /**
30   * ChainsawAppender receives LoggingEvents from the local
31   * Log4J environment, and appends them into a model that
32   * can be used inside a Swing GUI
33   * @author Paul Smith
34   * @version 1.0
35   */
36  public class ChainsawAppender
37      extends AppenderSkeleton{
38  
39    private Appender appender;
40    
41    /**
42     * The in-JVM singleton instance of the ChainsawAppender.
43     *
44     * If somehow Log4j initialises more than one, then the first one to
45     * initialise wins!
46     */
47    private static ChainsawAppender sSharedAppender = null;
48  
49    /**
50     * The classname of the viewer to create to view the events.
51     */
52    private String viewerClassname;
53    private String hostname = "localhost";
54    private String application = "app";
55  
56    /**
57     * Constructor, initialises the singleton instance of the appender
58     */
59    public ChainsawAppender() {
60      super(false);
61      synchronized (ChainsawAppender.class) {
62        if (sSharedAppender == null) {
63          sSharedAppender = this;
64        }
65      }
66    }
67  
68    /**
69     * Return the singleton instance of the ChainsawAppender, it should only
70     * be initialised once.
71     * @return the One and only instance of the ChainsawAppender that is
72     * allowed to be referenced by the GUI
73     */
74    static ChainsawAppender getInstance() {
75      return sSharedAppender;
76    }
77  
78    /**
79     * This appender does not require layout and so return false
80     * @return false and only false
81     */
82    public boolean requiresLayout() {
83      return false;
84    }
85    
86    public Appender getAppender() {
87        return appender;
88    } 
89  
90    public void setAppender(Appender appender) {
91      this.appender = appender;
92    }
93    
94    /**
95     * Appends the event
96     * @param aEvent the LoggingEvent to append
97     */
98    protected void append(LoggingEvent aEvent) {
99        if (hostname != null) {
100         aEvent.setProperty(Constants.HOSTNAME_KEY, hostname);
101       }
102 
103       if (application != null) {
104         aEvent.setProperty(Constants.APPLICATION_KEY, application);
105       }
106 
107       appender.doAppend(aEvent);
108   }
109 
110   /**
111    * Instantiates and activates an instance of a ChainsawViewer
112    * to view the contents of this appender.
113    */
114   public void activateOptions() {
115     if (viewerClassname == null) {
116       viewerClassname = "org.apache.log4j.chainsaw.DefaultViewer";
117     }
118       
119     ChainsawViewer viewer = 
120       (ChainsawViewer) OptionConverter.instantiateByClassName(viewerClassname, 
121         ChainsawViewer.class, null);
122         
123     if (viewer != null) {
124       viewer.activateViewer(this);
125     }
126     try {
127       hostname = InetAddress.getLocalHost().getHostName();
128     } catch (UnknownHostException uhe) {
129       try {
130         hostname = InetAddress.getLocalHost().getHostAddress();
131       } catch (UnknownHostException uhe2) {
132       }
133     }
134   }
135 
136   /**
137    * Close does nothing
138    */
139   public void close() {
140   }
141 
142   /**
143    * Sets the viewer class to use to view the events.  The class must
144    * implement the ChainsawViewer interface.
145    *
146    * @param classname The class name of the viewer class.
147    */
148   public void setViewerClass(String classname) {
149     viewerClassname = classname;
150   }
151 
152   /**
153    * Gets the viewer class to use to view the events.
154    *
155    * @return The class name of the viewer class.
156    */
157   public String getViewerClass() {
158     return viewerClassname;
159   }
160 
161   /**
162    * The <b>Application</b> option takes a string value which should be the
163    * name of the application getting logged
164    */
165   public void setApplication(String lapp) {
166     this.application = lapp;
167   }
168 
169   /**
170    *  Returns value of the <b>Application</b> option.
171    */
172   public String getApplication() {
173     return application;
174   }
175 
176 
177 }