1 23 24 package com.sun.jdo.spi.persistence.utility.logging; 25 26 import java.io.IOException ; 27 import java.text.MessageFormat ; 28 import java.util.ResourceBundle ; 29 import java.util.logging.Level ; 30 import java.util.logging.LogManager ; 31 import java.util.logging.Formatter ; 32 import java.util.logging.FileHandler ; 33 import java.util.logging.SimpleFormatter ; 34 import java.security.AccessController ; 35 import java.security.PrivilegedAction ; 36 import com.sun.jdo.spi.persistence.utility.I18NHelper; 37 38 43 44 public class LoggerFactoryJDK14 extends AbstractLoggerFactory { 45 46 47 private static final ResourceBundle _messages = 48 I18NHelper.loadBundle(LoggerFactoryJDK14.class); 49 50 52 protected static ResourceBundle getMessages () { return _messages; } 53 54 55 public LoggerFactoryJDK14() { 56 } 57 58 protected LoggerJDK14 findLogger(String absoluteLoggerName) { 59 return (LoggerJDK14) 60 LogManager.getLogManager().getLogger(absoluteLoggerName); 61 } 62 63 80 protected Logger createLogger (final String absoluteLoggerName, 81 final String bundleName, final ClassLoader loader) { 82 return (Logger) AccessController.doPrivileged ( 83 new PrivilegedAction () { 84 public Object run () { 85 LoggerJDK14 logger = null; 86 ClassLoader pushed = Thread.currentThread().getContextClassLoader(); 87 if(loader!=null) { 88 setContextClassLoader (loader); 89 } 90 try { 91 logger = createLogger(absoluteLoggerName, bundleName); 92 LogManager.getLogManager().addLogger(logger); 93 configureFileHandler(logger); 94 95 return logger; 96 } catch (Exception ex) { 97 MessageFormat messageFormat = new MessageFormat ( 98 getMessages().getString("errorlogger.create.exception")); 99 100 getErrorLogger().log(Logger.SEVERE, messageFormat.format( 101 new String []{absoluteLoggerName}), ex); 102 } finally { 103 setContextClassLoader (pushed); 104 } 105 106 return logger; 107 } 108 } 109 ); 110 } 111 112 117 protected void setContextClassLoader (final ClassLoader loader) { 118 if (loader != null) { 119 Thread.currentThread().setContextClassLoader (loader); 120 } 121 } 123 124 protected LoggerJDK14 createLogger (String absoluteLoggerName, String 125 bundleName) { 126 LoggerJDK14 result = new LoggerJDK14(absoluteLoggerName, bundleName); 127 return result; 128 } 129 130 135 protected void configureFileHandler(LoggerJDK14 logger) { 136 String name = logger.getName(); 137 String baseName = name + ".FileHandler"; LogManager logManager = LogManager.getLogManager(); 139 140 String pattern = logManager.getProperty(baseName + ".pattern"); if(pattern != null) { 142 146 int defaultLimit = 0; 147 String limit = logManager.getProperty(baseName + ".limit"); if(limit != null) { 149 try { 150 defaultLimit = Integer.parseInt(limit); 151 if(defaultLimit < 0) 152 defaultLimit = 0; 153 } 154 catch (NumberFormatException e) { 155 } 156 } 157 158 int defaultCount = 1; 159 String count = logManager.getProperty(baseName + ".count"); if(count != null) { 161 try { 162 defaultCount = Integer.parseInt(count); 163 if(defaultCount < 1) 164 defaultCount = 1; 165 } 166 catch (NumberFormatException e) { 167 } 168 } 169 170 boolean defaultAppend = false; 171 String append = logManager.getProperty(baseName + ".append"); if(append != null) { 173 defaultAppend = Boolean.valueOf(append).booleanValue(); 174 } 175 176 FileHandler fileHandler = null; 177 try { 178 fileHandler = new FileHandler (pattern, defaultLimit, 179 defaultCount, defaultAppend); 180 } 181 catch(Exception e) { 182 MessageFormat messageFormat = new MessageFormat ( getMessages().getString( 183 "errorlogger.filehandler.initialize.exception")); 185 getErrorLogger().log(Logger.WARNING, 186 messageFormat.format(new String []{name}), e); 187 } 188 189 if(fileHandler != null) { 190 String level = logManager.getProperty(baseName + ".level"); if (level != null) { 194 try { 195 fileHandler.setLevel(Level.parse(level) ); 196 } 197 catch(IllegalArgumentException e) { 198 } 199 } 200 201 Formatter defaultFormatter = null; 203 String formatter = logManager.getProperty(baseName + ".formatter"); if(formatter != null) { 206 try { 207 Class clz = ClassLoader.getSystemClassLoader().loadClass(formatter); 208 defaultFormatter = (Formatter ) clz.newInstance(); 209 } catch (Exception e) { 210 MessageFormat messageFormat = new MessageFormat ( 214 getMessages().getString("errorlogger.formatter.initialize.exception")); 215 216 getErrorLogger().log(Logger.WARNING, messageFormat.format(new String []{name}), e); 217 } 218 219 } 220 221 if (defaultFormatter == null) { 222 defaultFormatter = new SimpleFormatter (); 223 } 224 225 try { 226 fileHandler.setFormatter(defaultFormatter); 227 } 228 catch(IllegalArgumentException e) { 229 } 230 231 logger.addHandler(fileHandler); 232 233 } 235 } 237 } 238 } 239 | Popular Tags |