1 package com.protomatter.syslog; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.text.*; 56 import java.util.*; 57 58 import com.protomatter.util.*; 59 60 84 public class UNIXSyslogLog 85 extends BasicLogger 86 { 87 private static int DEFAULT_PORT = 514; 88 89 private DatagramSocket socket = null; 90 private InetAddress address = null; 91 private int port = DEFAULT_PORT; 92 private int facility = 16; private String tag = Syslog.getResourceString(MessageConstants.UNIX_DEFAULT_TAG); 94 private byte[] tagBytes = tag.getBytes(); 95 private boolean showHostname = false; 96 private String messageTemplate = "[{SEVERITY}] {CALLER}: {MESSAGE}"; 97 98 public static final String CHANNEL_TOKEN = "{CHANNEL}"; 99 public static final String FULLCALLER_TOKEN = "{CALLER-FULLNAME}"; 100 public static final String CALLER_TOKEN = "{CALLER}"; 101 public static final String MESSAGE_TOKEN = "{MESSAGE}"; 102 public static final String THREAD_TOKEN = "{THREAD}"; 103 public static final String SEVERITY_TOKEN = "{SEVERITY}"; 104 105 private DateFormat dateFormat = new SimpleDateFormat("MMM dd HH:MM:ss"); 106 private byte[] cacheDate = null; 107 private long cacheTime = 0; 108 private static long cacheTimeout = 1000; 109 110 private static int MAX_DATA_LENGTH = 1024; 112 private static char SPACE = ' '; 113 private static char COLON = ':'; 114 private static char L_ANGLE = '<'; 115 private static char R_ANGLE = '>'; 116 117 private static SimpleSyslogTextFormatter simpleFormat = null; 118 119 121 public static final int UNIX_DEBUG = 7; 122 123 124 public static final int UNIX_INFO = 6; 125 126 127 public static final int UNIX_NOTICE = 5; 128 129 130 public static final int UNIX_WARNING = 4; 131 132 133 public static final int UNIX_ERROR = 3; 134 135 136 public static final int UNIX_CRITICAL = 2; 137 138 139 public static final int UNIX_ALERT = 1; 140 141 142 public static final int UNIX_EMERGENCY = 0; 143 144 private Map severityMap = null; 146 147 150 public static Map DEFAULT_SEVERITY_MAP = null; 151 152 private int[] severityArray = new int[17]; 154 static 155 { 156 DEFAULT_SEVERITY_MAP = new HashMap(); 157 DEFAULT_SEVERITY_MAP.put( new Integer (Syslog.DEBUG), new Integer (UNIX_DEBUG) ); 158 DEFAULT_SEVERITY_MAP.put( new Integer (Syslog.INFO), new Integer (UNIX_INFO) ); 159 DEFAULT_SEVERITY_MAP.put( new Integer (Syslog.WARNING), new Integer (UNIX_WARNING) ); 160 DEFAULT_SEVERITY_MAP.put( new Integer (Syslog.ERROR), new Integer (UNIX_ERROR) ); 161 DEFAULT_SEVERITY_MAP.put( new Integer (Syslog.FATAL), new Integer (UNIX_EMERGENCY) ); 162 } 163 164 167 public UNIXSyslogLog(InetAddress address, int port) 168 throws SyslogInitException 169 { 170 this(); 171 this.address = address; 172 this.port = port; 173 } 174 175 179 public UNIXSyslogLog(InetAddress address) 180 throws SyslogInitException 181 { 182 this(address, DEFAULT_PORT); 183 } 184 185 190 public UNIXSyslogLog() 191 throws SyslogInitException 192 { 193 super(); 194 195 this.simpleFormat = new SimpleSyslogTextFormatter(); 196 this.simpleFormat.setClassWidth(0); 197 198 setSeverityMap(DEFAULT_SEVERITY_MAP); 199 200 try 201 { 202 socket = new DatagramSocket(); 203 } 204 catch (SocketException x) 205 { 206 throw new SyslogInitException(Syslog.getResourceString(MessageConstants.UNIX_CANNOT_INIT_SOCKET), x); 207 } 208 } 209 210 230 public void setMessageTemplate(String template) 231 { 232 this.messageTemplate = template; 233 } 234 235 238 public String getMessageTemplate() 239 { 240 return this.messageTemplate; 241 } 242 243 252 public void setSeverityMap(Map map) 253 { 254 this.severityMap = map; 255 256 try 259 { 260 severityArray[Syslog.DEBUG] = ((Integer )map.get(new Integer (Syslog.DEBUG))).intValue(); 261 severityArray[Syslog.INFO] = ((Integer )map.get(new Integer (Syslog.INFO))).intValue(); 262 severityArray[Syslog.WARNING] = ((Integer )map.get(new Integer (Syslog.WARNING))).intValue(); 263 severityArray[Syslog.ERROR] = ((Integer )map.get(new Integer (Syslog.ERROR))).intValue(); 264 severityArray[Syslog.FATAL] = ((Integer )map.get(new Integer (Syslog.FATAL))).intValue(); 265 } 266 catch (NullPointerException x) 267 { 268 throw new IllegalArgumentException (Syslog.getResourceString(MessageConstants.UNIX_BAD_SEVERITY_MAP)); 269 } 270 } 271 272 275 public Map getSeverityMap() 276 { 277 return this.severityMap; 278 } 279 280 283 public String getTag() 284 { 285 return this.tag; 286 } 287 288 291 public void setTag(String tag) 292 { 293 this.tag = tag; 294 this.tagBytes = tag.getBytes(); 295 } 296 297 303 public boolean getShowHostname() 304 { 305 return this.showHostname; 306 } 307 308 311 public void setShowHostname(boolean showHostname) 312 { 313 this.showHostname = showHostname;; 314 } 315 316 355 public void setFacility(int facility) 356 { 357 this.facility = facility; 358 } 359 360 363 public int getFacility() 364 { 365 return this.facility; 366 } 367 368 371 public void setLogServer(InetAddress address) 372 { 373 this.address = address; 374 } 375 376 379 public InetAddress getLogServer() 380 { 381 return this.address; 382 } 383 384 387 public void setPort(int port) 388 { 389 this.port = port; 390 } 391 392 395 public int getPort() 396 { 397 return this.port; 398 } 399 400 private int translateMessageSeverity(int syslogSeverity) 401 { 402 return severityArray[syslogSeverity]; 403 } 404 405 408 public final void log(SyslogMessage message) 409 { 410 try 411 { 412 int severity = severityArray[message.level]; 414 415 ByteArrayOutputStream os = new ByteArrayOutputStream(256); 416 417 os.write(L_ANGLE); 419 os.write(String.valueOf(facility).getBytes()); 420 os.write(String.valueOf((8*facility) + severity).getBytes()); 421 os.write(R_ANGLE); 422 423 byte timestamp[] = null; 425 long now = System.currentTimeMillis(); 426 if ((now - cacheTime) > cacheTimeout) 427 { 428 timestamp = dateFormat.format(new Date(message.time)).getBytes(); 429 cacheDate = timestamp; 430 cacheTime = now; 431 } 432 else 433 { 434 timestamp = cacheDate; 435 } 436 os.write(timestamp); 437 os.write(SPACE); 438 439 if (this.showHostname) 442 { 443 os.write(simpleFormat.getHostname(Syslog.getLocalHostName()).getBytes()); 444 os.write(SPACE); 445 } 446 447 os.write(this.tagBytes); 451 os.write(COLON); 452 os.write(SPACE); 453 454 String body = messageTemplate; 455 456 body = StringUtil.replace(body, CHANNEL_TOKEN, message.channel); 457 body = StringUtil.replace(body, FULLCALLER_TOKEN, message.loggerClassname); 458 body = StringUtil.replace(body, CALLER_TOKEN, trimFromLastPeriod(message.loggerClassname)); 459 body = StringUtil.replace(body, MESSAGE_TOKEN, String.valueOf(message.msg)); 460 body = StringUtil.replace(body, THREAD_TOKEN, message.threadName); 461 body = StringUtil.replace(body, SEVERITY_TOKEN, getSeverityName(message.level)); 462 463 os.write(body.getBytes()); 464 465 byte[] data = os.toByteArray(); 467 DatagramPacket packet = new DatagramPacket(data, 468 ((data.length > MAX_DATA_LENGTH) ? MAX_DATA_LENGTH : data.length), 469 address, port); 470 471 socket.send(packet); 473 } 474 catch (IOException x) 475 { 476 System.err.println(MessageFormat.format( 477 Syslog.getResourceString(MessageConstants.UNIX_CANNOT_WRITE_MESSAGE), 478 new Object [] { x.toString() })); 479 x.printStackTrace(); 480 } 481 } 482 483 protected String getSeverityName(int syslogSeverity) 484 { 485 switch (syslogSeverity) 486 { 487 case Syslog.DEBUG: return "DEBUG"; 488 case Syslog.INFO: return "INFO"; 489 case Syslog.WARNING: return "WARNING"; 490 case Syslog.ERROR: return "ERROR"; 491 case Syslog.FATAL: return "FATAL"; 492 } 493 return "UNKNOWN"; 494 } 495 496 private static final String trimFromLastPeriod(String s) 497 { 498 if (s == null || s.length() == 0) 499 return ""; 500 int index = s.lastIndexOf("."); 501 if (index == -1) 502 return s; 503 return s.substring(index +1); 504 } 505 506 public synchronized void shutdown() 507 { 508 } 510 511 public void flush() 512 { 513 } 515 } 516 | Popular Tags |