KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > logkit > factory > syslog > SyslogTarget


1 /*
2  * Copyright 2004 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
18 package org.apache.avalon.logging.logkit.factory.syslog;
19
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22
23 import org.apache.log.LogEvent;
24 import org.apache.log.format.Formatter;
25 import org.apache.log.output.net.DatagramOutputTarget;
26
27 /**
28  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
29  * @version 1.0
30  */

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

38     /** Kernel messages */
39     final static public int LOG_KERN = 0;
40     /** Random user-level messages */
41     final static public int LOG_USER = 1<<3;
42     /** Mail system */
43     final static public int LOG_MAIL = 2<<3;
44     /** System daemons */
45     final static public int LOG_DAEMON = 3<<3;
46     /** security/authorization messages */
47     final static public int LOG_AUTH = 4<<3;
48     /** messages generated internally by syslogd */
49     final static public int LOG_SYSLOG = 5<<3;
50
51     /** line printer subsystem */
52     final static public int LOG_LPR = 6<<3;
53     /** network news subsystem */
54     final static public int LOG_NEWS = 7<<3;
55     /** UUCP subsystem */
56     final static public int LOG_UUCP = 8<<3;
57     /** clock daemon */
58     final static public int LOG_CRON = 9<<3;
59     /** security/authorization messages (private) */
60     final static public int LOG_AUTHPRIV = 10<<3;
61     /** ftp daemon */
62     final static public int LOG_FTP = 11<<3;
63
64     // other codes through 15 reserved for system use
65
/** reserved for local use */
66     final static public int LOG_LOCAL0 = 16<<3;
67     /** reserved for local use */
68     final static public int LOG_LOCAL1 = 17<<3;
69     /** reserved for local use */
70     final static public int LOG_LOCAL2 = 18<<3;
71     /** reserved for local use */
72     final static public int LOG_LOCAL3 = 19<<3;
73     /** reserved for local use */
74     final static public int LOG_LOCAL4 = 20<<3;
75     /** reserved for local use */
76     final static public int LOG_LOCAL5 = 21<<3;
77     /** reserved for local use */
78     final static public int LOG_LOCAL6 = 22<<3;
79     /** reserved for local use*/
80     final static public int LOG_LOCAL7 = 23<<3;
81
82     final static public int SYSLOG_FATAL = 0;
83     final static public int SYSLOG_ERROR = 3;
84     final static public int SYSLOG_WARN = 4;
85     final static public int SYSLOG_INFO = 6;
86     final static public int SYSLOG_DEBUG = 7;
87
88     private int facility = LOG_USER;
89
90     /**
91      * Create a output target with end point specified by address and port.
92      *
93      * @param address the address endpoint
94      * @param port the address port
95      * @exception IOException if an error occurs
96      */

97     public SyslogTarget( final InetAddress JavaDoc address,
98                                  final int port,
99                                  final Formatter formatter,
100                                  final int facility )
101         throws IOException JavaDoc
102     {
103         super(address, port, formatter);
104         this.facility = facility;
105     }
106
107     /**
108      * Create a output target with end point specified by address and port.
109      *
110      * @param address the address endpoint
111      * @param port the address port
112      * @exception IOException if an error occurs
113      */

114     public SyslogTarget( final InetAddress JavaDoc address,
115                                  final int port,
116                                  final Formatter formatter )
117         throws IOException JavaDoc
118     {
119         super(address, port, formatter);
120     }
121
122     /**
123      * Create a output target with end point specified by address and port.
124      *
125      * @param address the address endpoint
126      * @param port the address port
127      * @exception IOException if an error occurs
128      */

129     public SyslogTarget( final InetAddress JavaDoc address, final int port )
130         throws IOException JavaDoc
131     {
132         super( address, port );
133     }
134
135     protected void doProcessEvent( LogEvent event )
136     {
137         int priority = event.getPriority().getValue();
138         int syslogPriority = SYSLOG_INFO;
139
140         switch (priority) {
141             case 5 : {
142                 syslogPriority = SYSLOG_DEBUG;
143                 break;
144             }
145             case 10 : {
146                 syslogPriority = SYSLOG_INFO;
147                 break;
148             }
149             case 15 : {
150                 syslogPriority = SYSLOG_WARN;
151                 break;
152             }
153             case 20 : {
154                 syslogPriority = SYSLOG_ERROR;
155                 break;
156             }
157             case 25 : {
158                 syslogPriority = SYSLOG_FATAL;
159                 break;
160             }
161         }
162
163         //final String data = format( event );
164
if( null != getFormatter() )
165         {
166             write ("<"+(facility | syslogPriority)+">"+getFormatter().format( event ));
167         }
168         else
169         {
170             write ("<"+(facility | syslogPriority)+">"+event.toString());
171         }
172     }
173
174     public static int getFacilityValue (String JavaDoc name)
175     {
176         if( name.equalsIgnoreCase( "kern" ) )
177         {
178             return LOG_KERN;
179         }
180         else if( name.equalsIgnoreCase( "user" ) )
181         {
182             return LOG_USER;
183         }
184         else if( name.equalsIgnoreCase("mail") )
185         {
186             return LOG_MAIL;
187         }
188         else if (name.equalsIgnoreCase("daemon"))
189         {
190             return LOG_DAEMON;
191         }
192         else if (name.equalsIgnoreCase("auth"))
193         {
194             return LOG_AUTH;
195         }
196         else if (name.equalsIgnoreCase("syslog"))
197         {
198             return LOG_SYSLOG;
199         }
200         else if (name.equalsIgnoreCase("lpr"))
201         {
202             return LOG_LPR;
203         }
204         else if (name.equalsIgnoreCase("news"))
205         {
206             return LOG_NEWS;
207         }
208         else if (name.equalsIgnoreCase("uucp"))
209         {
210             return LOG_UUCP;
211         }
212         else if (name.equalsIgnoreCase("cron"))
213         {
214             return LOG_CRON;
215         }
216         else if (name.equalsIgnoreCase("authpriv"))
217         {
218             return LOG_AUTHPRIV;
219         }
220         else if (name.equalsIgnoreCase("ftp"))
221         {
222             return LOG_FTP;
223         }
224         else if (name.equalsIgnoreCase("local0"))
225         {
226             return LOG_LOCAL0;
227         }
228         else if (name.equalsIgnoreCase("local1"))
229         {
230             return LOG_LOCAL1;
231         }
232         else if (name.equalsIgnoreCase("local2"))
233         {
234             return LOG_LOCAL2;
235         }
236         else if (name.equalsIgnoreCase("local3"))
237         {
238             return LOG_LOCAL3;
239         }
240         else if (name.equalsIgnoreCase("local4"))
241         {
242             return LOG_LOCAL4;
243         }
244         else if (name.equalsIgnoreCase("local5"))
245         {
246             return LOG_LOCAL5;
247         }
248         else if (name.equalsIgnoreCase("local6"))
249         {
250             return LOG_LOCAL6;
251         }
252         else if (name.equalsIgnoreCase("local7"))
253         {
254             return LOG_LOCAL7;
255         }
256         return LOG_USER;
257     }
258
259     public void setFacility(String JavaDoc name)
260     {
261         facility = getFacilityValue(name);
262     }
263
264     public void setFacility(int value)
265     {
266         facility = value;
267     }
268
269     public int getFacility()
270     {
271         return facility;
272     }
273 }
274
Popular Tags