logfactoryimpl.java
来自「logging日志的相关包 logging日志的相关包」· Java 代码 · 共 1,401 行 · 第 1/4 页
JAVA
1,401 行
instances.clear(); } /** * Remove any configuration attribute associated with the specified name. * If there is no such attribute, no action is taken. * * @param name Name of the attribute to remove */ public void removeAttribute(String name) { attributes.remove(name); } /** * Set the configuration attribute with the specified name. Calling * this with a <code>null</code> value is equivalent to calling * <code>removeAttribute(name)</code>. * <p> * This method can be used to set logging configuration programmatically * rather than via system properties. It can also be used in code running * within a container (such as a webapp) to configure behaviour on a * per-component level instead of globally as system properties would do. * To use this method instead of a system property, call * <pre> * LogFactory.getFactory().setAttribute(...) * </pre> * This must be done before the first Log object is created; configuration * changes after that point will be ignored. * <p> * This method is also called automatically if LogFactory detects a * commons-logging.properties file; every entry in that file is set * automatically as an attribute here. * * @param name Name of the attribute to set * @param value Value of the attribute to set, or <code>null</code> * to remove any setting for this attribute */ public void setAttribute(String name, Object value) { if (logConstructor != null) { logDiagnostic("setAttribute: call too late; configuration already performed."); } if (value == null) { attributes.remove(name); } else { attributes.put(name, value); } if (name.equals(TCCL_KEY)) { useTCCL = Boolean.valueOf(value.toString()).booleanValue(); } } // ------------------------------------------------------ // Static Methods // // These methods only defined as workarounds for a java 1.2 bug; // theoretically none of these are needed. // ------------------------------------------------------ /** * Gets the context classloader. * This method is a workaround for a java 1.2 compiler bug. * @since 1.1 */ protected static ClassLoader getContextClassLoader() throws LogConfigurationException { return LogFactory.getContextClassLoader(); } /** * Workaround for bug in Java1.2; in theory this method is not needed. * See LogFactory.isDiagnosticsEnabled. */ protected static boolean isDiagnosticsEnabled() { return LogFactory.isDiagnosticsEnabled(); } /** * Workaround for bug in Java1.2; in theory this method is not needed. * See LogFactory.getClassLoader. * @since 1.1 */ protected static ClassLoader getClassLoader(Class clazz) { return LogFactory.getClassLoader(clazz); } // ------------------------------------------------------ Protected Methods /** * Calculate and cache a string that uniquely identifies this instance, * including which classloader the object was loaded from. * <p> * This string will later be prefixed to each "internal logging" message * emitted, so that users can clearly see any unexpected behaviour. * <p> * Note that this method does not detect whether internal logging is * enabled or not, nor where to output stuff if it is; that is all * handled by the parent LogFactory class. This method just computes * its own unique prefix for log messages. */ private void initDiagnostics() { // It would be nice to include an identifier of the context classloader // that this LogFactoryImpl object is responsible for. However that // isn't possible as that information isn't available. It is possible // to figure this out by looking at the logging from LogFactory to // see the context & impl ids from when this object was instantiated, // in order to link the impl id output as this object's prefix back to // the context it is intended to manage. // Note that this prefix should be kept consistent with that // in LogFactory. Class clazz = this.getClass(); ClassLoader classLoader = getClassLoader(clazz); String classLoaderName; try { if (classLoader == null) { classLoaderName = "BOOTLOADER"; } else { classLoaderName = objectId(classLoader); } } catch(SecurityException e) { classLoaderName = "UNKNOWN"; } diagnosticPrefix = "[LogFactoryImpl@" + System.identityHashCode(this) + " from " + classLoaderName + "] "; } /** * Output a diagnostic message to a user-specified destination (if the * user has enabled diagnostic logging). * * @param msg diagnostic message * @since 1.1 */ protected void logDiagnostic(String msg) { if (isDiagnosticsEnabled()) { logRawDiagnostic(diagnosticPrefix + msg); } } /** * Return the fully qualified Java classname of the {@link Log} * implementation we will be using. * * @deprecated Never invoked by this class; subclasses should not assume * it will be. */ protected String getLogClassName() { if (logClassName == null) { discoverLogImplementation(getClass().getName()); } return logClassName; } /** * <p>Return the <code>Constructor</code> that can be called to instantiate * new {@link org.apache.commons.logging.Log} instances.</p> * * <p><strong>IMPLEMENTATION NOTE</strong> - Race conditions caused by * calling this method from more than one thread are ignored, because * the same <code>Constructor</code> instance will ultimately be derived * in all circumstances.</p> * * @exception LogConfigurationException if a suitable constructor * cannot be returned * * @deprecated Never invoked by this class; subclasses should not assume * it will be. */ protected Constructor getLogConstructor() throws LogConfigurationException { // Return the previously identified Constructor (if any) if (logConstructor == null) { discoverLogImplementation(getClass().getName()); } return logConstructor; } /** * Is <em>JDK 1.3 with Lumberjack</em> logging available? * * @deprecated Never invoked by this class; subclasses should not assume * it will be. */ protected boolean isJdk13LumberjackAvailable() { return isLogLibraryAvailable( "Jdk13Lumberjack", "org.apache.commons.logging.impl.Jdk13LumberjackLogger"); } /** * <p>Return <code>true</code> if <em>JDK 1.4 or later</em> logging * is available. Also checks that the <code>Throwable</code> class * supports <code>getStackTrace()</code>, which is required by * Jdk14Logger.</p> * * @deprecated Never invoked by this class; subclasses should not assume * it will be. */ protected boolean isJdk14Available() { return isLogLibraryAvailable( "Jdk14", "org.apache.commons.logging.impl.Jdk14Logger"); } /** * Is a <em>Log4J</em> implementation available? * * @deprecated Never invoked by this class; subclasses should not assume * it will be. */ protected boolean isLog4JAvailable() { return isLogLibraryAvailable( "Log4J", LOGGING_IMPL_LOG4J_LOGGER); } /** * Create and return a new {@link org.apache.commons.logging.Log} * instance for the specified name. * * @param name Name of the new logger * * @exception LogConfigurationException if a new instance cannot * be created */ protected Log newInstance(String name) throws LogConfigurationException { Log instance = null; try { if (logConstructor == null) { instance = discoverLogImplementation(name); } else { Object params[] = { name }; instance = (Log) logConstructor.newInstance(params); } if (logMethod != null) { Object params[] = { this }; logMethod.invoke(instance, params); } return (instance); } catch (LogConfigurationException lce) { // this type of exception means there was a problem in discovery // and we've already output diagnostics about the issue, etc.; // just pass it on throw (LogConfigurationException) lce; } catch (InvocationTargetException e) { // A problem occurred invoking the Constructor or Method // previously discovered Throwable c = e.getTargetException(); if (c != null) { throw new LogConfigurationException(c); } else { throw new LogConfigurationException(e); } } catch (Throwable t) { // A problem occurred invoking the Constructor or Method // previously discovered throw new LogConfigurationException(t); } } // ------------------------------------------------------ Private Methods /** * Utility method to check whether a particular logging library is * present and available for use. Note that this does <i>not</i> * affect the future behaviour of this class. */ private boolean isLogLibraryAvailable(String name, String classname) { if (isDiagnosticsEnabled()) { logDiagnostic("Checking for '" + name + "'."); } try { Log log = createLogFromClass( classname, this.getClass().getName(), // dummy category false); if (log == null) { if (isDiagnosticsEnabled()) { logDiagnostic("Did not find '" + name + "'."); } return false; } else { if (isDiagnosticsEnabled()) { logDiagnostic("Found '" + name + "'."); } return true; } } catch(LogConfigurationException e) { if (isDiagnosticsEnabled()) { logDiagnostic("Logging system '" + name + "' is available but not useable."); } return false; } } /** * Attempt to find an attribute (see method setAttribute) or a * system property with the provided name and return its value. * <p> * The attributes associated with this object are checked before * system properties in case someone has explicitly called setAttribute, * or a configuration property has been set in a commons-logging.properties * file. * * @return the value associated with the property, or null. */ private String getConfigurationValue(String property) { if (isDiagnosticsEnabled()) { logDiagnostic("[ENV] Trying to get configuration for item " + property); } Object valueObj = getAttribute(property); if (valueObj != null) { if (isDiagnosticsEnabled()) { logDiagnostic("[ENV] Found LogFactory attribute [" + valueObj + "] for " + property); } return valueObj.toString(); } if (isDiagnosticsEnabled()) { logDiagnostic("[ENV] No LogFactory attribute found for " + property); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?