1 13 package com.tonbeller.wcf.log; 14 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 import java.io.FileOutputStream ; 18 import java.io.FilenameFilter ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.URL ; 22 import java.util.Iterator ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.TreeMap ; 27 28 import org.apache.log4j.LogManager; 29 import org.apache.log4j.Logger; 30 import org.apache.log4j.PropertyConfigurator; 31 32 import com.tonbeller.tbutils.res.Resources; 33 34 public class LogHandler { 35 36 private static Logger logger = Logger.getLogger(LogHandler.class); 37 38 public static final String DEFAULT = "default"; 39 public static final String NOLOG = "nolog"; 40 public static final String DEBUG_FILE = "debug-file"; 41 public static final String DEBUG_STDOUT = "debug-stdout"; 42 public static final String ERROR_FILE = "error-file"; 43 public static final String ERROR_STDOUT = "error-stdout"; 44 45 private Map configs = new TreeMap (); 46 private Map labels = new TreeMap (); 47 private String context; 48 private File logFile; 49 private File logDir; 50 51 public LogHandler(String logDirName, Locale locale, String context) throws IOException { 52 this.context = context; 53 this.logDir = initLogDir(logDirName); 54 this.logFile = new File (logDir, context + ".log"); 55 56 createDefConfigs(); 57 addConfigs(); 58 59 } 60 61 private void createDefConfigs() throws IOException { 62 createDefConfig(DEBUG_FILE); 63 createDefConfig(DEBUG_STDOUT); 64 createDefConfig(ERROR_FILE); 65 createDefConfig(ERROR_STDOUT); 66 createDefConfig(NOLOG); 67 } 68 69 private void createDefConfig(String name) throws IOException { 70 String resName = "/com/tonbeller/wcf/log/" + name + ".properties"; 71 72 File file = new File (logDir, name + ".properties"); 73 if (!file.exists()) 74 copyRes(resName, file); 75 } 76 77 private void addConfigs() { 78 File files[] = logDir.listFiles(new FilenameFilter () { 79 public boolean accept(File f, String name) { 80 return name.endsWith(".properties"); 81 } 82 }); 83 84 for (int i = 0; i < files.length; i++) { 85 String fname = files[i].getName(); 86 String name = fname.substring(0, fname.lastIndexOf(".properties")); 87 88 configs.put(name, files[i]); 89 } 90 } 91 92 98 public void addConfig(String resourceName, String label) throws IOException { 99 String name = resourceName; 101 int pos = name.lastIndexOf('/'); 102 if (pos >= 0) 103 name = name.substring(pos + 1); 104 File file = new File (logDir, name + ".properties"); 105 if (!file.exists()) 106 copyRes(resourceName + ".properties", file); 107 108 configs.put(name, file); 110 labels.put(name, label); 111 } 112 113 118 private File initLogDir(String dir) { 119 File baseDir = Resources.instance().getHomeDir(); 120 File dirObj = logDir(dir, baseDir); 121 if (dirObj == null) 122 dirObj = logDir(dir, new File ("java.io.tmpdir")); 123 if (dirObj == null) 124 throw new IllegalArgumentException ("Not available: " + dirObj); 125 return dirObj; 126 } 127 128 private File logDir(String dir, File baseDir) { 129 File dirObj = new File (dir); 130 if (!dirObj.isAbsolute()) 131 dirObj = new File (baseDir, dir); 132 133 if (dirObj.exists()) { 134 if (!dirObj.isDirectory()) 135 return null; 136 return dirObj; 137 } 138 139 if (dirObj.mkdirs()) 141 return dirObj; 142 return null; 143 } 144 145 private void copyRes(String resName, File file) throws IOException { 146 InputStream is = null; 147 try { 148 is = getClass().getResourceAsStream(resName); 149 if (is == null) 150 throw new IllegalArgumentException ("Resource " + resName + " not found"); 151 FileOutputStream os = null; 152 try { 153 os = new FileOutputStream (file); 154 byte[] buf = new byte[1024]; 155 int len; 156 while ((len = is.read(buf)) > 0) 157 os.write(buf, 0, len); 158 } finally { 159 if (os != null) 160 os.close(); 161 } 162 } finally { 163 if (is != null) 164 is.close(); 165 } 166 } 167 168 public String [] getConfigNames() { 169 String [] names = new String [configs.keySet().size() + 1]; 170 names[0] = DEFAULT; 171 172 Iterator it = configs.keySet().iterator(); 173 int idx = 1; 174 while (it.hasNext()) 175 names[idx++] = (String ) it.next(); 176 177 return names; 178 } 179 180 185 public String getLabel(String configName) { 186 return (String )labels.get(configName); 187 } 188 189 public void applyConfig(String name) throws IOException { 190 191 Properties logProps; 192 193 if (DEFAULT.equals(name)) 194 logProps = loadDefaultConfig(); 195 else 196 logProps = loadConfig(name); 197 198 LogManager.resetConfiguration(); 199 PropertyConfigurator.configure(logProps); 200 201 System.out.println("--- Applied new logging configuration ---"); 202 logger.error("Test error message"); 203 logger.debug("Test debug message"); 204 } 205 206 210 private Properties loadDefaultConfig() throws IOException { 211 return loadProperties(getClass().getResource("/log4j.properties")); 212 } 213 214 private Properties loadConfig(String name) throws IOException { 215 Properties logProps; 216 File file = (File ) configs.get(name); 217 if (file == null) 218 throw new IllegalArgumentException ("Log configuration " + name + " not found"); 219 220 logProps = loadProperties(file); 222 223 if (logProps.get("context") == null) 224 logProps.put("context", context); 225 if (logProps.get("logfile") == null) 226 logProps.put("logfile", logFile.getAbsolutePath()); 227 return logProps; 228 } 229 230 private Properties loadProperties(File file) throws IOException { 231 Properties props = new Properties (); 232 233 FileInputStream in = null; 234 try { 235 in = new FileInputStream (file); 236 props.load(in); 237 238 return props; 239 } finally { 240 if (in != null) 241 in.close(); 242 } 243 } 244 245 private Properties loadProperties(URL url) throws IOException { 246 Properties props = new Properties (); 247 248 InputStream in = null; 249 try { 250 in = url.openStream(); 251 props.load(in); 252 253 return props; 254 } finally { 255 if (in != null) 256 in.close(); 257 } 258 } 259 260 public String version() { 261 Package p = LogManager.class.getPackage(); 262 StringBuffer version = new StringBuffer (); 263 version.append(p.getImplementationVendor()); 264 version.append(' '); 265 version.append(p.getImplementationTitle()); 266 version.append(' '); 267 version.append(p.getImplementationVersion()); 268 269 return version.toString(); 270 } 271 272 276 public File getLogFile() { 277 return logFile; 278 } 279 280 284 public void setLogFile(File logFile) { 285 this.logFile = logFile; 286 } 287 288 292 public static String getDefault() { 293 return DEFAULT; 294 } 295 296 299 public String getRootLoggerLevel() { 300 Logger logger = Logger.getRootLogger(); 301 302 if (logger.getLevel() != null) 303 return logger.getLevel().toString(); 304 return ""; 305 } 306 } 307 | Popular Tags |