1 17 18 package org.apache.avalon.logging.logkit.factory.syslog; 19 20 import java.io.IOException ; 21 import java.net.InetAddress ; 22 23 import org.apache.log.LogEvent; 24 import org.apache.log.format.Formatter; 25 import org.apache.log.output.net.DatagramOutputTarget; 26 27 31 32 public class SyslogTarget extends DatagramOutputTarget 33 { 34 38 39 final static public int LOG_KERN = 0; 40 41 final static public int LOG_USER = 1<<3; 42 43 final static public int LOG_MAIL = 2<<3; 44 45 final static public int LOG_DAEMON = 3<<3; 46 47 final static public int LOG_AUTH = 4<<3; 48 49 final static public int LOG_SYSLOG = 5<<3; 50 51 52 final static public int LOG_LPR = 6<<3; 53 54 final static public int LOG_NEWS = 7<<3; 55 56 final static public int LOG_UUCP = 8<<3; 57 58 final static public int LOG_CRON = 9<<3; 59 60 final static public int LOG_AUTHPRIV = 10<<3; 61 62 final static public int LOG_FTP = 11<<3; 63 64 66 final static public int LOG_LOCAL0 = 16<<3; 67 68 final static public int LOG_LOCAL1 = 17<<3; 69 70 final static public int LOG_LOCAL2 = 18<<3; 71 72 final static public int LOG_LOCAL3 = 19<<3; 73 74 final static public int LOG_LOCAL4 = 20<<3; 75 76 final static public int LOG_LOCAL5 = 21<<3; 77 78 final static public int LOG_LOCAL6 = 22<<3; 79 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 97 public SyslogTarget( final InetAddress address, 98 final int port, 99 final Formatter formatter, 100 final int facility ) 101 throws IOException 102 { 103 super(address, port, formatter); 104 this.facility = facility; 105 } 106 107 114 public SyslogTarget( final InetAddress address, 115 final int port, 116 final Formatter formatter ) 117 throws IOException 118 { 119 super(address, port, formatter); 120 } 121 122 129 public SyslogTarget( final InetAddress address, final int port ) 130 throws IOException 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 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 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 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 |