1 18 19 package alt.jiapi; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 24 25 import org.apache.log4j.Appender; 26 import org.apache.log4j.Category; 27 import org.apache.log4j.ConsoleAppender; 28 import org.apache.log4j.Priority; 29 import org.apache.log4j.PatternLayout; 31 32 33 41 public class Runtime { 42 private static Map fieldValues = new HashMap (); 43 44 public static synchronized void setFieldValue(String fieldName, 45 Object value) { 46 fieldValues.put(fieldName, value); 47 } 48 49 public static synchronized Object getFieldValue(String fieldName) { 50 Object o = fieldValues.get(fieldName); 51 return o; 53 } 54 55 59 public static Category getRootLogCategory() { 60 return SystemPropertyLogConfigurator.getJiapiRoot(); 61 } 62 63 public static Category getLogCategory(Class clazz) { 64 SystemPropertyLogConfigurator.configure(); 65 return Category.getInstance(clazz); 66 } 67 } 68 69 70 73 class SystemPropertyLogConfigurator { 74 private static Category jiapiRoot = Category.getInstance("alt.jiapi"); 75 private static boolean initialized = false; 76 77 static void configure() { 78 if (initialized) { 79 return; 80 } 81 82 jiapiRoot.addAppender(getAppender()); 83 jiapiRoot.setPriority(Priority.FATAL); 84 85 if (System.getProperty("alt.jiapi.debug") != null) { 86 initialize(System.getProperty("alt.jiapi.debug"), Priority.DEBUG); 87 } 88 89 if (System.getProperty("alt.jiapi.trace") != null) { 90 initialize(System.getProperty("alt.jiapi.trace"), Priority.INFO); 91 } 92 93 initialized = true; 94 } 95 96 static Appender getAppender() { 97 ConsoleAppender ca = 98 new ConsoleAppender(new PatternLayout("[%c] %-5p : %m\n")); 99 ca.setName("ConsoleAppender"); 100 ca.setTarget("System.out"); 101 102 return ca; 103 } 104 105 static Category getJiapiRoot() { 106 return jiapiRoot; 107 } 108 109 private static void initialize(String categoryNames, Priority logLevel) { 110 if (categoryNames.trim().equals("")) { 111 jiapiRoot.setPriority(logLevel); 112 } 113 else { 114 StringTokenizer st = new StringTokenizer (categoryNames, ","); 115 while (st.hasMoreTokens()) { 116 String c = st.nextToken().trim(); 117 Category cat = Category.getInstance(c); 118 cat.setPriority(logLevel); 119 } 120 } 121 } 122 } 123 124 | Popular Tags |