1 21 22 package org.opensubsystems.core.util; 23 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.URL ; 29 import java.util.logging.LogManager ; 30 import java.util.logging.Logger ; 31 32 42 public class Log 43 { 44 46 49 public static final String DEFAULT_LOGGER_NAME = "org.opensubsystems.defaultlog"; 50 51 54 public static final String DEFAULT_LOGCONFIG_FILE_NAME = "osslog.properties"; 55 56 58 61 protected static transient Logger s_lgLogger = null; 62 63 66 protected static transient String s_strMutex = "log.mutex"; 67 68 71 protected static String s_strConfigFile; 72 73 77 protected static Logger s_logger; 78 79 81 84 static 85 { 86 InputStream isConfigFile = null; 87 88 try 89 { 90 String strLogfilename = ""; 92 93 strLogfilename = System.getProperty("java.util.logging.config.file"); 94 try 95 { 96 if ((strLogfilename != null) && (strLogfilename.length() > 0)) 101 { 102 isConfigFile = new FileInputStream (strLogfilename); 103 s_strConfigFile = strLogfilename; 104 } 105 } 106 catch (FileNotFoundException fnfExc1) 107 { 108 isConfigFile = findConfigFile(strLogfilename); 110 111 if (isConfigFile == null) 112 { 113 System.out.println("File " + strLogfilename 115 + " set in java.util.logging.config.file doesn't exist"); 116 } 117 } 118 if (isConfigFile == null) 119 { 120 try 121 { 122 isConfigFile = new FileInputStream (DEFAULT_LOGCONFIG_FILE_NAME); 124 s_strConfigFile = DEFAULT_LOGCONFIG_FILE_NAME; 125 } 126 catch (FileNotFoundException fnfExc2) 127 { 128 isConfigFile = findConfigFile(DEFAULT_LOGCONFIG_FILE_NAME); 130 131 if (isConfigFile == null) 132 { 133 System.out.println("Default log configuration file " 135 + DEFAULT_LOGCONFIG_FILE_NAME 136 + " doesn't exist"); 137 } 138 } 139 } 140 if (isConfigFile != null) 141 { 142 LogManager.getLogManager().readConfiguration(isConfigFile); 143 } 144 } 145 catch (IOException ioeExc) 146 { 147 System.out.println("Cannot initialize logging subsystem."); 149 ioeExc.printStackTrace(); 150 } 151 finally 152 { 153 try 154 { 155 if (isConfigFile != null) 156 { 157 isConfigFile.close(); 158 } 159 } 160 catch (IOException ioeExc) 161 { 162 } 164 finally 165 { 166 s_logger = Log.getInstance(Log.class); 167 s_logger.info("Using log configuration file " + s_strConfigFile); 170 } 171 } 172 } 173 174 177 protected Log( 178 ) 179 { 180 } 181 182 184 191 public static Logger getInstance( 192 ) 193 { 194 if (s_lgLogger == null) 195 { 196 synchronized (s_strMutex) 197 { 198 s_lgLogger = getInstance(DEFAULT_LOGGER_NAME); 203 } 204 } 205 206 return s_lgLogger; 207 } 208 209 216 public static Logger getInstance( 217 Class classIdentifier 218 ) 219 { 220 return getInstance(classIdentifier.getName()); 221 } 222 223 230 public static Logger getInstance( 231 String logIdentifier 232 ) 233 { 234 return Logger.getLogger(logIdentifier); 239 } 240 241 249 protected static InputStream findConfigFile( 250 String strConfigFileName 251 ) throws IOException , 252 FileNotFoundException 253 { 254 InputStream isConfigFile = null; 255 URL urlDefaultPropertyFile; 256 257 urlDefaultPropertyFile = ClassLoader.getSystemResource(strConfigFileName); 258 if (urlDefaultPropertyFile == null) 259 { 260 urlDefaultPropertyFile = Log.class.getClassLoader().getResource( 270 strConfigFileName); 271 } 272 if (urlDefaultPropertyFile == null) 273 { 274 throw new FileNotFoundException ("Cannot find configuration file " 275 + strConfigFileName); 276 } 277 else 278 { 279 isConfigFile = urlDefaultPropertyFile.openStream(); 280 s_strConfigFile = urlDefaultPropertyFile.toString(); 281 } 282 283 return isConfigFile; 284 } 285 } 286 287 | Popular Tags |