1 6 package org.jfox.ioc.logger; 7 8 import java.io.IOException ; 9 import java.net.URL ; 10 import java.util.ArrayList ; 11 import java.util.Enumeration ; 12 import java.util.List ; 13 import java.util.Properties ; 14 15 import org.apache.log4j.Appender; 16 import org.apache.log4j.ConsoleAppender; 17 import org.apache.log4j.DailyRollingFileAppender; 18 import org.apache.log4j.FileAppender; 19 import org.apache.log4j.Layout; 20 import org.apache.log4j.Level; 21 import org.apache.log4j.LogManager; 22 import org.apache.log4j.PatternLayout; 23 import org.apache.log4j.PropertyConfigurator; 24 import org.apache.log4j.RollingFileAppender; 25 26 29 30 public class LoggerMananger { 31 private static org.apache.log4j.Logger rootLogger = LogManager.getRootLogger(); 32 private static final String configFile = "log4j.properties"; 33 34 static { 35 _init(); 36 } 37 38 static void _init() { 39 URL url = Thread.currentThread().getContextClassLoader().getResource(configFile); 40 if(url != null) { 41 PropertyConfigurator.configureAndWatch(configFile, 1000); 42 } 43 else { 44 Appender app = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)); 45 app.setName("console"); 46 rootLogger.addAppender(app); 47 } 48 rootLogger.info(LoggerMananger.class.getName() + " initialized."); 50 } 51 52 public void mx_reset() { 53 org.apache.log4j.LogManager.resetConfiguration(); 54 _init(); 55 } 56 57 public void mx_setLevel(String loggerName, String level) { 58 org.apache.log4j.Logger logger = getLogger(loggerName); 59 logger.setLevel(Level.toLevel(level)); 60 } 61 62 public String mx_getLevel(String loggerName) { 63 org.apache.log4j.Logger logger = getLogger(loggerName); 64 65 Level level = logger.getLevel(); 66 if(level == null) { 67 return null; 68 } 69 else { 70 return level.toString(); 71 } 72 } 73 74 public void mx_setPattern(String loggerName, String appenderName, String pattern) { 75 org.apache.log4j.Logger logger = getLogger(loggerName); 76 Appender appender = logger.getAppender(appenderName); 77 if(appender != null) { 78 appender.setLayout(new PatternLayout(pattern)); 79 } 80 } 81 82 public String mx_getPattern(String loggerName, String appenderName) { 83 org.apache.log4j.Logger logger = getLogger(loggerName); 84 Appender appender = logger.getAppender(appenderName); 85 if(appender != null) { 86 Layout layout = appender.getLayout(); 87 if((layout != null) && (layout instanceof PatternLayout)) { 88 return ((PatternLayout) layout).getConversionPattern(); 89 } 90 } 91 return ""; 92 } 93 94 public void mx_addFileAppender(String loggerName, String appenderName, String pattern, String filename) throws IOException { 95 org.apache.log4j.Logger logger = getLogger(loggerName); 96 FileAppender fa = new FileAppender(new PatternLayout(pattern), filename); 97 fa.setName(appenderName); 99 logger.addAppender(fa); 100 } 101 102 public void mx_addRollingFileAppender(String loggerName, String appenderName, String pattern, String filename, String maxFileSize) throws IOException { 103 org.apache.log4j.Logger logger = getLogger(loggerName); 104 RollingFileAppender rfa = new RollingFileAppender(new PatternLayout(pattern), filename); 105 rfa.setName(appenderName); 107 rfa.setMaxFileSize(maxFileSize); 108 logger.addAppender(rfa); 109 } 110 111 public void mx_addDailyRollingFileAppender(String loggerName, String appenderName, String pattern, String filename) throws IOException { 112 org.apache.log4j.Logger logger = getLogger(loggerName); 113 DailyRollingFileAppender drfa = new DailyRollingFileAppender(new PatternLayout(pattern), filename, "'.'yyyy-MM"); 114 drfa.setName(appenderName); 115 logger.addAppender(drfa); 116 } 117 118 119 public void mx_removeAppender(String loggerName, String appenderName) { 120 org.apache.log4j.Logger logger = getLogger(loggerName); 121 logger.removeAppender(appenderName); 122 } 123 124 125 public List mx_getAllApenders(String loggerName) { 126 org.apache.log4j.Logger logger = getLogger(loggerName); 127 Enumeration enu = logger.getAllAppenders(); 128 List appenders = new ArrayList (); 129 while(enu.hasMoreElements()) { 130 appenders.add(enu.nextElement()); 131 } 132 return appenders; 133 } 134 135 public Properties getOperationDescriptions() { 136 Properties prop = new Properties (); 137 prop.setProperty("mx_getPattern", " mx_getPattern(String loggerName,String appenderName), parameter example: root, console"); 138 prop.setProperty("mx_getLevel", "get level by Logger name, exsample: root"); 139 prop.setProperty("mx_setLevel", "mx_setLevel(String loggerName, String level)"); 140 prop.setProperty("mx_addFileAppender", "mx_addFileAppender(String loggerName,String appenderName, String pattern, String filename)"); 141 prop.setProperty("mx_addRollingFileAppender", "mx_addRollingFileAppender(String loggerName,String appenderName, String pattern, String filename,String maxFileSize)"); 142 prop.setProperty("mx_addDailyRollingFileAppender", "mx_addDailyRollingFileAppender(String loggerName,String appenderName, String pattern, String filename)"); 143 prop.setProperty("mx_removeAppender", "mx_removeAppender(String loggerName, String appenderName)"); 144 prop.setProperty("mx_getAllApenders", "mx_getAllApenders(String loggerName)"); 145 prop.setProperty("mx_setPattern", "mx_setPattern(String loggerName, String appenderName, String pattern)"); 146 return prop; 147 } 148 149 public Object getSource() { 150 return this; 151 } 152 153 public String getMxDescription() { 154 return "Logger Manager"; 155 } 156 157 private org.apache.log4j.Logger getLogger(String name) { 158 org.apache.log4j.Logger logger; 159 if(name.equals("root")) { 160 logger = rootLogger; 161 } 162 else { 163 logger = org.apache.log4j.Logger.getLogger(name); 164 } 165 return logger; 166 } 167 168 } 169 170 | Popular Tags |