1 package com.protomatter.syslog; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 import java.lang.reflect.*; 57 import java.text.MessageFormat ; 58 import com.protomatter.util.*; 59 import com.protomatter.Protomatter; 60 61 257 public class Syslog 258 { 259 public static final Debug debugging = Debug.forPackage(Syslog.class); 260 261 264 public final static int DEBUG = 1; 265 266 269 public final static int INFO = 2; 270 271 275 public final static int WARNING = 4; 276 277 280 public final static int ERROR = 8; 281 282 285 public final static int FATAL = 16; 286 287 291 public final static int INHERIT_MASK = 0; 292 293 296 public final static String DEFAULT_CHANNEL = "DEFAULT_CHANNEL"; 297 298 301 public final static String ALL_CHANNEL = "*"; 302 303 308 public static int currentLogMask = atOrAbove(WARNING); 309 310 314 public static String SYSLOG_CHANNEL_NAME = "SYSLOG"; 315 316 319 public static Channel channel = Channel.getChannel(SYSLOG_CHANNEL_NAME); 320 321 private static List loggers; 323 private static Syslogger loggerArray[]; 324 325 private static Syslog instance = null; 326 327 private static Map queueMap = new HashMap(); 328 329 private static InetAddress localHost = null; 330 331 private static SyslogFlushThread flushThread = null; 332 333 private static boolean masterSwitch = true; 335 336 private static String [] defaultChannelList = new String []{DEFAULT_CHANNEL}; 337 338 private static String Q_MARK = "?"; 339 340 private static ResourceBundle resources = null; 341 342 private static boolean computeCaller = false; 343 344 private static boolean alwaysComputeCaller = false; 345 346 349 public final static String WARNING_PROPERTY = "Syslog.classloader.warning"; 350 351 352 356 protected Syslog() 357 { 358 super(); 359 } 360 361 362 370 public static Syslog getInstance() 371 { 372 return instance; 373 } 374 375 379 public static void setComputeCaller(boolean setting) 380 { 381 computeCaller = setting; 382 } 383 384 388 public static boolean getComputeCaller() 389 { 390 return computeCaller; 391 } 392 393 394 398 public static void setAlwaysComputeCaller(boolean setting) 399 { 400 alwaysComputeCaller = setting; 401 } 402 403 415 public static boolean getAlwaysComputeCaller() 416 { 417 return alwaysComputeCaller; 418 } 419 420 421 432 public static Map getWorkQueueMap() 433 { 434 return Collections.unmodifiableMap(queueMap); 435 } 436 437 438 442 static 443 { 444 checkClassLoader(); 445 instance = new Syslog(); 446 loggers = new ArrayList(); 447 loggerArray = new Syslogger[0]; 448 init(); 449 } 450 451 452 455 private static void checkClassLoader() 456 { 457 ClassLoader loader = Syslog.class.getClassLoader(); 458 String loaderString = loader.toString(); 459 ClassLoader systemLoader = ClassLoader.getSystemClassLoader(); 460 ClassLoader systemLoaderParent = systemLoader.getParent(); 461 boolean warningFlag = "off".equalsIgnoreCase(System.getProperty(WARNING_PROPERTY)); 462 463 if ((systemLoader != loader) && (systemLoaderParent != loader) && (!warningFlag)) 464 { 465 System.err.println(" /***************************************************************************\\"); 466 System.err.println(" * *"); 467 System.err.println(" * Protomatter Syslog Classloader Warning *"); 468 System.err.println(" * -------------------------------------- *"); 469 System.err.println(" * *"); 470 System.err.println(" * Protomatter Syslog has been loaded by a classloader other than *"); 471 System.err.println(" * the system classloader. This indicates that the Syslog libraries *"); 472 System.err.println(" * are not in the system CLASSPATH. Usually this is because Syslog *"); 473 System.err.println(" * is being used in an application server and the Syslog libraries *"); 474 System.err.println(" * are in the \"lib\" directory for a WebApp or EAR, etc. *"); 475 System.err.println(" * *"); 476 System.err.println(" * ClassLoader: " + StringUtil.pad(loaderString, 56) + " *"); 477 System.err.println(" * *"); 478 System.err.println(" * Syslog may not work as you might expect if it is not loaded by *"); 479 System.err.println(" * the system classloader. Under some circumstances, users may want *"); 480 System.err.println(" * to take advantage of features of this kind of configuration. If *"); 481 System.err.println(" * this is the case, you can disable this warning message by setting *"); 482 System.err.println(" * the \"Syslog.classloader.warning\" system property to \"off\". In *"); 483 System.err.println(" * most cases, though, you don't want to do that. *"); 484 System.err.println(" * *"); 485 System.err.println(" * For more information on this behavior and the logic behind it, *"); 486 System.err.println(" * please see the documentation at the following address: *"); 487 System.err.println(" * *"); 488 System.err.println(" * http://protomatter.sourceforge.net/" + 489 StringUtil.pad(Protomatter.VERSION + "/javadoc/", 32) + " *"); 490 System.err.println(" * com/protomatter/syslog/classloader-warning.html *"); 491 System.err.println(" * *"); 492 System.err.println(" \\**************************************************************************/"); 493 } 494 } 495 496 497 502 public static InetAddress getLocalHostName() 503 { 504 return localHost; 505 } 506 507 508 517 public static void setFlushThreadInterval(long interval) 518 { 519 synchronized (instance) 520 { 521 if (flushThread != null) 522 { 523 flushThread.stopRunning(); 524 flushThread = null; 525 } 526 527 if (interval > 0) 528 { 529 if (interval < 1000) 530 { 531 throw new IllegalArgumentException ( 532 MessageFormat.format(getResourceString(MessageConstants.CANNOT_SET_FLUSH_INTERVAL_MESSAGE), 533 new Object []{"1000", "0"})); 534 } 535 536 flushThread = new SyslogFlushThread(interval); 537 flushThread.start(); 538 } 539 } 540 } 541 542 543 550 public static long getFlushThreadInterval() 551 { 552 synchronized (instance) 553 { 554 if (flushThread != null) 555 { 556 return flushThread.getSleepInterval(); 557 } 558 return 0; 559 } 560 } 561 562 563 569 public static void setLocalHostName() 570 { 571 try 572 { 573 setLocalHostName(InetAddress.getLocalHost()); 574 } 575 catch (UnknownHostException x) 576 { 577 throw new IllegalStateException (getResourceString(MessageConstants.CANNOT_DETERMINE_HOSTNAME_MESSAGE)); 578 } 579 } 580 581 582 587 public static void setLocalHostName(InetAddress host) 588 { 589 localHost = host; 590 } 591 592 593 599 public static void setMasterSwitch(boolean value) 600 { 601 masterSwitch = value; 602 } 603 604 605 612 public static void addWork(String queueName, Runnable r) 613 { 614 WorkQueue queue = (WorkQueue)queueMap.get(queueName); 615 if (queue == null) 616 { 617 synchronized (queueMap) 618 { 619 queue = (WorkQueue)queueMap.get(queueName); 621 if (queue == null) 622 { 623 queue = new WorkQueue("Syslog: " + queueName, 1); 624 queueMap.put(queueName, queue); 625 } 626 } 627 } 628 queue.addWork(r); 629 } 630 631 632 644 private final static synchronized void init() 645 { 646 resources = ResourceBundle.getBundle("com.protomatter.syslog.Syslog"); 647 648 String loglevelstr = System.getProperty("Syslog.level"); 649 if (loglevelstr != null) 650 { 651 try 653 { 654 int loglevel = Integer.parseInt(loglevelstr); 655 setLogMask(atOrAbove(loglevel)); 656 } 657 catch (NumberFormatException x) 658 { 659 setLogMask(loglevelstr); 660 } 661 } 662 663 Syslogger l = new PrintWriterLog("System.err"); 665 l.setName("PrintWriterLog.err"); 666 addLogger(l); 667 668 String logfilestr = System.getProperty("Syslog.file"); 670 if (logfilestr != null) 671 { 672 FileLog fileLogger = new FileLog(new File(logfilestr)); 673 fileLogger.setName("FileLog.file"); 674 addLogger(fileLogger); 675 } 676 677 678 if (System.getProperty("Syslog.config.xml") != null) 679 { 680 try 683 { 684 Class config = Class.forName("com.protomatter.syslog.xml.SyslogXML"); 685 Method configure = config.getMethod("configure", new Class [] { File.class, String .class, Boolean.TYPE }); 686 configure.invoke(null, 687 new Object [] { new File(System.getProperty("Syslog.config.xml")), 688 System.getProperty("Syslog.xml.parser"), new Boolean (true) }); 689 } 690 catch (Exception x) 691 { 692 System.err.println(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE)); 693 x.printStackTrace(); 694 } 695 } 696 } 697 698 699 704 public final static synchronized void addLogger(Syslogger log) 705 { 706 loggers.add(log); 707 loggerArray = (Syslogger[])loggers.toArray(new Syslogger[0]); 708 } 709 710 711 717 public final static synchronized boolean removeLogger(Syslogger log) 718 { 719 if (loggers.contains(log)) 720 { 721 loggers.remove(loggers.indexOf(log)); 722 loggerArray = (Syslogger[])loggers.toArray(new Syslogger[0]); 723 return true; 724 } 725 return false; 726 } 727 728 729 732 public final static synchronized void removeAllLoggers() 733 { 734 loggers = new ArrayList(); 735 loggerArray = new Syslogger[0]; 736 } 737 738 739 744 public final static Iterator getLoggers() 745 { 746 return loggers.iterator(); 747 } 748 749 750 755 public static synchronized void shutdown() 756 { 757 masterSwitch = false; 759 760 List list = loggers; 762 removeAllLoggers(); 763 764 Iterator i = queueMap.keySet().iterator(); 766 while (i.hasNext()) 767 { 768 String queueName = (String )i.next(); 769 WorkQueue queue = (WorkQueue)queueMap.get(queueName); 770 while ((queue.getQueueLength() > 0) || (queue.getNumWorkers() > 0)) 771 { 772 try 773 { 774 Thread.yield(); 775 Thread.sleep(100); 776 } 777 catch (InterruptedException x) 778 { 779 ; } 781 } 782 } 783 784 i = list.iterator(); 786 while (i.hasNext()) 787 { 788 Syslogger l = (Syslogger)i.next(); 789 l.shutdown(); 790 } 791 } 792 793 794 802 public final static Syslogger getLogger(String name) 803 { 804 Iterator i = getLoggers(); 805 Syslogger l = null; 806 while (i.hasNext()) 807 { 808 l = (Syslogger)i.next(); 809 if (l.getName() == null && name == null) 810 { 811 return l; 812 } 813 if (l.getName() != null && name != null && l.getName().equals(name)) 814 { 815 return l; 816 } 817 } 818 return null; 819 } 820 821 822 829 public final static void setLogMask(int mask) 830 { 831 if (mask == INHERIT_MASK) 832 { 833 throw new IllegalArgumentException (MessageFormat.format( 834 getResourceString(MessageConstants.CANNOT_SET_MASK_MESSAGE), 835 new Object []{"INHERIT_MASK"})); 836 } 837 currentLogMask = mask; 838 } 839 840 841 847 public final static void setLogMask(String mask) 848 { 849 setLogMask(parseLogMask(mask)); 850 } 851 852 853 868 public final static int parseLogMask(String mask) 869 { 870 if (mask.equals("DEBUG")) 871 { 872 return Syslog.atOrAbove(Syslog.DEBUG); 873 } 874 else if (mask.equals("INFO")) 875 { 876 return Syslog.atOrAbove(Syslog.INFO); 877 } 878 else if (mask.equals("WARNING")) 879 { 880 return Syslog.atOrAbove(Syslog.WARNING); 881 } 882 else if (mask.equals("ERROR")) 883 { 884 return Syslog.atOrAbove(Syslog.ERROR); 885 } 886 else if (mask.equals("FATAL")) 887 { 888 return Syslog.atOrAbove(Syslog.FATAL); 889 } 890 else if (mask.equals("INHERIT_MASK")) 891 { 892 return Syslog.INHERIT_MASK; 893 } 894 else if (mask.startsWith("=")) 895 { 896 int newMask = 0; 897 StringTokenizer st = new StringTokenizer(mask, ", "); 898 while (st.hasMoreTokens()) 899 { 900 String elt = st.nextToken().substring(1); 901 if (elt.equals("DEBUG")) 902 { 903 newMask |= Syslog.DEBUG; 904 } 905 else if (elt.equals("INFO")) 906 { 907 newMask |= Syslog.INFO; 908 } 909 else if (elt.equals("WARNING")) 910 { 911 newMask |= Syslog.WARNING; 912 } 913 else if (elt.equals("ERROR")) 914 { 915 newMask |= Syslog.ERROR; 916 } 917 else if (elt.equals("FATAL")) 918 { 919 newMask |= Syslog.FATAL; 920 } 921 } 922 return newMask; 923 } 924 else 925 { 926 throw new IllegalArgumentException (getResourceString(MessageConstants.INVALID_MASK_MESSAGE)); 927 } 928 } 929 930 931 936 public static String getLogMaskAsString() 937 { 938 return getLogMaskAsString(currentLogMask); 939 } 940 941 942 948 public static String getLogMaskAsString(int logMask) 949 { 950 if (logMask == Syslog.INHERIT_MASK) 951 { 952 return "INHERIT_MASK"; 953 } 954 else if (logMask == Syslog.atOrAbove(Syslog.DEBUG)) 955 { 956 return "DEBUG"; 957 } 958 else if (logMask == Syslog.atOrAbove(Syslog.INFO)) 959 { 960 return "INFO"; 961 } 962 else if (logMask == Syslog.atOrAbove(Syslog.WARNING)) 963 { 964 return "WARNING"; 965 } 966 else if (logMask == Syslog.atOrAbove(Syslog.ERROR)) 967 { 968 return "ERROR"; 969 } 970 else if (logMask == Syslog.atOrAbove(Syslog.FATAL)) 971 { 972 return "FATAL"; 973 } 974 975 Vector list = new Vector(5); 977 if (inMask(Syslog.DEBUG, logMask)) 978 { 979 list.addElement("=DEBUG"); 980 } 981 if (inMask(Syslog.INFO, logMask)) 982 { 983 list.addElement("=INFO"); 984 } 985 if (inMask(Syslog.WARNING, logMask)) 986 { 987 list.addElement("=WARNING"); 988 } 989 if (inMask(Syslog.ERROR, logMask)) 990 { 991 list.addElement("=ERROR"); 992 } 993 if (inMask(Syslog.FATAL, logMask)) 994 { 995 list.addElement("=FATAL"); 996 } 997 StringBuffer b = new StringBuffer (32); 998 Enumeration e = list.elements(); 999 while (e.hasMoreElements()) 1000 { 1001 b.append(e.nextElement()); 1002 if (e.hasMoreElements()) 1003 { 1004 b.append(","); 1005 } 1006 } 1007 return b.toString(); 1008 } 1009 1010 1011 1018 public final static boolean inMask(int level, int mask) 1019 { 1020 return ((level & mask) > 0); 1021 } 1022 1023 1024 1029 public final static int getLogMask() 1030 { 1031 return currentLogMask; 1032 } 1033 1034 1035 1039 public final static int atOrAbove(int level) 1040 { 1041 if (level == DEBUG) 1042 { 1043 return DEBUG | INFO | WARNING | ERROR | FATAL; 1044 } 1045 if (level == INFO) 1046 { 1047 return INFO | WARNING | ERROR | FATAL; 1048 } 1049 if (level == WARNING) 1050 { 1051 return WARNING | ERROR | FATAL; 1052 } 1053 if (level == ERROR) 1054 { 1055 return ERROR | FATAL; 1056 } 1057 if (level == FATAL) 1058 { 1059 return FATAL; 1060 } 1061 throw new IllegalArgumentException ( 1062 MessageFormat.format(getResourceString(MessageConstants.ILLEGAL_ARGUMENT_MESSAGE), 1063 new Object []{String.valueOf(level)})); 1064 } 1065 1066 1067 1077 public final static void log(InetAddress host, Object logger, Object msg, Object detail, int level) 1078 { 1079 log(host, logger, null, msg, detail, level, 1080 Thread.currentThread(), Thread.currentThread().getName(), 1081 System.currentTimeMillis(), 1); 1082 } 1083 1084 1085 1094 public final static void log(Object logger, Object msg, Object detail, int level) 1095 { 1096 log(localHost, logger, null, msg, detail, level, 1097 Thread.currentThread(), Thread.currentThread().getName(), 1098 System.currentTimeMillis(), 1); 1099 } 1100 1101 1102 1115 public final static void log(Object logger, Object channel, 1116 Object msg, Object detail, int level) 1117 { 1118 log(localHost, logger, channel, msg, detail, level, 1119 Thread.currentThread(), Thread.currentThread().getName(), 1120 System.currentTimeMillis(), 1); 1121 } 1122 1123 1124 1138 public final static void log(InetAddress host, Object logger, Object channel, 1139 Object msg, Object detail, int level) 1140 { 1141 log(host, logger, channel, msg, detail, level, 1142 Thread.currentThread(), Thread.currentThread().getName(), 1143 System.currentTimeMillis(), 1); 1144 } 1145 1146 1147 final static String [] convertChannelObject(Object channel) 1148 { 1149 if (channel == null) 1150 { 1151 return defaultChannelList; 1152 } 1153 1154 if (channel instanceof String ) 1155 { 1156 return new String []{(String )channel}; 1157 } 1158 else if (channel instanceof String []) 1159 { 1160 return (String [])channel; 1161 } 1162 else if (channel instanceof Object []) 1163 { 1164 Object array[] = (Object [])channel; 1165 String channels[] = new String [array.length]; 1166 for (int i = 0; i < array.length; i++) 1167 { 1168 channels[i] = array[i].toString(); 1169 } 1170 return channels; 1171 } 1172 1173 return new String []{channel.toString()}; 1174 } 1175 1176 1177 1193 public final static void log(InetAddress host, Object logger, 1194 Object incomingChannel, Object msg, Object detail, int level, 1195 Thread thread, String threadName, long messageSendTime) 1196 { 1197 log(host, logger, incomingChannel, msg, detail, level, 1198 thread, threadName, messageSendTime, 1); 1199 } 1200 1201 1202 1220 public final static void log(InetAddress host, Object logger, 1221 Object incomingChannel, Object msg, Object detail, int level, 1222 Thread thread, String threadName, long messageSendTime, 1223 int traceDepth) 1224 { 1225 if (!masterSwitch) 1226 { 1227 return; 1228 } 1229 1230 String methodName = null; 1231 int lineNumber = StackTraceInfo.LINE_NUMBER_UNKNOWN; 1232 traceDepth++; 1233 1234 Class loggerClass = (logger == null) ? null : logger.getClass(); 1235 1236 String classname = null; 1238 if (!alwaysComputeCaller && !computeCaller) 1239 { 1240 if (logger == null) 1241 { 1242 classname = Q_MARK; 1243 } 1244 else 1245 { 1246 if (loggerClass == Class .class) 1247 { 1248 classname = ((Class )logger).getName(); 1249 } 1250 else if (loggerClass == String .class) 1251 { 1252 classname = (String )logger; 1253 } 1254 else 1255 { 1256 classname = logger.getClass().getName(); 1257 } 1258 } 1259 } 1260 1261 String channels[] = null; 1264 if (incomingChannel != null) 1265 { 1267 channels = convertChannelObject(incomingChannel); 1268 } 1269 else 1270 { 1272 if (loggerClass == SyslogChannelAware.class) 1275 { 1276 channels = convertChannelObject(((SyslogChannelAware)logger).getSyslogChannel()); 1277 } 1278 else 1279 { 1280 channels = defaultChannelList; 1281 } 1282 } 1283 1284 int loggerIndex = 0; 1286 int channelIndex = channels.length; 1287 Syslogger[] localLoggerArray = loggerArray; 1288 int size = localLoggerArray.length; 1289 Syslogger log = null; 1290 SyslogMessage sm = null; 1291 1292 for (; --channelIndex >= 0; ) 1293 { 1294 sm = new SyslogMessage(host, messageSendTime, 1296 channels[channelIndex], logger, 1297 classname, msg, detail, level, thread, threadName, 1298 methodName, lineNumber); 1299 for (loggerIndex = size; --loggerIndex >= 0; ) 1300 { 1301 log = localLoggerArray[loggerIndex]; 1303 try 1304 { 1305 if (alwaysComputeCaller && (sm.callingMethodName == null)) 1307 { 1308 StackTraceInfo info = StackTraceUtil.whereAmI(traceDepth); 1309 if (info != null) 1310 { 1311 sm.loggerClassname = info.className; 1312 sm.callingMethodName = info.methodName; 1313 sm.callingMethodLineNumber = info.lineNumber; 1314 } 1315 } 1316 1317 if (log.getPolicy().shouldLog(sm)) 1318 { 1319 if (computeCaller && (sm.callingMethodName == null)) 1321 { 1322 StackTraceInfo info = StackTraceUtil.whereAmI(traceDepth); 1323 if (info != null) 1324 { 1325 sm.loggerClassname = info.className; 1326 sm.callingMethodName = info.methodName; 1327 sm.callingMethodLineNumber = info.lineNumber; 1328 } 1329 } 1330 1331 log.log(sm); 1332 } 1333 } 1334 catch (Throwable t) 1335 { 1336 System.err.println(MessageFormat.format( 1337 getResourceString(MessageConstants.WRITE_PROBLEM), 1338 new Object []{((log.getName() == null) ? log.toString() : log.getName()), 1339 t.toString()})); 1340 t.printStackTrace(); 1341 } 1342 } 1343 } 1344 } 1345 1346 1347 1354 public final static void log(InetAddress host, Object logger, Throwable e) 1355 { 1356 log(host, logger, null, e.toString(), e, ERROR, 1357 Thread.currentThread(), Thread.currentThread().getName(), 1358 System.currentTimeMillis(), 1); 1359 } 1360 1361 1362 1370 public final static void log(InetAddress host, Object logger, Throwable e, int level) 1371 { 1372 log(host, logger, null, e.toString(), e, level, 1373 Thread.currentThread(), Thread.currentThread().getName(), 1374 System.currentTimeMillis(), 1); 1375 } 1376 1377 1378 1384 public final static void log(Object logger, Throwable e) 1385 { 1386 log(localHost, logger, null, e.toString(), e, ERROR, 1387 Thread.currentThread(), Thread.currentThread().getName(), 1388 System.currentTimeMillis(), 1); 1389 } 1390 1391 1392 1399 public final static void log(Object logger, Throwable e, int level) 1400 { 1401 log(localHost, logger, null, e.toString(), e, level, 1402 Thread.currentThread(), Thread.currentThread().getName(), 1403 System.currentTimeMillis(), 1); 1404 } 1405 1406 1407 1414 public final static void debug(InetAddress host, Object logger, Object message) 1415 { 1416 log(host, logger, null, message, null, DEBUG); 1417 log(host, logger, null, message, null, DEBUG, 1418 Thread.currentThread(), Thread.currentThread().getName(), 1419 System.currentTimeMillis(), 1); 1420 } 1421 1422 1423 1432 public final static void debug(InetAddress host, Object logger, Object message, Object detail) 1433 { 1434 log(host, logger, null, message, detail, DEBUG, 1435 Thread.currentThread(), Thread.currentThread().getName(), 1436 System.currentTimeMillis(), 1); 1437 } 1438 1439 1440 1447 public final static void debugToChannel(InetAddress host, Object logger, Object channel, Object message) 1448 { 1449 log(host, logger, channel, message, null, DEBUG, 1450 Thread.currentThread(), Thread.currentThread().getName(), 1451 System.currentTimeMillis(), 1); 1452 } 1453 1454 1455 1464 public final static void debugToChannel(InetAddress host, Object logger, Object channel, Object message, Object detail) 1465 { 1466 log(host, logger, channel, message, detail, DEBUG, 1467 Thread.currentThread(), Thread.currentThread().getName(), 1468 System.currentTimeMillis(), 1); 1469 } 1470 1471 1472 1478 public final static void debug(Object logger, Object message) 1479 { 1480 log(localHost, logger, null, message, null, DEBUG, 1481 Thread.currentThread(), Thread.currentThread().getName(), 1482 System.currentTimeMillis(), 1); 1483 } 1484 1485 1486 1494 public final static void debug(Object logger, Object message, Object detail) 1495 { 1496 log(localHost, logger, null, message, detail, DEBUG, 1497 Thread.currentThread(), Thread.currentThread().getName(), 1498 System.currentTimeMillis(), 1); 1499 } 1500 1501 1502 1509 public final static void debugToChannel(Object logger, Object channel, Object message) 1510 { 1511 log(localHost, logger, channel, message, null, DEBUG, 1512 Thread.currentThread(), Thread.currentThread().getName(), 1513 System.currentTimeMillis(), 1); 1514 } 1515 1516 1517 1525 public final static void debugToChannel(Object logger, Object channel, Object message, Object detail) 1526 { 1527 log(localHost, logger, channel, message, detail, DEBUG, 1528 Thread.currentThread(), Thread.currentThread().getName(), 1529 System.currentTimeMillis(), 1); 1530 } 1531 1532 1533 1534 1541 public final static void info(InetAddress host, Object logger, Object message) 1542 { 1543 log(host, logger, null, message, null, INFO, 1544 Thread.currentThread(), Thread.currentThread().getName(), 1545 System.currentTimeMillis(), 1); 1546 } 1547 1548 1549 1558 public final static void info(InetAddress host, Object logger, Object message, Object detail) 1559 { 1560 log(host, logger, null, message, detail, INFO, 1561 Thread.currentThread(), Thread.currentThread().getName(), 1562 System.currentTimeMillis(), 1); 1563 } 1564 1565 1566 1574 public final static void infoToChannel(InetAddress host, Object logger, Object channel, Object message) 1575 { 1576 log(host, logger, channel, message, null, INFO, 1577 Thread.currentThread(), Thread.currentThread().getName(), 1578 System.currentTimeMillis(), 1); 1579 } 1580 1581 1582 1591 public final static void infoToChannel(InetAddress host, Object logger, Object channel, Object message, Object detail) 1592 { 1593 log(host, logger, channel, message, detail, INFO, 1594 Thread.currentThread(), Thread.currentThread().getName(), 1595 System.currentTimeMillis(), 1); 1596 } 1597 1598 1599 1605 public final static void info(Object logger, Object message) 1606 { 1607 log(localHost, logger, null, message, null, INFO, 1608 Thread.currentThread(), Thread.currentThread().getName(), 1609 System.currentTimeMillis(), 1); 1610 } 1611 1612 1613 1621 public final static void info(Object logger, Object message, Object detail) 1622 { 1623 log(localHost, logger, null, message, detail, INFO, 1624 Thread.currentThread(), Thread.currentThread().getName(), 1625 System.currentTimeMillis(), 1); 1626 } 1627 1628 1629 1636 public final static void infoToChannel(Object logger, Object channel, Object message) 1637 { 1638 log(localHost, logger, channel, message, null, INFO, 1639 Thread.currentThread(), Thread.currentThread().getName(), 1640 System.currentTimeMillis(), 1); 1641 } 1642 1643 1644 1652 public final static void infoToChannel(Object logger, Object channel, Object message, Object detail) 1653 { 1654 log(localHost, logger, channel, message, detail, INFO, 1655 Thread.currentThread(), Thread.currentThread().getName(), 1656 System.currentTimeMillis(), 1); 1657 } 1658 1659 1660 1667 public final static void warning(InetAddress host, Object logger, Object message) 1668 { 1669 log(host, logger, null, message, null, WARNING, 1670 Thread.currentThread(), Thread.currentThread().getName(), 1671 System.currentTimeMillis(), 1); 1672 } 1673 1674 1675 1684 public final static void warning(InetAddress host, Object logger, Object message, Object detail) 1685 { 1686 log(host, logger, null, message, detail, WARNING, 1687 Thread.currentThread(), Thread.currentThread().getName(), 1688 System.currentTimeMillis(), 1); 1689 } 1690 1691 1692 1700 public final static void warningToChannel(InetAddress host, Object logger, Object channel, Object message) 1701 { 1702 log(host, logger, channel, message, null, WARNING, 1703 Thread.currentThread(), Thread.currentThread().getName(), 1704 System.currentTimeMillis(), 1); 1705 } 1706 1707 1708 1717 public final static void warningToChannel(InetAddress host, Object logger, Object channel, Object message, Object detail) 1718 { 1719 log(host, logger, channel, message, detail, WARNING, 1720 Thread.currentThread(), Thread.currentThread().getName(), 1721 System.currentTimeMillis(), 1); 1722 } 1723 1724 1725 1731 public final static void warning(Object logger, Object message) 1732 { 1733 log(localHost, logger, null, message, null, WARNING, 1734 Thread.currentThread(), Thread.currentThread().getName(), 1735 System.currentTimeMillis(), 1); 1736 } 1737 1738 1739 1747 public final static void warning(Object logger, Object message, Object detail) 1748 { 1749 log(localHost, logger, null, message, detail, WARNING, 1750 Thread.currentThread(), Thread.currentThread().getName(), 1751 System.currentTimeMillis(), 1); 1752 } 1753 1754 1755 1762 public final static void warningToChannel(Object logger, Object channel, Object message) 1763 { 1764 log(localHost, logger, channel, message, null, WARNING, 1765 Thread.currentThread(), Thread.currentThread().getName(), 1766 System.currentTimeMillis(), 1); 1767 } 1768 1769 1770 1778 public final static void warningToChannel(Object logger, Object channel, Object message, Object detail) 1779 { 1780 log(localHost, logger, channel, message, detail, WARNING, 1781 Thread.currentThread(), Thread.currentThread().getName(), 1782 System.currentTimeMillis(), 1); 1783 } 1784 1785 1786 1787 1794 public final static void error(InetAddress host, Object logger, Object message) 1795 { 1796 log(host, logger, null, message, null, ERROR, 1797 Thread.currentThread(), Thread.currentThread().getName(), 1798 System.currentTimeMillis(), 1); 1799 } 1800 1801 1802 1811 public final static void error(InetAddress host, Object logger, Object message, Object detail) 1812 { 1813 log(host, logger, null, message, detail, ERROR, 1814 Thread.currentThread(), Thread.currentThread().getName(), 1815 System.currentTimeMillis(), 1); 1816 } 1817 1818 1819 1827 public final static void errorToChannel(InetAddress host, Object logger, Object channel, Object message) 1828 { 1829 log(host, logger, channel, message, null, ERROR, 1830 Thread.currentThread(), Thread.currentThread().getName(), 1831 System.currentTimeMillis(), 1); 1832 } 1833 1834 1835 1844 public final static void errorToChannel(InetAddress host, Object logger, Object channel, Object message, Object detail) 1845 { 1846 log(host, logger, channel, message, detail, ERROR, 1847 Thread.currentThread(), Thread.currentThread().getName(), 1848 System.currentTimeMillis(), 1); 1849 } 1850 1851 1852 1858 public final static void error(Object logger, Object message) 1859 { 1860 log(localHost, logger, null, message, null, ERROR, 1861 Thread.currentThread(), Thread.currentThread().getName(), 1862 System.currentTimeMillis(), 1); 1863 } 1864 1865 1866 1874 public final static void error(Object logger, Object message, Object detail) 1875 { 1876 log(localHost, logger, null, message, detail, ERROR, 1877 Thread.currentThread(), Thread.currentThread().getName(), 1878 System.currentTimeMillis(), 1); 1879 } 1880 1881 1882 1889 public final static void errorToChannel(Object logger, Object channel, Object message) 1890 { 1891 log(localHost, logger, channel, message, null, ERROR, 1892 Thread.currentThread(), Thread.currentThread().getName(), 1893 System.currentTimeMillis(), 1); 1894 } 1895 1896 1897 1905 public final static void errorToChannel(Object logger, Object channel, Object message, Object detail) 1906 { 1907 log(localHost, logger, channel, message, detail, ERROR, 1908 Thread.currentThread(), Thread.currentThread().getName(), 1909 System.currentTimeMillis(), 1); 1910 } 1911 1912 1913 1914 1921 public final static void fatal(InetAddress host, Object logger, Object message) 1922 { 1923 log(host, logger, null, message, null, FATAL, 1924 Thread.currentThread(), Thread.currentThread().getName(), 1925 System.currentTimeMillis(), 1); 1926 } 1927 1928 1929 1938 public final static void fatal(InetAddress host, Object logger, Object message, Object detail) 1939 { 1940 log(host, logger, null, message, detail, FATAL, 1941 Thread.currentThread(), Thread.currentThread().getName(), 1942 System.currentTimeMillis(), 1); 1943 } 1944 1945 1946 1954 public final static void fatalToChannel(InetAddress host, Object logger, Object channel, Object message) 1955 { 1956 log(host, logger, channel, message, null, FATAL, 1957 Thread.currentThread(), Thread.currentThread().getName(), 1958 System.currentTimeMillis(), 1); 1959 } 1960 1961 1962 1971 public final static void fatalToChannel(InetAddress host, Object logger, Object channel, Object message, Object detail) 1972 { 1973 log(host, logger, channel, message, detail, FATAL, 1974 Thread.currentThread(), Thread.currentThread().getName(), 1975 System.currentTimeMillis(), 1); 1976 } 1977 1978 1979 1985 public final static void fatal(Object logger, Object message) 1986 { 1987 log(localHost, logger, null, message, null, FATAL, 1988 Thread.currentThread(), Thread.currentThread().getName(), 1989 System.currentTimeMillis(), 1); 1990 } 1991 1992 1993 2001 public final static void fatal(Object logger, Object message, Object detail) 2002 { 2003 log(localHost, logger, null, message, detail, FATAL, 2004 Thread.currentThread(), Thread.currentThread().getName(), 2005 System.currentTimeMillis(), 1); 2006 } 2007 2008 2009 2016 public final static void fatalToChannel(Object logger, Object channel, Object message) 2017 { 2018 log(localHost, logger, channel, message, null, FATAL, 2019 Thread.currentThread(), Thread.currentThread().getName(), 2020 System.currentTimeMillis(), 1); 2021 } 2022 2023 2024 2032 public final static void fatalToChannel(Object logger, Object channel, Object message, Object detail) 2033 { 2034 log(localHost, logger, channel, message, detail, FATAL, 2035 Thread.currentThread(), Thread.currentThread().getName(), 2036 System.currentTimeMillis(), 1); 2037 } 2038 2039 2040 2045 public final static void crumb() 2046 { 2047 StackTraceInfo trace = StackTraceUtil.whereAmI(1); 2048 log(localHost, trace.className, null, 2049 MessageFormat.format(getResourceString(MessageConstants.CRUMB_MESSAGE), 2050 new Object [] { trace }), 2051 (Object )null, DEBUG, Thread.currentThread(), Thread.currentThread().getName(), 2052 System.currentTimeMillis(), 1); 2053 } 2054 2055 2060 public final static void crumb(Object logger) 2061 { 2062 log(localHost, logger, (Object )null, 2063 MessageFormat.format(getResourceString(MessageConstants.CRUMB_MESSAGE), 2064 new Object [] { Syslog.whereAmI(1) }), 2065 (Object )null, DEBUG, Thread.currentThread(), Thread.currentThread().getName(), 2066 System.currentTimeMillis(), 1); 2067 } 2068 2069 2072 public final static void crumb(Object logger, Object channel) 2073 { 2074 log(localHost, logger, channel, 2075 MessageFormat.format(getResourceString(MessageConstants.CRUMB_MESSAGE), 2076 new Object [] { Syslog.whereAmI(1) }), 2077 null, DEBUG, Thread.currentThread(), Thread.currentThread().getName(), 2078 System.currentTimeMillis(), 1); 2079 } 2080 2081 static String whereAmI(int depth) 2082 { 2083 StackTraceInfo trace = StackTraceUtil.whereAmI(depth +1); 2084 return trace.toString(); 2085 } 2086 2087 2098 public final static boolean canLog(int level) 2099 { 2100 return inMask(level, currentLogMask); 2101 } 2102 2103 2104 2111 public final static boolean canDebug() 2112 { 2113 return canLog(DEBUG); 2114 } 2115 2116 2117 2124 public final static boolean canInfo() 2125 { 2126 return canLog(INFO); 2127 } 2128 2129 2130 2137 public final static boolean canWarning() 2138 { 2139 return canLog(WARNING); 2140 } 2141 2142 2143 2151 public final static boolean canError() 2152 { 2153 return canLog(ERROR); 2154 } 2155 2156 2157 2165 public final static boolean canFatal() 2166 { 2167 return canLog(FATAL); 2168 } 2169 2170 2171 2180 public final static boolean mightLog(Object logger, int level) 2181 { 2182 return mightLog(logger, level, null); 2183 } 2184 2185 2186 2195 public final static boolean mightDebug(Object logger) 2196 { 2197 return mightLog(logger, DEBUG, null); 2198 } 2199 2200 2201 2210 public final static boolean mightInfo(Object logger) 2211 { 2212 return mightLog(logger, INFO, null); 2213 } 2214 2215 2216 2225 public final static boolean mightWarning(Object logger) 2226 { 2227 return mightLog(logger, WARNING, null); 2228 } 2229 2230 2231 2240 public final static boolean mightError(Object logger) 2241 { 2242 return mightLog(logger, ERROR, null); 2243 } 2244 2245 2246 2255 public final static boolean mightFatal(Object logger) 2256 { 2257 return mightLog(logger, FATAL, null); 2258 } 2259 2260 2261 2271 public final static boolean mightDebug(Object logger, Object channel) 2272 { 2273 return mightLog(logger, DEBUG, channel); 2274 } 2275 2276 2277 2287 public final static boolean mightInfo(Object logger, Object channel) 2288 { 2289 return mightLog(logger, INFO, channel); 2290 } 2291 2292 2293 2303 public final static boolean mightWarning(Object logger, Object channel) 2304 { 2305 return mightLog(logger, WARNING, channel); 2306 } 2307 2308 2309 2319 public final static boolean mightError(Object logger, Object channel) 2320 { 2321 return mightLog(logger, ERROR, channel); 2322 } 2323 2324 2325 2335 public final static boolean mightFatal(Object logger, Object channel) 2336 { 2337 return mightLog(logger, FATAL, channel); 2338 } 2339 2340 2341 2352 public final static boolean mightLog(Object logger, int level, Object channel) 2353 { 2354 int theLevel = level; 2355 Object theLogger = logger; 2356 2357 if (channel == null) 2359 { 2360 if (logger instanceof SyslogChannelAware) 2361 { 2362 channel = ((SyslogChannelAware)logger).getSyslogChannel(); 2363 } 2364 else 2365 { 2366 channel = new Object []{DEFAULT_CHANNEL}; 2367 } 2368 } 2369 2370 String [] list = null; 2372 if (channel instanceof String ) 2373 { 2374 list = new String []{(String )channel}; 2375 } 2376 else if (channel instanceof String []) 2377 { 2378 list = (String [])channel; 2379 } 2380 2381 int i = loggers.size(); 2383 int j = 0; 2384 Syslogger l = null; 2385 for (; --i >= 0; ) 2386 { 2387 l = (Syslogger)loggers.get(i); 2388 for (j = list.length; --j >= 0; ) 2389 { 2390 if (l.mightLog(theLogger, level, list[j])) 2391 { 2392 return true; 2393 } } 2395 } 2396 2397 return false; 2399 } 2400 2401 2402 2411 public final static boolean canLog(Object logger, int level) 2412 { 2413 return mightLog(logger, level); 2414 } 2415 2416 2417 2425 public final static boolean canDebug(Object logger) 2426 { 2427 return canLog(logger, DEBUG); 2428 } 2429 2430 2431 2439 public final static boolean canInfo(Object logger) 2440 { 2441 return canLog(logger, INFO); 2442 } 2443 2444 2445 2453 public final static boolean canWarning(Object logger) 2454 { 2455 return canLog(logger, WARNING); 2456 } 2457 2458 2459 2467 public final static boolean canError(Object logger) 2468 { 2469 return canLog(logger, ERROR); 2470 } 2471 2472 2473 2481 public final static boolean canFatal(Object logger) 2482 { 2483 return canLog(logger, FATAL); 2484 } 2485 2486 2487 2490 public static void flush() 2491 { 2492 Iterator i = Syslog.getLoggers(); 2495 while (i.hasNext()) 2496 { 2497 Syslogger logger = (Syslogger)i.next(); 2498 try 2499 { 2500 logger.flush(); 2501 } 2502 catch (Throwable t) 2503 { 2504 System.err.println(MessageFormat.format( 2505 getResourceString(MessageConstants.FLUSH_PROBLEM_MESSAGE), 2506 new Object []{logger.getName(), t.toString()})); 2507 t.printStackTrace(); 2508 } 2509 } 2510 } 2511 2512 2513 2518 public final static ResourceBundle getResources() 2519 { 2520 return resources; 2521 } 2522 2523 2524 2530 public final static String getResourceString(String name) 2531 { 2532 return getResources().getString(name); 2533 } 2534 2535 2536 private static class SyslogFlushThread 2537 extends Thread  2538 { 2539 private long sleepInterval; 2540 private boolean stop = false; 2541 2542 2543 public SyslogFlushThread(long sleepInterval) 2544 { 2545 super(Syslog.getResourceString(MessageConstants.FLUSH_THREAD_NAME_MESSAGE)); 2546 setDaemon(true); 2547 this.sleepInterval = sleepInterval; 2548 } 2549 2550 2551 public long getSleepInterval() 2552 { 2553 return this.sleepInterval; 2554 } 2555 2556 2557 public void stopRunning() 2558 { 2559 this.stop = true; 2560 } 2561 2562 2563 public void run() 2564 { 2565 while (true && !stop) 2566 { 2567 Syslog.flush(); 2568 try 2569 { 2570 Thread.sleep(sleepInterval); 2571 } 2572 catch (InterruptedException x) 2573 { 2574 ; 2575 } 2576 } 2577 } 2578 } 2579} 2580 2581
| Popular Tags
|