1
0

Sort members

This commit is contained in:
Gary Gregory
2023-11-25 11:07:56 -05:00
parent 35427e2ce7
commit 942b1c37af
2 changed files with 77 additions and 77 deletions

View File

@@ -229,6 +229,39 @@ public abstract class LogFactory {
@Deprecated @Deprecated
protected static volatile LogFactory nullClassLoaderFactory; protected static volatile LogFactory nullClassLoaderFactory;
static {
// note: it's safe to call methods before initDiagnostics (though
// diagnostic output gets discarded).
final ClassLoader thisClassLoader = getClassLoader(LogFactory.class);
thisClassLoaderRef = new WeakReference<>(thisClassLoader);
// In order to avoid confusion where multiple instances of JCL are
// being used via different class loaders within the same app, we
// ensure each logged message has a prefix of form
// [LogFactory from classloader OID]
//
// Note that this prefix should be kept consistent with that
// in LogFactoryImpl. However here we don't need to output info
// about the actual *instance* of LogFactory, as all methods that
// output diagnostics from this class are static.
String classLoaderName;
try {
if (thisClassLoader == null) {
classLoaderName = "BOOTLOADER";
} else {
classLoaderName = objectId(thisClassLoader);
}
} catch (final SecurityException e) {
classLoaderName = "UNKNOWN";
}
diagnosticPrefix = "[LogFactory from " + classLoaderName + "] ";
DIAGNOSTICS_STREAM = initDiagnostics();
logClassLoaderEnvironment(LogFactory.class);
factories = createFactoryStore();
if (isDiagnosticsEnabled()) {
logDiagnostic("BOOTSTRAP COMPLETED");
}
}
/** /**
* Remember this factory, so later calls to LogFactory.getCachedFactory * Remember this factory, so later calls to LogFactory.getCachedFactory
* can return the previously created object (together with all its * can return the previously created object (together with all its
@@ -497,6 +530,7 @@ public abstract class LogFactory {
return classLoader; return classLoader;
} }
/** /**
* Check cached factories (keyed by contextClassLoader) * Check cached factories (keyed by contextClassLoader)
* *
@@ -522,7 +556,6 @@ public abstract class LogFactory {
return factories.get(contextClassLoader); return factories.get(contextClassLoader);
} }
/** /**
* Safely get access to the classloader for the specified class. * Safely get access to the classloader for the specified class.
* <p> * <p>
@@ -655,6 +688,7 @@ public abstract class LogFactory {
return props; return props;
} }
/** /**
* Returns the current context classloader. * Returns the current context classloader.
* <p> * <p>
@@ -676,7 +710,6 @@ public abstract class LogFactory {
return directGetContextClassLoader(); return directGetContextClassLoader();
} }
/** /**
* Calls LogFactory.directGetContextClassLoader under the control of an * Calls LogFactory.directGetContextClassLoader under the control of an
* AccessController class. This means that java code running under a * AccessController class. This means that java code running under a
@@ -1042,6 +1075,7 @@ public abstract class LogFactory {
}); });
} }
/** /**
* Read the specified system property, using an AccessController so that * Read the specified system property, using an AccessController so that
* the property can be read if JCL has been granted the appropriate * the property can be read if JCL has been granted the appropriate
@@ -1056,7 +1090,6 @@ public abstract class LogFactory {
return AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(key, def)); return AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(key, def));
} }
/** /**
* Checks whether the supplied Throwable is one that needs to be * Checks whether the supplied Throwable is one that needs to be
* re-thrown and ignores all others. * re-thrown and ignores all others.
@@ -1568,17 +1601,6 @@ public abstract class LogFactory {
*/ */
public abstract void removeAttribute(String name); public abstract void removeAttribute(String name);
/**
* Sets the configuration attribute with the specified name. Calling
* this with a {@code null} value is equivalent to calling
* {@code removeAttribute(name)}.
*
* @param name Name of the attribute to set
* @param value Value of the attribute to set, or {@code null}
* to remove any setting for this attribute
*/
public abstract void setAttribute(String name, Object value);
// //
// We can't do this in the class constructor, as there are many // We can't do this in the class constructor, as there are many
// static methods on this class that can be called before any // static methods on this class that can be called before any
@@ -1595,37 +1617,15 @@ public abstract class LogFactory {
// So the wisest thing to do is just to place this code at the very end // So the wisest thing to do is just to place this code at the very end
// of the class file. // of the class file.
static { /**
// note: it's safe to call methods before initDiagnostics (though * Sets the configuration attribute with the specified name. Calling
// diagnostic output gets discarded). * this with a {@code null} value is equivalent to calling
final ClassLoader thisClassLoader = getClassLoader(LogFactory.class); * {@code removeAttribute(name)}.
thisClassLoaderRef = new WeakReference<>(thisClassLoader); *
// In order to avoid confusion where multiple instances of JCL are * @param name Name of the attribute to set
// being used via different class loaders within the same app, we * @param value Value of the attribute to set, or {@code null}
// ensure each logged message has a prefix of form * to remove any setting for this attribute
// [LogFactory from classloader OID] */
// public abstract void setAttribute(String name, Object value);
// Note that this prefix should be kept consistent with that
// in LogFactoryImpl. However here we don't need to output info
// about the actual *instance* of LogFactory, as all methods that
// output diagnostics from this class are static.
String classLoaderName;
try {
if (thisClassLoader == null) {
classLoaderName = "BOOTLOADER";
} else {
classLoaderName = objectId(thisClassLoader);
}
} catch (final SecurityException e) {
classLoaderName = "UNKNOWN";
}
diagnosticPrefix = "[LogFactory from " + classLoaderName + "] ";
DIAGNOSTICS_STREAM = initDiagnostics();
logClassLoaderEnvironment(LogFactory.class);
factories = createFactoryStore();
if (isDiagnosticsEnabled()) {
logDiagnostic("BOOTSTRAP COMPLETED");
}
}
} }

