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