1 64 package com.jcorporate.expresso.kernel; 65 66 import org.apache.log4j.Logger; 67 import org.apache.log4j.xml.DOMConfigurator; 68 69 import java.io.File ; 70 import java.io.IOException ; 71 import java.net.URL ; 72 73 80 public final class LogManager { 81 public static long lastChecked = System.currentTimeMillis(); 82 83 private static boolean initialized = false; 84 85 86 89 protected LogManager() { 90 91 } 92 93 101 public LogManager(String logConfigFile, String logDirectory) { 102 initialize(logConfigFile, logDirectory); 103 } 104 105 113 public LogManager(URL logConfigFile, String logDirectory) { 114 initialize(logConfigFile, logDirectory); 115 } 116 117 120 public static void destroy() { 121 initialized = false; 122 org.apache.log4j.LogManager.shutdown(); 123 } 124 125 126 137 synchronized private void initialize(String loggingConfiguration, String loggingDirectory) { 138 synchronized (LogManager.class) { 139 if (initialized) { 140 return; 141 } 142 143 if (loggingConfiguration == null) { 144 loggingConfiguration = System.getProperty("log4j.configuration", null); 145 } 146 147 java.net.URL configFile; 148 if (loggingConfiguration == null) { 149 configFile = this.getClass().getResource("log4j.xml"); 150 } else { 151 initLoggingDirectory(loggingDirectory); 152 java.io.File f = new java.io.File (loggingConfiguration); 153 if (f == null) { 154 throw new IllegalArgumentException ( 155 "Unable to locate logging configuration file " + loggingConfiguration); 156 } 157 DOMConfigurator.configureAndWatch(loggingConfiguration, 5000); 158 Logger log = Logger.getLogger(LogManager.class); 159 if (log.isInfoEnabled()) { 160 log.info("Logging Configuration Complete. Config File: " + 161 loggingConfiguration); 162 } 163 return; 164 } 165 166 if (configFile == null) { 167 throw new IllegalArgumentException ("Unable to locate a log4j " + 168 "configuration file, either through log.xml or loggingConfiguration parameter"); 169 } 170 171 initialize(configFile, loggingDirectory); 172 } 173 } 174 175 181 private void initLoggingDirectory(String loggingDirectory) { 182 if (loggingDirectory == null) { 183 return; 184 } 185 186 if (loggingDirectory != null && loggingDirectory.length() > 0 && 187 System.getProperty("expresso.logDir", null) == null) { 188 System.setProperty("expresso.logDir", loggingDirectory); 189 } 190 File logdir = new File (loggingDirectory); 191 if (!logdir.exists()) { 192 logdir.mkdirs(); 193 } 194 try { 195 System.out.println("Logging directory is: " + logdir.getCanonicalPath()); 196 } catch (IOException e) { 197 e.printStackTrace(); 198 } 199 200 } 201 202 protected void initialize(URL loggingConfiguration, String loggingDirectory) { 203 initLoggingDirectory(loggingDirectory); 204 205 DOMConfigurator.configure(loggingConfiguration); 206 Logger log = Logger.getLogger(LogManager.class); 207 if (log.isInfoEnabled()) { 208 log.info("Logging Configuration Complete. Using config URL: " + 209 loggingConfiguration.toString()); 210 } 211 212 213 } 214 215 } | Popular Tags |