View File

@@ -54,6 +54,36 @@ public class Log4JLogger implements Log, Serializable {
private static final Priority traceLevel; private static final Priority traceLevel;
//
// Note that this must come after the static variable declarations
// otherwise initializer expressions associated with those variables
// will override any settings done here.
//
// Verify that log4j is available, and that it is version 1.2.
// If an ExceptionInInitializerError is generated, then LogFactoryImpl
// will treat that as meaning that the appropriate underlying logging
// library is just not present - if discovery is in progress then
// discovery will continue.
static {
if (!Priority.class.isAssignableFrom(Level.class)) {
// nope, this is log4j 1.3, so force an ExceptionInInitializerError
throw new InstantiationError("Log4J 1.2 not available");
}
// Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier
// versions do not. If TRACE is not available, then we have to map
// calls to Log.trace(...) onto the DEBUG level.
Priority _traceLevel;
try {
_traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
} catch (final Exception ex) {
// ok, trace not available
_traceLevel = Level.DEBUG;
}
traceLevel = _traceLevel;
}
/** Log to this logger */ /** Log to this logger */
private transient volatile Logger logger; private transient volatile Logger logger;
@@ -301,34 +331,4 @@ public class Log4JLogger implements Log, Serializable {
getLogger().log(FQCN, Level.WARN, message, t); getLogger().log(FQCN, Level.WARN, message, t);
} }
//
// Note that this must come after the static variable declarations
// otherwise initializer expressions associated with those variables
// will override any settings done here.
//
// Verify that log4j is available, and that it is version 1.2.
// If an ExceptionInInitializerError is generated, then LogFactoryImpl
// will treat that as meaning that the appropriate underlying logging
// library is just not present - if discovery is in progress then
// discovery will continue.
static {
if (!Priority.class.isAssignableFrom(Level.class)) {
// nope, this is log4j 1.3, so force an ExceptionInInitializerError
throw new InstantiationError("Log4J 1.2 not available");
}
// Releases of log4j1.2 >= 1.2.12 have Priority.TRACE available, earlier
// versions do not. If TRACE is not available, then we have to map
// calls to Log.trace(...) onto the DEBUG level.
Priority _traceLevel;
try {
_traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
} catch (final Exception ex) {
// ok, trace not available
_traceLevel = Level.DEBUG;
}
traceLevel = _traceLevel;
}
} }