KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > net > SyslogAppender


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
17 package org.apache.log4j.net;
18
19 import org.apache.log4j.AppenderSkeleton;
20 import org.apache.log4j.spi.LoggingEvent;
21 import org.apache.log4j.Layout;
22 import org.apache.log4j.helpers.SyslogWriter;
23 import org.apache.log4j.helpers.SyslogQuietWriter;
24
25 // Contributors: Yves Bossel <ybossel@opengets.cl>
26
// Christopher Taylor <cstaylor@pacbell.net>
27

28 /**
29     Use SyslogAppender to send log messages to a remote syslog daemon.
30
31     @author Ceki G&uuml;lc&uuml;
32     @author Anders Kristensen
33  */

34 public class SyslogAppender extends AppenderSkeleton {
35   // The following constants are extracted from a syslog.h file
36
// copyrighted by the Regents of the University of California
37
// I hope nobody at Berkley gets offended.
38

39   /** Kernel messages */
40   final static public int LOG_KERN = 0;
41   /** Random user-level messages */
42   final static public int LOG_USER = 1<<3;
43   /** Mail system */
44   final static public int LOG_MAIL = 2<<3;
45   /** System daemons */
46   final static public int LOG_DAEMON = 3<<3;
47   /** security/authorization messages */
48   final static public int LOG_AUTH = 4<<3;
49   /** messages generated internally by syslogd */
50   final static public int LOG_SYSLOG = 5<<3;
51
52   /** line printer subsystem */
53   final static public int LOG_LPR = 6<<3;
54   /** network news subsystem */
55   final static public int LOG_NEWS = 7<<3;
56   /** UUCP subsystem */
57   final static public int LOG_UUCP = 8<<3;
58   /** clock daemon */
59   final static public int LOG_CRON = 9<<3;
60   /** security/authorization messages (private) */
61   final static public int LOG_AUTHPRIV = 10<<3;
62   /** ftp daemon */
63   final static public int LOG_FTP = 11<<3;
64
65   // other codes through 15 reserved for system use
66
/** reserved for local use */
67   final static public int LOG_LOCAL0 = 16<<3;
68   /** reserved for local use */
69   final static public int LOG_LOCAL1 = 17<<3;
70   /** reserved for local use */
71   final static public int LOG_LOCAL2 = 18<<3;
72   /** reserved for local use */
73   final static public int LOG_LOCAL3 = 19<<3;
74   /** reserved for local use */
75   final static public int LOG_LOCAL4 = 20<<3;
76   /** reserved for local use */
77   final static public int LOG_LOCAL5 = 21<<3;
78   /** reserved for local use */
79   final static public int LOG_LOCAL6 = 22<<3;
80   /** reserved for local use*/
81   final static public int LOG_LOCAL7 = 23<<3;
82
83   protected static final int SYSLOG_HOST_OI = 0;
84   protected static final int FACILITY_OI = 1;
85
86   static final String JavaDoc TAB = " ";
87
88   // Have LOG_USER as default
89
int syslogFacility = LOG_USER;
90   String JavaDoc facilityStr;
91   boolean facilityPrinting = false;
92
93   //SyslogTracerPrintWriter stp;
94
SyslogQuietWriter sqw;
95   String JavaDoc syslogHost;
96
97   public
98   SyslogAppender() {
99     this.initSyslogFacilityStr();
100   }
101
102   public
103   SyslogAppender(Layout layout, int syslogFacility) {
104     this.layout = layout;
105     this.syslogFacility = syslogFacility;
106     this.initSyslogFacilityStr();
107   }
108
109   public
110   SyslogAppender(Layout layout, String JavaDoc syslogHost, int syslogFacility) {
111     this(layout, syslogFacility);
112     setSyslogHost(syslogHost);
113   }
114
115   /**
116      Release any resources held by this SyslogAppender.
117
118      @since 0.8.4
119    */

120   synchronized
121   public
122   void close() {
123     closed = true;
124     // A SyslogWriter is UDP based and needs no opening. Hence, it
125
// can't be closed. We just unset the variables here.
126
sqw = null;
127   }
128
129   private
130   void initSyslogFacilityStr() {
131     facilityStr = getFacilityString(this.syslogFacility);
132
133     if (facilityStr == null) {
134       System.err.println("\"" + syslogFacility +
135                   "\" is an unknown syslog facility. Defaulting to \"USER\".");
136       this.syslogFacility = LOG_USER;
137       facilityStr = "user:";
138     } else {
139       facilityStr += ":";
140     }
141   }
142
143   /**
144      Returns the specified syslog facility as a lower-case String,
145      e.g. "kern", "user", etc.
146   */

147   public
148   static
149   String JavaDoc getFacilityString(int syslogFacility) {
150     switch(syslogFacility) {
151     case LOG_KERN: return "kern";
152     case LOG_USER: return "user";
153     case LOG_MAIL: return "mail";
154     case LOG_DAEMON: return "daemon";
155     case LOG_AUTH: return "auth";
156     case LOG_SYSLOG: return "syslog";
157     case LOG_LPR: return "lpr";
158     case LOG_NEWS: return "news";
159     case LOG_UUCP: return "uucp";
160     case LOG_CRON: return "cron";
161     case LOG_AUTHPRIV: return "authpriv";
162     case LOG_FTP: return "ftp";
163     case LOG_LOCAL0: return "local0";
164     case LOG_LOCAL1: return "local1";
165     case LOG_LOCAL2: return "local2";
166     case LOG_LOCAL3: return "local3";
167     case LOG_LOCAL4: return "local4";
168     case LOG_LOCAL5: return "local5";
169     case LOG_LOCAL6: return "local6";
170     case LOG_LOCAL7: return "local7";
171     default: return null;
172     }
173   }
174
175   /**
176      Returns the integer value corresponding to the named syslog
177      facility, or -1 if it couldn't be recognized.
178
179      @param facilityName one of the strings KERN, USER, MAIL, DAEMON,
180             AUTH, SYSLOG, LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP, LOCAL0,
181             LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7.
182             The matching is case-insensitive.
183
184      @since 1.1
185   */

186   public
187   static
188   int getFacility(String JavaDoc facilityName) {
189     if(facilityName != null) {
190       facilityName = facilityName.trim();
191     }
192     if("KERN".equalsIgnoreCase(facilityName)) {
193       return LOG_KERN;
194     } else if("USER".equalsIgnoreCase(facilityName)) {
195       return LOG_USER;
196     } else if("MAIL".equalsIgnoreCase(facilityName)) {
197       return LOG_MAIL;
198     } else if("DAEMON".equalsIgnoreCase(facilityName)) {
199       return LOG_DAEMON;
200     } else if("AUTH".equalsIgnoreCase(facilityName)) {
201       return LOG_AUTH;
202     } else if("SYSLOG".equalsIgnoreCase(facilityName)) {
203       return LOG_SYSLOG;
204     } else if("LPR".equalsIgnoreCase(facilityName)) {
205       return LOG_LPR;
206     } else if("NEWS".equalsIgnoreCase(facilityName)) {
207       return LOG_NEWS;
208     } else if("UUCP".equalsIgnoreCase(facilityName)) {
209       return LOG_UUCP;
210     } else if("CRON".equalsIgnoreCase(facilityName)) {
211       return LOG_CRON;
212     } else if("AUTHPRIV".equalsIgnoreCase(facilityName)) {
213       return LOG_AUTHPRIV;
214     } else if("FTP".equalsIgnoreCase(facilityName)) {
215       return LOG_FTP;
216     } else if("LOCAL0".equalsIgnoreCase(facilityName)) {
217       return LOG_LOCAL0;
218     } else if("LOCAL1".equalsIgnoreCase(facilityName)) {
219       return LOG_LOCAL1;
220     } else if("LOCAL2".equalsIgnoreCase(facilityName)) {
221       return LOG_LOCAL2;
222     } else if("LOCAL3".equalsIgnoreCase(facilityName)) {
223       return LOG_LOCAL3;
224     } else if("LOCAL4".equalsIgnoreCase(facilityName)) {
225       return LOG_LOCAL4;
226     } else if("LOCAL5".equalsIgnoreCase(facilityName)) {
227       return LOG_LOCAL5;
228     } else if("LOCAL6".equalsIgnoreCase(facilityName)) {
229       return LOG_LOCAL6;
230     } else if("LOCAL7".equalsIgnoreCase(facilityName)) {
231       return LOG_LOCAL7;
232     } else {
233       return -1;
234     }
235   }
236
237   public
238   void append(LoggingEvent event) {
239
240     if(!isAsSevereAsThreshold(event.getLevel()))
241       return;
242
243     // We must not attempt to append if sqw is null.
244
if(sqw == null) {
245       errorHandler.error("No syslog host is set for SyslogAppedender named \""+
246             this.name+"\".");
247       return;
248     }
249
250     String JavaDoc buffer = (facilityPrinting? facilityStr : "") +
251                           layout.format(event);
252
253     sqw.setLevel(event.getLevel().getSyslogEquivalent());
254     sqw.write(buffer);
255
256     String JavaDoc[] s = event.getThrowableStrRep();
257     if (s != null) {
258       int len = s.length;
259       if(len > 0) {
260     sqw.write(s[0]);
261
262     for(int i = 1; i < len; i++) {
263         sqw.write(TAB+s[i].substring(1));
264     }
265       }
266
267     }
268   }
269
270   /**
271      This method returns immediately as options are activated when they
272      are set.
273   */

274   public
275   void activateOptions() {
276   }
277
278   /**
279      The SyslogAppender requires a layout. Hence, this method returns
280      <code>true</code>.
281
282      @since 0.8.4 */

283   public
284   boolean requiresLayout() {
285     return true;
286   }
287
288   /**
289     The <b>SyslogHost</b> option is the name of the the syslog host
290     where log output should go.
291
292     <b>WARNING</b> If the SyslogHost is not set, then this appender
293     will fail.
294    */

295   public
296   void setSyslogHost(String JavaDoc syslogHost) {
297     this.sqw = new SyslogQuietWriter(new SyslogWriter(syslogHost),
298                      syslogFacility, errorHandler);
299     //this.stp = new SyslogTracerPrintWriter(sqw);
300
this.syslogHost = syslogHost;
301   }
302
303   /**
304      Returns the value of the <b>SyslogHost</b> option.
305    */

306   public
307   String JavaDoc getSyslogHost() {
308     return syslogHost;
309   }
310
311   /**
312      Set the syslog facility. This is the <b>Facility</b> option.
313
314      <p>The <code>facilityName</code> parameter must be one of the
315      strings KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, LPR, NEWS, UUCP,
316      CRON, AUTHPRIV, FTP, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4,
317      LOCAL5, LOCAL6, LOCAL7. Case is unimportant.
318
319      @since 0.8.1 */

320   public
321   void setFacility(String JavaDoc facilityName) {
322     if(facilityName == null)
323       return;
324
325     syslogFacility = getFacility(facilityName);
326     if (syslogFacility == -1) {
327       System.err.println("["+facilityName +
328                   "] is an unknown syslog facility. Defaulting to [USER].");
329       syslogFacility = LOG_USER;
330     }
331
332     this.initSyslogFacilityStr();
333
334     // If there is already a sqw, make it use the new facility.
335
if(sqw != null) {
336       sqw.setSyslogFacility(this.syslogFacility);
337     }
338   }
339
340   /**
341      Returns the value of the <b>Facility</b> option.
342    */

343   public
344   String JavaDoc getFacility() {
345     return getFacilityString(syslogFacility);
346   }
347
348   /**
349     If the <b>FacilityPrinting</b> option is set to true, the printed
350     message will include the facility name of the application. It is
351     <em>false</em> by default.
352    */

353   public
354   void setFacilityPrinting(boolean on) {
355     facilityPrinting = on;
356   }
357
358   /**
359      Returns the value of the <b>FacilityPrinting</b> option.
360    */

361   public
362   boolean getFacilityPrinting() {
363     return facilityPrinting;
364   }
365 }
366
Popular Tags