1 21 package oracle.toplink.essentials.logging; 23 24 25 29 import java.util.*; 30 import java.util.logging.*; 31 import java.security.AccessController ; 32 import java.security.PrivilegedAction ; 33 import oracle.toplink.essentials.sessions.Session; 34 import oracle.toplink.essentials.internal.localization.i18n.*; 35 36 47 public class JavaLog extends AbstractSessionLog { 48 49 52 protected static final String DEFAULT_SESSION_NAME = "defaultsessionname"; 53 public static final String DEFAULT_TOPLINK_NAMESPACE = "oracle.toplink.essentials"; 54 protected static final String LOGGING_LOCALIZATION_STRING = "oracle.toplink.essentials.internal.localization.i18n.LoggingLocalizationResource"; 55 protected static final String TRACE_LOCALIZATION_STRING = "oracle.toplink.essentials.internal.localization.i18n.TraceLocalizationResource"; 56 57 60 private static final Level[] levels = new Level[] { Level.ALL, Level.FINEST, Level.FINER, Level.FINE, Level.CONFIG, Level.INFO, Level.WARNING, Level.SEVERE, Level.OFF }; 61 62 65 private static Logger defaultTopLinkLogger = Logger.getLogger(DEFAULT_TOPLINK_NAMESPACE);; 66 67 71 private Map nameSpaceMap = new HashMap(); 72 73 76 private String sessionNameSpace; 77 78 81 private Logger sessionLogger; 82 83 private Map catagoryloggers = new HashMap<String , Logger>(); 84 85 86 97 public int getLevel(String category) { 98 Logger logger = getLogger(category); 99 while ((logger != null) && (logger.getLevel() == null)) { 100 logger = logger.getParent(); 101 } 102 103 if (logger == null) { 104 return OFF; 105 } 106 107 int logLevel = logger.getLevel().intValue(); 109 for (int i = 0; i < levels.length ; i++) { 110 if (logLevel == levels[i].intValue()) { 111 return i; 112 } 113 } 114 return OFF; 115 } 116 117 123 public void setLevel(final int level, String category) { 124 final Logger logger = getLogger(category); 125 AccessController.doPrivileged(new PrivilegedAction () { 126 public Object run() { 127 logger.setLevel(getJavaLevel(level)); 128 return null; } 130 }); 131 } 132 133 137 protected String getNameSpaceString(String category) { 138 if (session == null) { 139 return DEFAULT_TOPLINK_NAMESPACE; 140 } else if ((category == null) || (category.length() == 0)) { 141 return sessionNameSpace; 142 } else { 143 return (String )nameSpaceMap.get(category); 144 } 145 } 146 147 151 protected Logger getLogger(String category) { 152 if (session == null) { 153 return defaultTopLinkLogger; 154 } else if ((category == null) || (category.length() == 0)) { 155 return sessionLogger; 156 } else { 157 Logger logger = (Logger) catagoryloggers.get(category); 158 assert logger != null; 160 return logger; 161 } 162 } 163 164 165 174 public void setSession(Session session) { 175 super.setSession(session); 176 if (session != null) { 177 String sessionName; 178 if ((session.getName() != null) && (session.getName().length() != 0)) { 179 sessionName = session.getName(); 180 } else { 181 sessionName = DEFAULT_SESSION_NAME; 182 } 183 sessionNameSpace = DEFAULT_TOPLINK_NAMESPACE + "." + sessionName; 184 185 sessionLogger = Logger.getLogger(sessionNameSpace); 187 for (int i = 0; i < loggerCatagories.length; i++) { 188 String loggerCatagory = loggerCatagories[i]; 189 String loggerNameSpace = sessionNameSpace + "." + loggerCatagory; 190 nameSpaceMap.put(loggerCatagory, loggerNameSpace); 191 catagoryloggers.put(loggerCatagory, Logger.getLogger(loggerNameSpace)); 192 } 193 } 194 } 195 196 200 private Level getJavaLevel(int level) { 201 return levels[level]; 202 } 203 204 214 public boolean shouldLog(int level, String category) { 215 Logger logger = getLogger(category); 216 return logger.isLoggable(getJavaLevel(level)); 217 } 218 219 227 public void log(SessionLogEntry entry) { 228 if (!shouldLog(entry.getLevel(), entry.getNameSpace())) { 229 return; 230 } 231 232 Logger logger = getLogger(entry.getNameSpace()); 233 Level javaLevel = getJavaLevel(entry.getLevel()); 234 235 internalLog(entry, javaLevel, logger); 236 } 237 238 247 protected void internalLog(SessionLogEntry entry, Level javaLevel, Logger logger) { 248 TopLinkLogRecord lr = new TopLinkLogRecord(javaLevel, entry.getMessage()); 249 lr.setSourceClassName(null); 250 lr.setSourceMethodName(null); 251 lr.setParameters(entry.getParameters()); 252 lr.setLoggerName(getNameSpaceString(entry.getNameSpace())); 253 if (entry.shouldTranslate()) { 254 String bundleName; 255 ResourceBundle resourceBundle; 256 if (entry.getLevel() > FINE) { 257 bundleName = LOGGING_LOCALIZATION_STRING; 258 resourceBundle = new LoggingLocalizationResource(); 259 } else { 260 bundleName = TRACE_LOCALIZATION_STRING; 261 resourceBundle = new TraceLocalizationResource(); 262 } 263 lr.setResourceBundleName(bundleName); 264 lr.setResourceBundle(resourceBundle); 265 } 266 if (shouldPrintSession()) { 267 lr.setSessionString(getSessionString(entry.getSession())); 268 } 269 if (shouldPrintConnection()) { 270 lr.setConnection(entry.getConnection()); 271 } 272 lr.setThrown(entry.getException()); 273 lr.setShouldLogExceptionStackTrace(shouldLogExceptionStackTrace()); 274 lr.setShouldPrintDate(shouldPrintDate()); 275 lr.setShouldPrintThread(shouldPrintThread()); 276 logger.log(lr); 277 } 278 279 287 public void throwing(Throwable throwable) { 288 getLogger(null).throwing(null, null, throwable); 289 } 290 291 295 public Object clone() { 296 JavaLog cloneLog = (JavaLog)super.clone(); 303 return cloneLog; 304 } 305 } 306 307 | Popular Tags |