KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > format > SyslogFormatter


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.format;
18
19 import org.apache.log.LogEvent;
20 import org.apache.log.Priority;
21
22 /**
23  * A formatter that serializes in the format originally
24  * used by BSD syslog daemon.
25  *
26  * @author Peter Donald
27  */

28 public class SyslogFormatter
29     implements Formatter
30 {
31     public static final int PRIORITY_DEBUG = 7;
32     public static final int PRIORITY_INFO = 6;
33     public static final int PRIORITY_NOTICE = 5;
34     public static final int PRIORITY_WARNING = 4;
35     public static final int PRIORITY_ERR = 3;
36     public static final int PRIORITY_CRIT = 2;
37     public static final int PRIORITY_ALERT = 1;
38     public static final int PRIORITY_EMERG = 0;
39
40     /*
41      * Constants for facility.
42      */

43     public static final int FACILITY_KERN = ( 0 << 3 );
44     public static final int FACILITY_USER = ( 1 << 3 );
45     public static final int FACILITY_MAIL = ( 2 << 3 );
46     public static final int FACILITY_DAEMON = ( 3 << 3 );
47     public static final int FACILITY_AUTH = ( 4 << 3 );
48     public static final int FACILITY_SYSLOG = ( 5 << 3 );
49     public static final int FACILITY_LPR = ( 6 << 3 );
50     public static final int FACILITY_NEWS = ( 7 << 3 );
51     public static final int FACILITY_UUCP = ( 8 << 3 );
52     public static final int FACILITY_CRON = ( 9 << 3 );
53     public static final int FACILITY_AUTHPRIV = ( 10 << 3 );
54     public static final int FACILITY_FTP = ( 11 << 3 );
55
56     public static final int FACILITY_LOCAL0 = ( 16 << 3 );
57     public static final int FACILITY_LOCAL1 = ( 17 << 3 );
58     public static final int FACILITY_LOCAL2 = ( 18 << 3 );
59     public static final int FACILITY_LOCAL3 = ( 19 << 3 );
60     public static final int FACILITY_LOCAL4 = ( 20 << 3 );
61     public static final int FACILITY_LOCAL5 = ( 21 << 3 );
62     public static final int FACILITY_LOCAL6 = ( 22 << 3 );
63     public static final int FACILITY_LOCAL7 = ( 23 << 3 );
64
65     ///String descriptions of all the facilities
66
protected static final String JavaDoc[] FACILITY_DESCRIPTIONS =
67         {
68             "kern", "user", "mail", "daemon", "auth", "syslog",
69             "lpr", "news", "uucp", "cron", "authpriv", "ftp",
70             "", "", "", "", "local0", "local1", "local2", "local3",
71             "local4", "local5", "local6", "local7"
72         };
73
74     ///Constant for holding facility id
75
private int m_facility;
76
77     ///flag to decide whether we write out Facility banner
78
private boolean m_showFacilityBanner;
79
80     /**
81      * Constructor that assumes FACILITY_USER.
82      */

83     public SyslogFormatter()
84     {
85         this( FACILITY_USER );
86     }
87
88     /**
89      * Constructor so that you can associate facility with formatter.
90      *
91      * @param facility the facility constant
92      */

93     public SyslogFormatter( final int facility )
94     {
95         this( facility, true );
96     }
97
98     /**
99      * Constructor allowing setting of facility and whether to show banner.
100      *
101      * @param facility the facility code.
102      * @param showFacilityBanner true if facility banner should be shown
103      */

104     public SyslogFormatter( final int facility, final boolean showFacilityBanner )
105     {
106         m_facility = facility;
107         m_showFacilityBanner = showFacilityBanner;
108     }
109
110     /**
111      * Format log event into syslog string.
112      *
113      * @param event the event
114      * @return the formatted string
115      */

116     public String JavaDoc format( final LogEvent event )
117     {
118         final int priority = getSyslogPriority( event );
119         final int facility = getSyslogFacility( event );
120         String JavaDoc message = event.getMessage();
121
122         //TODO: Clean and spruce message here (ie remove \t and \n's)
123

124         if( null == message )
125         {
126             message = "";
127         }
128
129         if( m_showFacilityBanner )
130         {
131             message = getFacilityDescription( facility ) + ": " + message;
132         }
133
134         return "<" + ( facility | priority ) + "> " + message;
135     }
136
137     /**
138      * Retrieve description for facility.
139      *
140      * @param facility the facility code
141      * @return the facility description
142      */

143     protected String JavaDoc getFacilityDescription( final int facility )
144     {
145         return FACILITY_DESCRIPTIONS[ facility >> 3 ];
146     }
147
148     /**
149      * Get facility associated with event.
150      * Default implementation returns facility set in constructor.
151      *
152      * @param event the log event
153      * @return the facility code
154      */

155     protected int getSyslogFacility( final LogEvent event )
156     {
157         return m_facility;
158     }
159
160     /**
161      * Get syslog priority code for LogEvent.
162      * This is done by translating LogKit priority to syslog priority.
163      *
164      * @param event the log event
165      * @return the priority code
166      */

167     protected int getSyslogPriority( final LogEvent event )
168     {
169         if( event.getPriority().isLowerOrEqual( Priority.DEBUG ) )
170         {
171             return PRIORITY_DEBUG;
172         }
173         else if( event.getPriority().isLowerOrEqual( Priority.INFO ) )
174         {
175             return PRIORITY_INFO;
176         }
177         else if( event.getPriority().isLowerOrEqual( Priority.WARN ) )
178         {
179             return PRIORITY_WARNING;
180         }
181         else if( event.getPriority().isLowerOrEqual( Priority.ERROR ) )
182         {
183             return PRIORITY_ERR;
184         }
185         else
186         {
187             return PRIORITY_CRIT;
188         }
189     }
190 }
191
Popular Tags