1 16 17 package org.springframework.util; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.net.URL ; 22 23 import org.apache.log4j.LogManager; 24 import org.apache.log4j.PropertyConfigurator; 25 import org.apache.log4j.xml.DOMConfigurator; 26 27 45 public abstract class Log4jConfigurer { 46 47 48 public static final String CLASSPATH_URL_PREFIX = "classpath:"; 49 50 51 public static final String XML_FILE_EXTENSION = ".xml"; 52 53 54 63 public static void initLogging(String location) throws FileNotFoundException { 64 String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); 65 URL url = ResourceUtils.getURL(resolvedLocation); 66 if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) { 67 DOMConfigurator.configure(url); 68 } 69 else { 70 PropertyConfigurator.configure(url); 71 } 72 } 73 74 93 public static void initLogging(String location, long refreshInterval) throws FileNotFoundException { 94 String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); 95 File file = ResourceUtils.getFile(resolvedLocation); 96 if (!file.exists()) { 97 throw new FileNotFoundException ("Log4J config file [" + resolvedLocation + "] not found"); 98 } 99 if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) { 100 DOMConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval); 101 } 102 else { 103 PropertyConfigurator.configureAndWatch(file.getAbsolutePath(), refreshInterval); 104 } 105 } 106 107 113 public static void shutdownLogging() { 114 LogManager.shutdown(); 115 } 116 117 125 public static void setWorkingDirSystemProperty(String key) { 126 System.setProperty(key, new File ("").getAbsolutePath()); 127 } 128 129 } 130 | Popular Tags |