提交 f91f21c6 编写于 作者: D dfuchs

8029740: Enhance handling of loggers

Reviewed-by: mchung, ahgross
上级 b746c767
...@@ -548,7 +548,7 @@ public class LogManager { ...@@ -548,7 +548,7 @@ public class LogManager {
Logger result = getLogger(name); Logger result = getLogger(name);
if (result == null) { if (result == null) {
// only allocate the new logger once // only allocate the new logger once
Logger newLogger = new Logger(name, resourceBundleName, caller, this); Logger newLogger = new Logger(name, resourceBundleName, caller, this, false);
do { do {
if (addLogger(newLogger)) { if (addLogger(newLogger)) {
// We successfully added the new Logger that we // We successfully added the new Logger that we
...@@ -595,13 +595,13 @@ public class LogManager { ...@@ -595,13 +595,13 @@ public class LogManager {
} while (logger == null); } while (logger == null);
// LogManager will set the sysLogger's handlers via LogManager.addLogger method. // LogManager will set the sysLogger's handlers via LogManager.addLogger method.
if (logger != sysLogger && sysLogger.getHandlers().length == 0) { if (logger != sysLogger && sysLogger.accessCheckedHandlers().length == 0) {
// if logger already exists but handlers not set // if logger already exists but handlers not set
final Logger l = logger; final Logger l = logger;
AccessController.doPrivileged(new PrivilegedAction<Void>() { AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override @Override
public Void run() { public Void run() {
for (Handler hdl : l.getHandlers()) { for (Handler hdl : l.accessCheckedHandlers()) {
sysLogger.addHandler(hdl); sysLogger.addHandler(hdl);
} }
return null; return null;
...@@ -919,7 +919,7 @@ public class LogManager { ...@@ -919,7 +919,7 @@ public class LogManager {
Logger result = findLogger(name); Logger result = findLogger(name);
if (result == null) { if (result == null) {
// only allocate the new system logger once // only allocate the new system logger once
Logger newLogger = new Logger(name, resourceBundleName, null, getOwner()); Logger newLogger = new Logger(name, resourceBundleName, null, getOwner(), true);
do { do {
if (addLocalLogger(newLogger)) { if (addLocalLogger(newLogger)) {
// We successfully added the new Logger that we // We successfully added the new Logger that we
...@@ -1637,7 +1637,7 @@ public class LogManager { ...@@ -1637,7 +1637,7 @@ public class LogManager {
// We do not call the protected Logger two args constructor here, // We do not call the protected Logger two args constructor here,
// to avoid calling LogManager.getLogManager() from within the // to avoid calling LogManager.getLogManager() from within the
// RootLogger constructor. // RootLogger constructor.
super("", null, null, LogManager.this); super("", null, null, LogManager.this, true);
setLevel(defaultLevel); setLevel(defaultLevel);
} }
...@@ -1661,9 +1661,9 @@ public class LogManager { ...@@ -1661,9 +1661,9 @@ public class LogManager {
} }
@Override @Override
public Handler[] getHandlers() { Handler[] accessCheckedHandlers() {
initializeGlobalHandlers(); initializeGlobalHandlers();
return super.getHandlers(); return super.accessCheckedHandlers();
} }
} }
......
...@@ -277,6 +277,7 @@ public class Logger { ...@@ -277,6 +277,7 @@ public class Logger {
private volatile Level levelObject; private volatile Level levelObject;
private volatile int levelValue; // current effective level value private volatile int levelValue; // current effective level value
private WeakReference<ClassLoader> callersClassLoaderRef; private WeakReference<ClassLoader> callersClassLoaderRef;
private final boolean isSystemLogger;
/** /**
* GLOBAL_LOGGER_NAME is a name for the global logger. * GLOBAL_LOGGER_NAME is a name for the global logger.
...@@ -370,11 +371,12 @@ public class Logger { ...@@ -370,11 +371,12 @@ public class Logger {
* no corresponding resource can be found. * no corresponding resource can be found.
*/ */
protected Logger(String name, String resourceBundleName) { protected Logger(String name, String resourceBundleName) {
this(name, resourceBundleName, null, LogManager.getLogManager()); this(name, resourceBundleName, null, LogManager.getLogManager(), false);
} }
Logger(String name, String resourceBundleName, Class<?> caller, LogManager manager) { Logger(String name, String resourceBundleName, Class<?> caller, LogManager manager, boolean isSystemLogger) {
this.manager = manager; this.manager = manager;
this.isSystemLogger = isSystemLogger;
setupResourceInfo(resourceBundleName, caller); setupResourceInfo(resourceBundleName, caller);
this.name = name; this.name = name;
levelValue = Level.INFO.intValue(); levelValue = Level.INFO.intValue();
...@@ -401,6 +403,7 @@ public class Logger { ...@@ -401,6 +403,7 @@ public class Logger {
private Logger(String name) { private Logger(String name) {
// The manager field is not initialized here. // The manager field is not initialized here.
this.name = name; this.name = name;
this.isSystemLogger = true;
levelValue = Level.INFO.intValue(); levelValue = Level.INFO.intValue();
} }
...@@ -635,7 +638,7 @@ public class Logger { ...@@ -635,7 +638,7 @@ public class Logger {
// cleanup some Loggers that have been GC'ed // cleanup some Loggers that have been GC'ed
manager.drainLoggerRefQueueBounded(); manager.drainLoggerRefQueueBounded();
Logger result = new Logger(null, resourceBundleName, Logger result = new Logger(null, resourceBundleName,
Reflection.getCallerClass(), manager); Reflection.getCallerClass(), manager, false);
result.anonymous = true; result.anonymous = true;
Logger root = manager.getLogger(""); Logger root = manager.getLogger("");
result.doSetParent(root); result.doSetParent(root);
...@@ -727,15 +730,23 @@ public class Logger { ...@@ -727,15 +730,23 @@ public class Logger {
Logger logger = this; Logger logger = this;
while (logger != null) { while (logger != null) {
for (Handler handler : logger.getHandlers()) { final Handler[] loggerHandlers = isSystemLogger
? logger.accessCheckedHandlers()
: logger.getHandlers();
for (Handler handler : loggerHandlers) {
handler.publish(record); handler.publish(record);
} }
if (!logger.getUseParentHandlers()) { final boolean useParentHdls = isSystemLogger
? logger.useParentHandlers
: logger.getUseParentHandlers();
if (!useParentHdls) {
break; break;
} }
logger = logger.getParent(); logger = isSystemLogger ? logger.parent : logger.getParent();
} }
} }
...@@ -1762,6 +1773,12 @@ public class Logger { ...@@ -1762,6 +1773,12 @@ public class Logger {
* @return an array of all registered Handlers * @return an array of all registered Handlers
*/ */
public Handler[] getHandlers() { public Handler[] getHandlers() {
return accessCheckedHandlers();
}
// This method should ideally be marked final - but unfortunately
// it needs to be overridden by LogManager.RootLogger
Handler[] accessCheckedHandlers() {
return handlers.toArray(emptyHandlers); return handlers.toArray(emptyHandlers);
} }
...@@ -2150,12 +2167,14 @@ public class Logger { ...@@ -2150,12 +2167,14 @@ public class Logger {
if (trb.userBundle != null) { if (trb.userBundle != null) {
return trb; return trb;
} }
final String rbName = target.getResourceBundleName(); final String rbName = isSystemLogger
? trb.resourceBundleName
: target.getResourceBundleName();
if (rbName != null) { if (rbName != null) {
return LoggerBundle.get(rbName, return LoggerBundle.get(rbName,
findResourceBundle(rbName, true)); findResourceBundle(rbName, true));
} }
target = target.getParent(); target = isSystemLogger ? target.parent : target.getParent();
} }
return NO_RESOURCE_BUNDLE; return NO_RESOURCE_BUNDLE;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册