1 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 28 34 public class SyslogAppender extends AppenderSkeleton { 35 39 40 final static public int LOG_KERN = 0; 41 42 final static public int LOG_USER = 1<<3; 43 44 final static public int LOG_MAIL = 2<<3; 45 46 final static public int LOG_DAEMON = 3<<3; 47 48 final static public int LOG_AUTH = 4<<3; 49 50 final static public int LOG_SYSLOG = 5<<3; 51 52 53 final static public int LOG_LPR = 6<<3; 54 55 final static public int LOG_NEWS = 7<<3; 56 57 final static public int LOG_UUCP = 8<<3; 58 59 final static public int LOG_CRON = 9<<3; 60 61 final static public int LOG_AUTHPRIV = 10<<3; 62 63 final static public int LOG_FTP = 11<<3; 64 65 67 final static public int LOG_LOCAL0 = 16<<3; 68 69 final static public int LOG_LOCAL1 = 17<<3; 70 71 final static public int LOG_LOCAL2 = 18<<3; 72 73 final static public int LOG_LOCAL3 = 19<<3; 74 75 final static public int LOG_LOCAL4 = 20<<3; 76 77 final static public int LOG_LOCAL5 = 21<<3; 78 79 final static public int LOG_LOCAL6 = 22<<3; 80 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 TAB = " "; 87 88 int syslogFacility = LOG_USER; 90 String facilityStr; 91 boolean facilityPrinting = false; 92 93 SyslogQuietWriter sqw; 95 String 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 syslogHost, int syslogFacility) { 111 this(layout, syslogFacility); 112 setSyslogHost(syslogHost); 113 } 114 115 120 synchronized 121 public 122 void close() { 123 closed = true; 124 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 147 public 148 static 149 String 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 186 public 187 static 188 int getFacility(String 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 if(sqw == null) { 245 errorHandler.error("No syslog host is set for SyslogAppedender named \""+ 246 this.name+"\"."); 247 return; 248 } 249 250 String buffer = (facilityPrinting? facilityStr : "") + 251 layout.format(event); 252 253 sqw.setLevel(event.getLevel().getSyslogEquivalent()); 254 sqw.write(buffer); 255 256 String [] 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 274 public 275 void activateOptions() { 276 } 277 278 283 public 284 boolean requiresLayout() { 285 return true; 286 } 287 288 295 public 296 void setSyslogHost(String syslogHost) { 297 this.sqw = new SyslogQuietWriter(new SyslogWriter(syslogHost), 298 syslogFacility, errorHandler); 299 this.syslogHost = syslogHost; 301 } 302 303 306 public 307 String getSyslogHost() { 308 return syslogHost; 309 } 310 311 320 public 321 void setFacility(String 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(sqw != null) { 336 sqw.setSyslogFacility(this.syslogFacility); 337 } 338 } 339 340 343 public 344 String getFacility() { 345 return getFacilityString(syslogFacility); 346 } 347 348 353 public 354 void setFacilityPrinting(boolean on) { 355 facilityPrinting = on; 356 } 357 358 361 public 362 boolean getFacilityPrinting() { 363 return facilityPrinting; 364 } 365 } 366 | Popular Tags |