📄 logger.java
字号:
public void setLevel(Level newLevel) throws SecurityException { if (!anonymous) { manager.checkAccess(); } synchronized (treeLock) { levelObject = newLevel; updateEffectiveLevel(); } } /** * Get the log Level that has been specified for this Logger. * The result may be null, which means that this logger's * effective level will be inherited from its parent. * * @return this Logger's level */ public Level getLevel() { return levelObject; } /** * Check if a message of the given level would actually be logged * by this logger. This check is based on the Loggers effective level, * which may be inherited from its parent. * * @param level a message logging level * @return true if the given message level is currently being logged. */ public boolean isLoggable(Level level) { if (level.intValue() < levelValue || levelValue == offValue) { return false; } return true; } /** * Get the name for this logger. * @return logger name. Will be null for anonymous Loggers. */ public String getName() { return name; } /** * Add a log Handler to receive logging messages. * <p> * By default, Loggers also send their output to their parent logger. * Typically the root Logger is configured with a set of Handlers * that essentially act as default handlers for all loggers. * * @param handler a logging Handler * @exception SecurityException if a security manager exists and if * the caller does not have LoggingPermission("control"). */ public synchronized void addHandler(Handler handler) throws SecurityException { // Check for null handler handler.getClass(); if (!anonymous) { manager.checkAccess(); } if (handlers == null) { handlers = new ArrayList(); } handlers.add(handler); } /** * Remove a log Handler. * <P> * Returns silently if the given Handler is not found. * * @param handler a logging Handler * @exception SecurityException if a security manager exists and if * the caller does not have LoggingPermission("control"). */ public synchronized void removeHandler(Handler handler) throws SecurityException { if (!anonymous) { manager.checkAccess(); } if (handler == null) { throw new NullPointerException(); } if (handlers == null) { return; } handlers.remove(handler); } /** * Get the Handlers associated with this logger. * <p> * @return an array of all registered Handlers */ public synchronized Handler[] getHandlers() { if (handlers == null) { return emptyHandlers; } Handler result[] = new Handler[handlers.size()]; result = (Handler [])handlers.toArray(result); return result; } /** * Specify whether or not this logger should send its output * to it's parent Logger. This means that any LogRecords will * also be written to the parent's Handlers, and potentially * to its parent, recursively up the namespace. * * @param useParentHandlers true if output is to be sent to the * logger's parent. * @exception SecurityException if a security manager exists and if * the caller does not have LoggingPermission("control"). */ public synchronized void setUseParentHandlers(boolean useParentHandlers) { if (!anonymous) { manager.checkAccess(); } this.useParentHandlers = useParentHandlers; } /** * Discover whether or not this logger is sending its output * to its parent logger. * * @return true if output is to be sent to the logger's parent */ public synchronized boolean getUseParentHandlers() { return useParentHandlers; } // Private utility method to map a resource bundle name to an // actual resource bundle, using a simple one-entry cache. // Returns null for a null name. // May also return null if we can't find the resource bundle and // there is no suitable previous cached value. private synchronized ResourceBundle findResourceBundle(String name) { // Return a null bundle for a null name. if (name == null) { return null; } Locale currentLocale = Locale.getDefault(); // Normally we should hit on our simple one entry cache. if (catalog != null && currentLocale == catalogLocale && name == catalogName) { return catalog; } // Use the thread's context ClassLoader. If there isn't one, // use the SystemClassloader. ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl == null) { cl = ClassLoader.getSystemClassLoader(); } try { catalog = ResourceBundle.getBundle(name, currentLocale, cl); catalogName = name; catalogLocale = currentLocale; return catalog; } catch (MissingResourceException ex) { // Woops. We can't find the ResourceBundle in the default // ClassLoader. Drop through. } // Fall back to searching up the call stack and trying each // calling ClassLoader. for (int ix = 0; ; ix++) { Class clz = sun.reflect.Reflection.getCallerClass(ix); if (clz == null) { break; } ClassLoader cl2 = clz.getClassLoader(); if (cl2 == null) { cl2 = ClassLoader.getSystemClassLoader(); } if (cl == cl2) { // We've already checked this classloader. continue; } cl = cl2; try { catalog = ResourceBundle.getBundle(name, currentLocale, cl); catalogName = name; catalogLocale = currentLocale; return catalog; } catch (MissingResourceException ex) { // Ok, this one didn't work either. // Drop through, and try the next one. } } if (name.equals(catalogName)) { // Return the previous cached value for that name. // This may be null. return catalog; } // Sorry, we're out of luck. return null; } // Private utility method to initialize our one entry // resource bundle cache. // Note: for consistency reasons, we are careful to check // that a suitable ResourceBundle exists before setting the // ResourceBundleName. private synchronized void setupResourceInfo(String name) { if (name == null) { return; } ResourceBundle rb = findResourceBundle(name); if (rb == null) { // We've failed to find an expected ResourceBundle. throw new MissingResourceException("Can't find " + name + " bundle", name, ""); } resourceBundleName = name; } /** * Return the parent for this Logger. * <p> * This method returns the nearest extant parent in the namespace. * Thus if a Logger is called "a.b.c.d", and a Logger called "a.b" * has been created but no logger "a.b.c" exists, then a call of * getParent on the Logger "a.b.c.d" will return the Logger "a.b". * <p> * The result will be null if it is called on the root Logger * in the namespace. * * @return nearest existing parent Logger */ public Logger getParent() { synchronized (treeLock) { return parent; } } /** * Set the parent for this Logger. This method is used by * the LogManager to update a Logger when the namespace changes. * <p> * It should not be called from application code. * <p> * @param parent the new parent logger * @exception SecurityException if a security manager exists and if * the caller does not have LoggingPermission("control"). */ public void setParent(Logger parent) { if (parent == null) { throw new NullPointerException(); } manager.checkAccess(); doSetParent(parent); } // Private method to do the work for parenting a child // Loggger onto a parent logger. private void doSetParent(Logger newParent) { // System.err.println("doSetParent \"" + getName() + "\" \"" // + newParent.getName() + "\""); synchronized (treeLock) { // Remove ourself from any previous parent. if (parent != null) { // assert parent.kids != null; for (Iterator iter = parent.kids.iterator(); iter.hasNext(); ) { WeakReference ref = (WeakReference) iter.next(); Logger kid = (Logger) ref.get(); if (kid == this) { iter.remove(); break; } } // We have now removed ourself from our parents' kids. } // Set our new parent. parent = newParent; if (parent.kids == null) { parent.kids = new ArrayList(2); } parent.kids.add(new WeakReference(this)); // As a result of the reparenting, the effective level // may have changed for us and our children. updateEffectiveLevel(); } } // Recalculate the effective level for this node and // recursively for our children. private void updateEffectiveLevel() { // assert Thread.holdsLock(treeLock); // Figure out our current effective level. int newLevelValue; if (levelObject != null) { newLevelValue = levelObject.intValue(); } else { if (parent != null) { newLevelValue = parent.levelValue; } else { // This may happen during initialization. newLevelValue = Level.INFO.intValue(); } } // If our effective value hasn't changed, we're done. if (levelValue == newLevelValue) { return; } levelValue = newLevelValue; // System.err.println("effective level: \"" + getName() + "\" := " + level); // Recursively update the level on each of our kids. if (kids != null) { for (int i = 0; i < kids.size(); i++) { WeakReference ref = (WeakReference)kids.get(i); Logger kid = (Logger) ref.get(); if (kid != null) { kid.updateEffectiveLevel(); } } } } // Private method to get the potentially inherited // resource bundle name for this Logger. // May return null private String getEffectiveResourceBundleName() { Logger target = this; while (target != null) { String rbn = target.getResourceBundleName(); if (rbn != null) { return rbn; } target = target.getParent(); } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -