author | dfuchs |
Wed, 22 Oct 2014 17:23:14 +0200 | |
changeset 27191 | 45a3002de56f |
parent 26859 | 8df035e321b8 |
child 27754 | ee2f7768e1c9 |
child 27565 | 729f9700483a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
26859
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package java.util.logging; |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.util.*; |
|
31 |
import java.security.*; |
|
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
32 |
import java.lang.ref.ReferenceQueue; |
2 | 33 |
import java.lang.ref.WeakReference; |
16098 | 34 |
import sun.misc.JavaAWTAccess; |
35 |
import sun.misc.SharedSecrets; |
|
2 | 36 |
|
37 |
/** |
|
38 |
* There is a single global LogManager object that is used to |
|
39 |
* maintain a set of shared state about Loggers and log services. |
|
40 |
* <p> |
|
41 |
* This LogManager object: |
|
42 |
* <ul> |
|
43 |
* <li> Manages a hierarchical namespace of Logger objects. All |
|
44 |
* named Loggers are stored in this namespace. |
|
45 |
* <li> Manages a set of logging control properties. These are |
|
46 |
* simple key-value pairs that can be used by Handlers and |
|
47 |
* other logging objects to configure themselves. |
|
48 |
* </ul> |
|
49 |
* <p> |
|
50 |
* The global LogManager object can be retrieved using LogManager.getLogManager(). |
|
51 |
* The LogManager object is created during class initialization and |
|
52 |
* cannot subsequently be changed. |
|
53 |
* <p> |
|
54 |
* At startup the LogManager class is located using the |
|
55 |
* java.util.logging.manager system property. |
|
56 |
* <p> |
|
14691
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
57 |
* The LogManager defines two optional system properties that allow control over |
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
58 |
* the initial configuration: |
2 | 59 |
* <ul> |
60 |
* <li>"java.util.logging.config.class" |
|
61 |
* <li>"java.util.logging.config.file" |
|
62 |
* </ul> |
|
14691
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
63 |
* These two properties may be specified on the command line to the "java" |
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
64 |
* command, or as system property definitions passed to JNI_CreateJavaVM. |
2 | 65 |
* <p> |
66 |
* If the "java.util.logging.config.class" property is set, then the |
|
67 |
* property value is treated as a class name. The given class will be |
|
68 |
* loaded, an object will be instantiated, and that object's constructor |
|
69 |
* is responsible for reading in the initial configuration. (That object |
|
70 |
* may use other system properties to control its configuration.) The |
|
71 |
* alternate configuration class can use <tt>readConfiguration(InputStream)</tt> |
|
72 |
* to define properties in the LogManager. |
|
73 |
* <p> |
|
74 |
* If "java.util.logging.config.class" property is <b>not</b> set, |
|
75 |
* then the "java.util.logging.config.file" system property can be used |
|
76 |
* to specify a properties file (in java.util.Properties format). The |
|
77 |
* initial logging configuration will be read from this file. |
|
78 |
* <p> |
|
14691
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
79 |
* If neither of these properties is defined then the LogManager uses its |
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
80 |
* default configuration. The default configuration is typically loaded from the |
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
81 |
* properties file "{@code lib/logging.properties}" in the Java installation |
22056e110b04
8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents:
14342
diff
changeset
|
82 |
* directory. |
2 | 83 |
* <p> |
84 |
* The properties for loggers and Handlers will have names starting |
|
85 |
* with the dot-separated name for the handler or logger. |
|
86 |
* <p> |
|
87 |
* The global logging properties may include: |
|
88 |
* <ul> |
|
89 |
* <li>A property "handlers". This defines a whitespace or comma separated |
|
90 |
* list of class names for handler classes to load and register as |
|
91 |
* handlers on the root Logger (the Logger named ""). Each class |
|
92 |
* name must be for a Handler class which has a default constructor. |
|
93 |
* Note that these Handlers may be created lazily, when they are |
|
94 |
* first used. |
|
95 |
* |
|
96 |
* <li>A property "<logger>.handlers". This defines a whitespace or |
|
97 |
* comma separated list of class names for handlers classes to |
|
98 |
* load and register as handlers to the specified logger. Each class |
|
99 |
* name must be for a Handler class which has a default constructor. |
|
100 |
* Note that these Handlers may be created lazily, when they are |
|
101 |
* first used. |
|
102 |
* |
|
103 |
* <li>A property "<logger>.useParentHandlers". This defines a boolean |
|
104 |
* value. By default every logger calls its parent in addition to |
|
105 |
* handling the logging message itself, this often result in messages |
|
106 |
* being handled by the root logger as well. When setting this property |
|
107 |
* to false a Handler needs to be configured for this logger otherwise |
|
108 |
* no logging messages are delivered. |
|
109 |
* |
|
110 |
* <li>A property "config". This property is intended to allow |
|
111 |
* arbitrary configuration code to be run. The property defines a |
|
112 |
* whitespace or comma separated list of class names. A new instance will be |
|
113 |
* created for each named class. The default constructor of each class |
|
114 |
* may execute arbitrary code to update the logging configuration, such as |
|
115 |
* setting logger levels, adding handlers, adding filters, etc. |
|
116 |
* </ul> |
|
117 |
* <p> |
|
118 |
* Note that all classes loaded during LogManager configuration are |
|
119 |
* first searched on the system class path before any user class path. |
|
120 |
* That includes the LogManager class, any config classes, and any |
|
121 |
* handler classes. |
|
122 |
* <p> |
|
123 |
* Loggers are organized into a naming hierarchy based on their |
|
124 |
* dot separated names. Thus "a.b.c" is a child of "a.b", but |
|
125 |
* "a.b1" and a.b2" are peers. |
|
126 |
* <p> |
|
127 |
* All properties whose names end with ".level" are assumed to define |
|
128 |
* log levels for Loggers. Thus "foo.level" defines a log level for |
|
129 |
* the logger called "foo" and (recursively) for any of its children |
|
130 |
* in the naming hierarchy. Log Levels are applied in the order they |
|
131 |
* are defined in the properties file. Thus level settings for child |
|
132 |
* nodes in the tree should come after settings for their parents. |
|
133 |
* The property name ".level" can be used to set the level for the |
|
134 |
* root of the tree. |
|
135 |
* <p> |
|
136 |
* All methods on the LogManager object are multi-thread safe. |
|
137 |
* |
|
138 |
* @since 1.4 |
|
139 |
*/ |
|
140 |
||
141 |
public class LogManager { |
|
142 |
// The global LogManager object |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
143 |
private static final LogManager manager; |
2 | 144 |
|
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
145 |
// 'props' is assigned within a lock but accessed without it. |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
146 |
// Declaring it volatile makes sure that another thread will not |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
147 |
// be able to see a partially constructed 'props' object. |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
148 |
// (seeing a partially constructed 'props' object can result in |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
149 |
// NPE being thrown in Hashtable.get(), because it leaves the door |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
150 |
// open for props.getProperties() to be called before the construcor |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
151 |
// of Hashtable is actually completed). |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
152 |
private volatile Properties props = new Properties(); |
2 | 153 |
private final static Level defaultLevel = Level.INFO; |
154 |
||
16098 | 155 |
// LoggerContext for system loggers and user loggers |
156 |
private final LoggerContext systemContext = new SystemLoggerContext(); |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
157 |
private final LoggerContext userContext = new LoggerContext(); |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
158 |
// non final field - make it volatile to make sure that other threads |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
159 |
// will see the new value once ensureLogManagerInitialized() has finished |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
160 |
// executing. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
161 |
private volatile Logger rootLogger; |
2 | 162 |
// Have we done the primordial reading of the configuration file? |
163 |
// (Must be done after a suitable amount of java.lang.System |
|
164 |
// initialization has been done) |
|
165 |
private volatile boolean readPrimordialConfiguration; |
|
166 |
// Have we initialized global (root) handlers yet? |
|
167 |
// This gets set to false in readConfiguration |
|
168 |
private boolean initializedGlobalHandlers = true; |
|
169 |
// True if JVM death is imminent and the exit hook has been called. |
|
170 |
private boolean deathImminent; |
|
171 |
||
26859
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
172 |
private final Map<Object, Runnable> listeners = |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
173 |
Collections.synchronizedMap(new IdentityHashMap<>()); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
174 |
|
2 | 175 |
static { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
176 |
manager = AccessController.doPrivileged(new PrivilegedAction<LogManager>() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
177 |
@Override |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
178 |
public LogManager run() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
179 |
LogManager mgr = null; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
180 |
String cname = null; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
181 |
try { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
182 |
cname = System.getProperty("java.util.logging.manager"); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
183 |
if (cname != null) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
184 |
try { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
185 |
Class<?> clz = ClassLoader.getSystemClassLoader() |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
186 |
.loadClass(cname); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
187 |
mgr = (LogManager) clz.newInstance(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
188 |
} catch (ClassNotFoundException ex) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
189 |
Class<?> clz = Thread.currentThread() |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
190 |
.getContextClassLoader().loadClass(cname); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
191 |
mgr = (LogManager) clz.newInstance(); |
2 | 192 |
} |
193 |
} |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
194 |
} catch (Exception ex) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
195 |
System.err.println("Could not load Logmanager \"" + cname + "\""); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
196 |
ex.printStackTrace(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
197 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
198 |
if (mgr == null) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
199 |
mgr = new LogManager(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
200 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
201 |
return mgr; |
2 | 202 |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
203 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
204 |
}); |
2 | 205 |
} |
206 |
||
207 |
||
208 |
// This private class is used as a shutdown hook. |
|
209 |
// It does a "reset" to close all open handlers. |
|
210 |
private class Cleaner extends Thread { |
|
2384
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
211 |
|
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
212 |
private Cleaner() { |
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
213 |
/* Set context class loader to null in order to avoid |
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
214 |
* keeping a strong reference to an application classloader. |
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
215 |
*/ |
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
216 |
this.setContextClassLoader(null); |
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
217 |
} |
b3ba5fb77f89
6799583: LogManager shutdown hook may cause a memory leak.
bae
parents:
2
diff
changeset
|
218 |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
219 |
@Override |
2 | 220 |
public void run() { |
221 |
// This is to ensure the LogManager.<clinit> is completed |
|
222 |
// before synchronized block. Otherwise deadlocks are possible. |
|
223 |
LogManager mgr = manager; |
|
224 |
||
225 |
// If the global handlers haven't been initialized yet, we |
|
226 |
// don't want to initialize them just so we can close them! |
|
227 |
synchronized (LogManager.this) { |
|
228 |
// Note that death is imminent. |
|
229 |
deathImminent = true; |
|
230 |
initializedGlobalHandlers = true; |
|
231 |
} |
|
232 |
||
233 |
// Do a reset to close all active handlers. |
|
234 |
reset(); |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
||
239 |
/** |
|
240 |
* Protected constructor. This is protected so that container applications |
|
241 |
* (such as J2EE containers) can subclass the object. It is non-public as |
|
242 |
* it is intended that there only be one LogManager object, whose value is |
|
14767
b64db1d425e5
4819681: Typo in http://java.sun.com/j2se/1.4.1/docs/api/java/util/logging/LogManager.html
mchung
parents:
14691
diff
changeset
|
243 |
* retrieved by calling LogManager.getLogManager. |
2 | 244 |
*/ |
245 |
protected LogManager() { |
|
22321 | 246 |
this(checkSubclassPermissions()); |
247 |
} |
|
248 |
||
249 |
private LogManager(Void checked) { |
|
250 |
||
2 | 251 |
// Add a shutdown hook to close the global handlers. |
252 |
try { |
|
253 |
Runtime.getRuntime().addShutdownHook(new Cleaner()); |
|
254 |
} catch (IllegalStateException e) { |
|
255 |
// If the VM is already shutting down, |
|
256 |
// We do not need to register shutdownHook. |
|
257 |
} |
|
258 |
} |
|
259 |
||
22321 | 260 |
private static Void checkSubclassPermissions() { |
261 |
final SecurityManager sm = System.getSecurityManager(); |
|
262 |
if (sm != null) { |
|
263 |
// These permission will be checked in the LogManager constructor, |
|
264 |
// in order to register the Cleaner() thread as a shutdown hook. |
|
265 |
// Check them here to avoid the penalty of constructing the object |
|
266 |
// etc... |
|
267 |
sm.checkPermission(new RuntimePermission("shutdownHooks")); |
|
268 |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
|
269 |
} |
|
270 |
return null; |
|
271 |
} |
|
272 |
||
2 | 273 |
/** |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
274 |
* Lazy initialization: if this instance of manager is the global |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
275 |
* manager then this method will read the initial configuration and |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
276 |
* add the root logger and global logger by calling addLogger(). |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
277 |
* |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
278 |
* Note that it is subtly different from what we do in LoggerContext. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
279 |
* In LoggerContext we're patching up the logger context tree in order to add |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
280 |
* the root and global logger *to the context tree*. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
281 |
* |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
282 |
* For this to work, addLogger() must have already have been called |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
283 |
* once on the LogManager instance for the default logger being |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
284 |
* added. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
285 |
* |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
286 |
* This is why ensureLogManagerInitialized() needs to be called before |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
287 |
* any logger is added to any logger context. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
288 |
* |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
289 |
*/ |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
290 |
private boolean initializedCalled = false; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
291 |
private volatile boolean initializationDone = false; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
292 |
final void ensureLogManagerInitialized() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
293 |
final LogManager owner = this; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
294 |
if (initializationDone || owner != manager) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
295 |
// we don't want to do this twice, and we don't want to do |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
296 |
// this on private manager instances. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
297 |
return; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
298 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
299 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
300 |
// Maybe another thread has called ensureLogManagerInitialized() |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
301 |
// before us and is still executing it. If so we will block until |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
302 |
// the log manager has finished initialized, then acquire the monitor, |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
303 |
// notice that initializationDone is now true and return. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
304 |
// Otherwise - we have come here first! We will acquire the monitor, |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
305 |
// see that initializationDone is still false, and perform the |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
306 |
// initialization. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
307 |
// |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
308 |
synchronized(this) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
309 |
// If initializedCalled is true it means that we're already in |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
310 |
// the process of initializing the LogManager in this thread. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
311 |
// There has been a recursive call to ensureLogManagerInitialized(). |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
312 |
final boolean isRecursiveInitialization = (initializedCalled == true); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
313 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
314 |
assert initializedCalled || !initializationDone |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
315 |
: "Initialization can't be done if initialized has not been called!"; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
316 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
317 |
if (isRecursiveInitialization || initializationDone) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
318 |
// If isRecursiveInitialization is true it means that we're |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
319 |
// already in the process of initializing the LogManager in |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
320 |
// this thread. There has been a recursive call to |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
321 |
// ensureLogManagerInitialized(). We should not proceed as |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
322 |
// it would lead to infinite recursion. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
323 |
// |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
324 |
// If initializationDone is true then it means the manager |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
325 |
// has finished initializing; just return: we're done. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
326 |
return; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
327 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
328 |
// Calling addLogger below will in turn call requiresDefaultLogger() |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
329 |
// which will call ensureLogManagerInitialized(). |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
330 |
// We use initializedCalled to break the recursion. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
331 |
initializedCalled = true; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
332 |
try { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
333 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
334 |
@Override |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
335 |
public Object run() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
336 |
assert rootLogger == null; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
337 |
assert initializedCalled && !initializationDone; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
338 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
339 |
// Read configuration. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
340 |
owner.readPrimordialConfiguration(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
341 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
342 |
// Create and retain Logger for the root of the namespace. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
343 |
owner.rootLogger = owner.new RootLogger(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
344 |
owner.addLogger(owner.rootLogger); |
22086
866b0a7d0127
8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents:
22078
diff
changeset
|
345 |
if (!owner.rootLogger.isLevelInitialized()) { |
866b0a7d0127
8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents:
22078
diff
changeset
|
346 |
owner.rootLogger.setLevel(defaultLevel); |
866b0a7d0127
8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents:
22078
diff
changeset
|
347 |
} |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
348 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
349 |
// Adding the global Logger. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
350 |
// Do not call Logger.getGlobal() here as this might trigger |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
351 |
// subtle inter-dependency issues. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
352 |
@SuppressWarnings("deprecation") |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
353 |
final Logger global = Logger.global; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
354 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
355 |
// Make sure the global logger will be registered in the |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
356 |
// global manager |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
357 |
owner.addLogger(global); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
358 |
return null; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
359 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
360 |
}); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
361 |
} finally { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
362 |
initializationDone = true; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
363 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
364 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
365 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
366 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
367 |
/** |
18565 | 368 |
* Returns the global LogManager object. |
369 |
* @return the global LogManager object |
|
2 | 370 |
*/ |
371 |
public static LogManager getLogManager() { |
|
372 |
if (manager != null) { |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
373 |
manager.ensureLogManagerInitialized(); |
2 | 374 |
} |
375 |
return manager; |
|
376 |
} |
|
377 |
||
378 |
private void readPrimordialConfiguration() { |
|
379 |
if (!readPrimordialConfiguration) { |
|
380 |
synchronized (this) { |
|
381 |
if (!readPrimordialConfiguration) { |
|
382 |
// If System.in/out/err are null, it's a good |
|
383 |
// indication that we're still in the |
|
384 |
// bootstrapping phase |
|
385 |
if (System.out == null) { |
|
386 |
return; |
|
387 |
} |
|
388 |
readPrimordialConfiguration = true; |
|
16098 | 389 |
|
2 | 390 |
try { |
16098 | 391 |
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
392 |
@Override |
16098 | 393 |
public Void run() throws Exception { |
2 | 394 |
readConfiguration(); |
3861
a98a057ec335
6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
3319
diff
changeset
|
395 |
|
a98a057ec335
6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
3319
diff
changeset
|
396 |
// Platform loggers begin to delegate to java.util.logging.Logger |
a98a057ec335
6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents:
3319
diff
changeset
|
397 |
sun.util.logging.PlatformLogger.redirectPlatformLoggers(); |
2 | 398 |
return null; |
399 |
} |
|
400 |
}); |
|
401 |
} catch (Exception ex) { |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
402 |
assert false : "Exception raised while reading logging configuration: " + ex; |
2 | 403 |
} |
404 |
} |
|
405 |
} |
|
406 |
} |
|
407 |
} |
|
408 |
||
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
409 |
// LoggerContext maps from AppContext |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
410 |
private WeakHashMap<Object, LoggerContext> contextsMap = null; |
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
411 |
|
16098 | 412 |
// Returns the LoggerContext for the user code (i.e. application or AppContext). |
413 |
// Loggers are isolated from each AppContext. |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
414 |
private LoggerContext getUserContext() { |
16098 | 415 |
LoggerContext context = null; |
9700
fdbacf68f185
7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents:
9013
diff
changeset
|
416 |
|
16098 | 417 |
SecurityManager sm = System.getSecurityManager(); |
418 |
JavaAWTAccess javaAwtAccess = SharedSecrets.getJavaAWTAccess(); |
|
419 |
if (sm != null && javaAwtAccess != null) { |
|
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
420 |
// for each applet, it has its own LoggerContext isolated from others |
16098 | 421 |
synchronized (javaAwtAccess) { |
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
422 |
// find the AppContext of the applet code |
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
423 |
// will be null if we are in the main app context. |
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
424 |
final Object ecx = javaAwtAccess.getAppletContext(); |
18202 | 425 |
if (ecx != null) { |
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
426 |
if (contextsMap == null) { |
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
427 |
contextsMap = new WeakHashMap<>(); |
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
428 |
} |
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
429 |
context = contextsMap.get(ecx); |
18202 | 430 |
if (context == null) { |
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
431 |
// Create a new LoggerContext for the applet. |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
432 |
context = new LoggerContext(); |
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
433 |
contextsMap.put(ecx, context); |
16098 | 434 |
} |
435 |
} |
|
436 |
} |
|
437 |
} |
|
19809
dc0480c1b36c
8019853: Break logging and AWT circular dependency
dfuchs
parents:
18595
diff
changeset
|
438 |
// for standalone app, return userContext |
18202 | 439 |
return context != null ? context : userContext; |
16098 | 440 |
} |
9700
fdbacf68f185
7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents:
9013
diff
changeset
|
441 |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
442 |
// The system context. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
443 |
final LoggerContext getSystemContext() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
444 |
return systemContext; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
445 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
446 |
|
16098 | 447 |
private List<LoggerContext> contexts() { |
448 |
List<LoggerContext> cxs = new ArrayList<>(); |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
449 |
cxs.add(getSystemContext()); |
16098 | 450 |
cxs.add(getUserContext()); |
451 |
return cxs; |
|
2 | 452 |
} |
453 |
||
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
454 |
// Find or create a specified logger instance. If a logger has |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
455 |
// already been created with the given name it is returned. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
456 |
// Otherwise a new logger instance is created and registered |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
457 |
// in the LogManager global namespace. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
458 |
// This method will always return a non-null Logger object. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
459 |
// Synchronization is not required here. All synchronization for |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
460 |
// adding a new Logger object is handled by addLogger(). |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
461 |
// |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
462 |
// This method must delegate to the LogManager implementation to |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
463 |
// add a new Logger or return the one that has been added previously |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
464 |
// as a LogManager subclass may override the addLogger, getLogger, |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
465 |
// readConfiguration, and other methods. |
17487
77566e5979d1
8013380: Removal of stack walk to find resource bundle breaks Glassfish startup
jgish
parents:
17164
diff
changeset
|
466 |
Logger demandLogger(String name, String resourceBundleName, Class<?> caller) { |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
467 |
Logger result = getLogger(name); |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
468 |
if (result == null) { |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
469 |
// only allocate the new logger once |
23899 | 470 |
Logger newLogger = new Logger(name, resourceBundleName, caller, this, false); |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
471 |
do { |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
472 |
if (addLogger(newLogger)) { |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
473 |
// We successfully added the new Logger that we |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
474 |
// created above so return it without refetching. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
475 |
return newLogger; |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
476 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
477 |
|
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
478 |
// We didn't add the new Logger that we created above |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
479 |
// because another thread added a Logger with the same |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
480 |
// name after our null check above and before our call |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
481 |
// to addLogger(). We have to refetch the Logger because |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
482 |
// addLogger() returns a boolean instead of the Logger |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
483 |
// reference itself. However, if the thread that created |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
484 |
// the other Logger is not holding a strong reference to |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
485 |
// the other Logger, then it is possible for the other |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
486 |
// Logger to be GC'ed after we saw it in addLogger() and |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
487 |
// before we can refetch it. If it has been GC'ed then |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
488 |
// we'll just loop around and try again. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
489 |
result = getLogger(name); |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
490 |
} while (result == null); |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
491 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
492 |
return result; |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
493 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
494 |
|
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
495 |
Logger demandSystemLogger(String name, String resourceBundleName) { |
16111
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
496 |
// Add a system logger in the system context's namespace |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
497 |
final Logger sysLogger = getSystemContext().demandLogger(name, resourceBundleName); |
16111
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
498 |
|
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
499 |
// Add the system logger to the LogManager's namespace if not exist |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
500 |
// so that there is only one single logger of the given name. |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
501 |
// System loggers are visible to applications unless a logger of |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
502 |
// the same name has been added. |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
503 |
Logger logger; |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
504 |
do { |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
505 |
// First attempt to call addLogger instead of getLogger |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
506 |
// This would avoid potential bug in custom LogManager.getLogger |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
507 |
// implementation that adds a logger if does not exist |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
508 |
if (addLogger(sysLogger)) { |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
509 |
// successfully added the new system logger |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
510 |
logger = sysLogger; |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
511 |
} else { |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
512 |
logger = getLogger(name); |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
513 |
} |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
514 |
} while (logger == null); |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
515 |
|
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
516 |
// LogManager will set the sysLogger's handlers via LogManager.addLogger method. |
23899 | 517 |
if (logger != sysLogger && sysLogger.accessCheckedHandlers().length == 0) { |
16111
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
518 |
// if logger already exists but handlers not set |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
519 |
final Logger l = logger; |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
520 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
521 |
@Override |
16111
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
522 |
public Void run() { |
23899 | 523 |
for (Handler hdl : l.accessCheckedHandlers()) { |
16111
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
524 |
sysLogger.addHandler(hdl); |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
525 |
} |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
526 |
return null; |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
527 |
} |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
528 |
}); |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
529 |
} |
5c5fc1d5bb38
8007393: Possible race condition after JDK-6664509
mchung
parents:
16105
diff
changeset
|
530 |
return sysLogger; |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
531 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
532 |
|
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
533 |
// LoggerContext maintains the logger namespace per context. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
534 |
// The default LogManager implementation has one system context and user |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
535 |
// context. The system context is used to maintain the namespace for |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
536 |
// all system loggers and is queried by the system code. If a system logger |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
537 |
// doesn't exist in the user context, it'll also be added to the user context. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
538 |
// The user context is queried by the user code and all other loggers are |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
539 |
// added in the user context. |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
540 |
class LoggerContext { |
16098 | 541 |
// Table of named Loggers that maps names to Loggers. |
542 |
private final Hashtable<String,LoggerWeakRef> namedLoggers = new Hashtable<>(); |
|
543 |
// Tree of named Loggers |
|
544 |
private final LogNode root; |
|
545 |
private LoggerContext() { |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
546 |
this.root = new LogNode(null, this); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
547 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
548 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
549 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
550 |
// Tells whether default loggers are required in this context. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
551 |
// If true, the default loggers will be lazily added. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
552 |
final boolean requiresDefaultLoggers() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
553 |
final boolean requiresDefaultLoggers = (getOwner() == manager); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
554 |
if (requiresDefaultLoggers) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
555 |
getOwner().ensureLogManagerInitialized(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
556 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
557 |
return requiresDefaultLoggers; |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
558 |
} |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
559 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
560 |
// This context's LogManager. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
561 |
final LogManager getOwner() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
562 |
return LogManager.this; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
563 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
564 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
565 |
// This context owner's root logger, which if not null, and if |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
566 |
// the context requires default loggers, will be added to the context |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
567 |
// logger's tree. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
568 |
final Logger getRootLogger() { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
569 |
return getOwner().rootLogger; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
570 |
} |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
571 |
|
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
572 |
// The global logger, which if not null, and if |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
573 |
// the context requires default loggers, will be added to the context |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
574 |
// logger's tree. |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
575 |
final Logger getGlobalLogger() { |
23342 | 576 |
@SuppressWarnings("deprecation") // avoids initialization cycles. |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
577 |
final Logger global = Logger.global; |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
578 |
return global; |
16098 | 579 |
} |
580 |
||
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
581 |
Logger demandLogger(String name, String resourceBundleName) { |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
582 |
// a LogManager subclass may have its own implementation to add and |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
583 |
// get a Logger. So delegate to the LogManager to do the work. |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
584 |
final LogManager owner = getOwner(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
585 |
return owner.demandLogger(name, resourceBundleName, null); |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
586 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
587 |
|
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
588 |
|
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
589 |
// Due to subtle deadlock issues getUserContext() no longer |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
590 |
// calls addLocalLogger(rootLogger); |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
591 |
// Therefore - we need to add the default loggers later on. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
592 |
// Checks that the context is properly initialized |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
593 |
// This is necessary before calling e.g. find(name) |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
594 |
// or getLoggerNames() |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
595 |
// |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
596 |
private void ensureInitialized() { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
597 |
if (requiresDefaultLoggers()) { |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
598 |
// Ensure that the root and global loggers are set. |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
599 |
ensureDefaultLogger(getRootLogger()); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
600 |
ensureDefaultLogger(getGlobalLogger()); |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
601 |
} |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
602 |
} |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
603 |
|
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
604 |
|
16098 | 605 |
synchronized Logger findLogger(String name) { |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
606 |
// ensure that this context is properly initialized before |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
607 |
// looking for loggers. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
608 |
ensureInitialized(); |
16098 | 609 |
LoggerWeakRef ref = namedLoggers.get(name); |
610 |
if (ref == null) { |
|
611 |
return null; |
|
612 |
} |
|
613 |
Logger logger = ref.get(); |
|
614 |
if (logger == null) { |
|
615 |
// Hashtable holds stale weak reference |
|
616 |
// to a logger which has been GC-ed. |
|
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
617 |
ref.dispose(); |
16098 | 618 |
} |
619 |
return logger; |
|
620 |
} |
|
621 |
||
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
622 |
// This method is called before adding a logger to the |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
623 |
// context. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
624 |
// 'logger' is the context that will be added. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
625 |
// This method will ensure that the defaults loggers are added |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
626 |
// before adding 'logger'. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
627 |
// |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
628 |
private void ensureAllDefaultLoggers(Logger logger) { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
629 |
if (requiresDefaultLoggers()) { |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
630 |
final String name = logger.getName(); |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
631 |
if (!name.isEmpty()) { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
632 |
ensureDefaultLogger(getRootLogger()); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
633 |
if (!Logger.GLOBAL_LOGGER_NAME.equals(name)) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
634 |
ensureDefaultLogger(getGlobalLogger()); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
635 |
} |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
636 |
} |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
637 |
} |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
638 |
} |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
639 |
|
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
640 |
private void ensureDefaultLogger(Logger logger) { |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
641 |
// Used for lazy addition of root logger and global logger |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
642 |
// to a LoggerContext. |
18202 | 643 |
|
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
644 |
// This check is simple sanity: we do not want that this |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
645 |
// method be called for anything else than Logger.global |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
646 |
// or owner.rootLogger. |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
647 |
if (!requiresDefaultLoggers() || logger == null |
23342 | 648 |
|| logger != getGlobalLogger() && logger != LogManager.this.rootLogger ) { |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
649 |
|
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
650 |
// the case where we have a non null logger which is neither |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
651 |
// Logger.global nor manager.rootLogger indicates a serious |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
652 |
// issue - as ensureDefaultLogger should never be called |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
653 |
// with any other loggers than one of these two (or null - if |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
654 |
// e.g manager.rootLogger is not yet initialized)... |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
655 |
assert logger == null; |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
656 |
|
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
657 |
return; |
18202 | 658 |
} |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
659 |
|
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
660 |
// Adds the logger if it's not already there. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
661 |
if (!namedLoggers.containsKey(logger.getName())) { |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
662 |
// It is important to prevent addLocalLogger to |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
663 |
// call ensureAllDefaultLoggers when we're in the process |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
664 |
// off adding one of those default loggers - as this would |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
665 |
// immediately cause a stack overflow. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
666 |
// Therefore we must pass addDefaultLoggersIfNeeded=false, |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
667 |
// even if requiresDefaultLoggers is true. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
668 |
addLocalLogger(logger, false); |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
669 |
} |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
670 |
} |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
671 |
|
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
672 |
boolean addLocalLogger(Logger logger) { |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
673 |
// no need to add default loggers if it's not required |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
674 |
return addLocalLogger(logger, requiresDefaultLoggers()); |
18202 | 675 |
} |
676 |
||
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
677 |
// Add a logger to this context. This method will only set its level |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
678 |
// and process parent loggers. It doesn't set its handlers. |
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
679 |
synchronized boolean addLocalLogger(Logger logger, boolean addDefaultLoggersIfNeeded) { |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
680 |
// addDefaultLoggersIfNeeded serves to break recursion when adding |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
681 |
// default loggers. If we're adding one of the default loggers |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
682 |
// (we're being called from ensureDefaultLogger()) then |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
683 |
// addDefaultLoggersIfNeeded will be false: we don't want to |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
684 |
// call ensureAllDefaultLoggers again. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
685 |
// |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
686 |
// Note: addDefaultLoggersIfNeeded can also be false when |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
687 |
// requiresDefaultLoggers is false - since calling |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
688 |
// ensureAllDefaultLoggers would have no effect in this case. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
689 |
if (addDefaultLoggersIfNeeded) { |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
690 |
ensureAllDefaultLoggers(logger); |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
691 |
} |
18202 | 692 |
|
16098 | 693 |
final String name = logger.getName(); |
694 |
if (name == null) { |
|
695 |
throw new NullPointerException(); |
|
696 |
} |
|
697 |
LoggerWeakRef ref = namedLoggers.get(name); |
|
698 |
if (ref != null) { |
|
699 |
if (ref.get() == null) { |
|
17164 | 700 |
// It's possible that the Logger was GC'ed after a |
16098 | 701 |
// drainLoggerRefQueueBounded() call above so allow |
702 |
// a new one to be registered. |
|
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
703 |
ref.dispose(); |
16098 | 704 |
} else { |
705 |
// We already have a registered logger with the given name. |
|
706 |
return false; |
|
707 |
} |
|
708 |
} |
|
709 |
||
710 |
// We're adding a new logger. |
|
711 |
// Note that we are creating a weak reference here. |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
712 |
final LogManager owner = getOwner(); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
713 |
logger.setLogManager(owner); |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
714 |
ref = owner.new LoggerWeakRef(logger); |
16098 | 715 |
namedLoggers.put(name, ref); |
716 |
||
21304
7971ecf0fbed
8026499: Root Logger level can be reset unexpectedly
dfuchs
parents:
19825
diff
changeset
|
717 |
// Apply any initial level defined for the new logger, unless |
7971ecf0fbed
8026499: Root Logger level can be reset unexpectedly
dfuchs
parents:
19825
diff
changeset
|
718 |
// the logger's level is already initialized |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
719 |
Level level = owner.getLevelProperty(name + ".level", null); |
21304
7971ecf0fbed
8026499: Root Logger level can be reset unexpectedly
dfuchs
parents:
19825
diff
changeset
|
720 |
if (level != null && !logger.isLevelInitialized()) { |
16098 | 721 |
doSetLevel(logger, level); |
722 |
} |
|
723 |
||
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
724 |
// instantiation of the handler is done in the LogManager.addLogger |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
725 |
// implementation as a handler class may be only visible to LogManager |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
726 |
// subclass for the custom log manager case |
16098 | 727 |
processParentHandlers(logger, name); |
728 |
||
729 |
// Find the new node and its parent. |
|
730 |
LogNode node = getNode(name); |
|
731 |
node.loggerRef = ref; |
|
732 |
Logger parent = null; |
|
733 |
LogNode nodep = node.parent; |
|
734 |
while (nodep != null) { |
|
735 |
LoggerWeakRef nodeRef = nodep.loggerRef; |
|
736 |
if (nodeRef != null) { |
|
737 |
parent = nodeRef.get(); |
|
738 |
if (parent != null) { |
|
739 |
break; |
|
740 |
} |
|
741 |
} |
|
742 |
nodep = nodep.parent; |
|
743 |
} |
|
744 |
||
745 |
if (parent != null) { |
|
746 |
doSetParent(logger, parent); |
|
2 | 747 |
} |
16098 | 748 |
// Walk over the children and tell them we are their new parent. |
749 |
node.walkAndSetParent(logger); |
|
750 |
// new LogNode is ready so tell the LoggerWeakRef about it |
|
751 |
ref.setNode(node); |
|
752 |
return true; |
|
753 |
} |
|
754 |
||
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
755 |
synchronized void removeLoggerRef(String name, LoggerWeakRef ref) { |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
756 |
namedLoggers.remove(name, ref); |
16098 | 757 |
} |
758 |
||
759 |
synchronized Enumeration<String> getLoggerNames() { |
|
18593
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
760 |
// ensure that this context is properly initialized before |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
761 |
// returning logger names. |
40715ebb6681
8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents:
18565
diff
changeset
|
762 |
ensureInitialized(); |
16098 | 763 |
return namedLoggers.keys(); |
764 |
} |
|
765 |
||
766 |
// If logger.getUseParentHandlers() returns 'true' and any of the logger's |
|
767 |
// parents have levels or handlers defined, make sure they are instantiated. |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
768 |
private void processParentHandlers(final Logger logger, final String name) { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
769 |
final LogManager owner = getOwner(); |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
770 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
771 |
@Override |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
772 |
public Void run() { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
773 |
if (logger != owner.rootLogger) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
774 |
boolean useParent = owner.getBooleanProperty(name + ".useParentHandlers", true); |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
775 |
if (!useParent) { |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
776 |
logger.setUseParentHandlers(false); |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
777 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
778 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
779 |
return null; |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
780 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
781 |
}); |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
782 |
|
16098 | 783 |
int ix = 1; |
784 |
for (;;) { |
|
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
24196
diff
changeset
|
785 |
int ix2 = name.indexOf('.', ix); |
16098 | 786 |
if (ix2 < 0) { |
787 |
break; |
|
788 |
} |
|
789 |
String pname = name.substring(0, ix2); |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
790 |
if (owner.getProperty(pname + ".level") != null || |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
791 |
owner.getProperty(pname + ".handlers") != null) { |
16098 | 792 |
// This pname has a level/handlers definition. |
793 |
// Make sure it exists. |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
794 |
demandLogger(pname, null); |
16098 | 795 |
} |
796 |
ix = ix2+1; |
|
797 |
} |
|
798 |
} |
|
2 | 799 |
|
16098 | 800 |
// Gets a node in our tree of logger nodes. |
801 |
// If necessary, create it. |
|
802 |
LogNode getNode(String name) { |
|
803 |
if (name == null || name.equals("")) { |
|
804 |
return root; |
|
805 |
} |
|
806 |
LogNode node = root; |
|
807 |
while (name.length() > 0) { |
|
24685
215fa91e1b4c
8044461: Cleanup new Boolean and single character strings
rriggs
parents:
24196
diff
changeset
|
808 |
int ix = name.indexOf('.'); |
16098 | 809 |
String head; |
810 |
if (ix > 0) { |
|
811 |
head = name.substring(0, ix); |
|
812 |
name = name.substring(ix + 1); |
|
813 |
} else { |
|
814 |
head = name; |
|
815 |
name = ""; |
|
816 |
} |
|
817 |
if (node.children == null) { |
|
818 |
node.children = new HashMap<>(); |
|
819 |
} |
|
820 |
LogNode child = node.children.get(head); |
|
821 |
if (child == null) { |
|
822 |
child = new LogNode(node, this); |
|
823 |
node.children.put(head, child); |
|
824 |
} |
|
825 |
node = child; |
|
2 | 826 |
} |
16098 | 827 |
return node; |
828 |
} |
|
829 |
} |
|
830 |
||
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
831 |
final class SystemLoggerContext extends LoggerContext { |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
832 |
// Add a system logger in the system context's namespace as well as |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
833 |
// in the LogManager's namespace if not exist so that there is only |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
834 |
// one single logger of the given name. System loggers are visible |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
835 |
// to applications unless a logger of the same name has been added. |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
836 |
@Override |
16098 | 837 |
Logger demandLogger(String name, String resourceBundleName) { |
838 |
Logger result = findLogger(name); |
|
839 |
if (result == null) { |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
840 |
// only allocate the new system logger once |
23899 | 841 |
Logger newLogger = new Logger(name, resourceBundleName, null, getOwner(), true); |
16098 | 842 |
do { |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
843 |
if (addLocalLogger(newLogger)) { |
16098 | 844 |
// We successfully added the new Logger that we |
845 |
// created above so return it without refetching. |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
846 |
result = newLogger; |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
847 |
} else { |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
848 |
// We didn't add the new Logger that we created above |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
849 |
// because another thread added a Logger with the same |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
850 |
// name after our null check above and before our call |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
851 |
// to addLogger(). We have to refetch the Logger because |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
852 |
// addLogger() returns a boolean instead of the Logger |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
853 |
// reference itself. However, if the thread that created |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
854 |
// the other Logger is not holding a strong reference to |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
855 |
// the other Logger, then it is possible for the other |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
856 |
// Logger to be GC'ed after we saw it in addLogger() and |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
857 |
// before we can refetch it. If it has been GC'ed then |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
858 |
// we'll just loop around and try again. |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
859 |
result = findLogger(name); |
16098 | 860 |
} |
861 |
} while (result == null); |
|
862 |
} |
|
863 |
return result; |
|
2 | 864 |
} |
865 |
} |
|
866 |
||
867 |
// Add new per logger handlers. |
|
868 |
// We need to raise privilege here. All our decisions will |
|
869 |
// be made based on the logging configuration, which can |
|
870 |
// only be modified by trusted code. |
|
871 |
private void loadLoggerHandlers(final Logger logger, final String name, |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
872 |
final String handlersPropertyName) |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
873 |
{ |
2 | 874 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
875 |
@Override |
2 | 876 |
public Object run() { |
877 |
String names[] = parseClassNames(handlersPropertyName); |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
22046
diff
changeset
|
878 |
for (String word : names) { |
2 | 879 |
try { |
11274
7e7196757acd
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents:
9700
diff
changeset
|
880 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word); |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
881 |
Handler hdl = (Handler) clz.newInstance(); |
16098 | 882 |
// Check if there is a property defining the |
883 |
// this handler's level. |
|
884 |
String levs = getProperty(word + ".level"); |
|
885 |
if (levs != null) { |
|
886 |
Level l = Level.findLevel(levs); |
|
887 |
if (l != null) { |
|
888 |
hdl.setLevel(l); |
|
889 |
} else { |
|
890 |
// Probably a bad level. Drop through. |
|
891 |
System.err.println("Can't set level for " + word); |
|
2 | 892 |
} |
893 |
} |
|
894 |
// Add this Handler to the logger |
|
895 |
logger.addHandler(hdl); |
|
896 |
} catch (Exception ex) { |
|
897 |
System.err.println("Can't load log handler \"" + word + "\""); |
|
898 |
System.err.println("" + ex); |
|
899 |
ex.printStackTrace(); |
|
900 |
} |
|
901 |
} |
|
902 |
return null; |
|
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
903 |
} |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
904 |
}); |
2 | 905 |
} |
906 |
||
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
907 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
908 |
// loggerRefQueue holds LoggerWeakRef objects for Logger objects |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
909 |
// that have been GC'ed. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
910 |
private final ReferenceQueue<Logger> loggerRefQueue |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
6675
diff
changeset
|
911 |
= new ReferenceQueue<>(); |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
912 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
913 |
// Package-level inner class. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
914 |
// Helper class for managing WeakReferences to Logger objects. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
915 |
// |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
916 |
// LogManager.namedLoggers |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
917 |
// - has weak references to all named Loggers |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
918 |
// - namedLoggers keeps the LoggerWeakRef objects for the named |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
919 |
// Loggers around until we can deal with the book keeping for |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
920 |
// the named Logger that is being GC'ed. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
921 |
// LogManager.LogNode.loggerRef |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
922 |
// - has a weak reference to a named Logger |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
923 |
// - the LogNode will also keep the LoggerWeakRef objects for |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
924 |
// the named Loggers around; currently LogNodes never go away. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
925 |
// Logger.kids |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
926 |
// - has a weak reference to each direct child Logger; this |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
927 |
// includes anonymous and named Loggers |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
928 |
// - anonymous Loggers are always children of the rootLogger |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
929 |
// which is a strong reference; rootLogger.kids keeps the |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
930 |
// LoggerWeakRef objects for the anonymous Loggers around |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
931 |
// until we can deal with the book keeping. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
932 |
// |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
933 |
final class LoggerWeakRef extends WeakReference<Logger> { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
934 |
private String name; // for namedLoggers cleanup |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
935 |
private LogNode node; // for loggerRef cleanup |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
936 |
private WeakReference<Logger> parentRef; // for kids cleanup |
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
937 |
private boolean disposed = false; // avoid calling dispose twice |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
938 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
939 |
LoggerWeakRef(Logger logger) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
940 |
super(logger, loggerRefQueue); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
941 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
942 |
name = logger.getName(); // save for namedLoggers cleanup |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
943 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
944 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
945 |
// dispose of this LoggerWeakRef object |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
946 |
void dispose() { |
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
947 |
// Avoid calling dispose twice. When a Logger is gc'ed, its |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
948 |
// LoggerWeakRef will be enqueued. |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
949 |
// However, a new logger of the same name may be added (or looked |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
950 |
// up) before the queue is drained. When that happens, dispose() |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
951 |
// will be called by addLocalLogger() or findLogger(). |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
952 |
// Later when the queue is drained, dispose() will be called again |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
953 |
// for the same LoggerWeakRef. Marking LoggerWeakRef as disposed |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
954 |
// avoids processing the data twice (even though the code should |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
955 |
// now be reentrant). |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
956 |
synchronized(this) { |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
957 |
// Note to maintainers: |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
958 |
// Be careful not to call any method that tries to acquire |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
959 |
// another lock from within this block - as this would surely |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
960 |
// lead to deadlocks, given that dispose() can be called by |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
961 |
// multiple threads, and from within different synchronized |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
962 |
// methods/blocks. |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
963 |
if (disposed) return; |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
964 |
disposed = true; |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
965 |
} |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
966 |
|
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
967 |
final LogNode n = node; |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
968 |
if (n != null) { |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
969 |
// n.loggerRef can only be safely modified from within |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
970 |
// a lock on LoggerContext. removeLoggerRef is already |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
971 |
// synchronized on LoggerContext so calling |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
972 |
// n.context.removeLoggerRef from within this lock is safe. |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
973 |
synchronized (n.context) { |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
974 |
// if we have a LogNode, then we were a named Logger |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
975 |
// so clear namedLoggers weak ref to us |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
976 |
n.context.removeLoggerRef(name, this); |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
977 |
name = null; // clear our ref to the Logger's name |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
978 |
|
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
979 |
// LogNode may have been reused - so only clear |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
980 |
// LogNode.loggerRef if LogNode.loggerRef == this |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
981 |
if (n.loggerRef == this) { |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
982 |
n.loggerRef = null; // clear LogNode's weak ref to us |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
983 |
} |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
984 |
node = null; // clear our ref to LogNode |
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
985 |
} |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
986 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
987 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
988 |
if (parentRef != null) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
989 |
// this LoggerWeakRef has or had a parent Logger |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
990 |
Logger parent = parentRef.get(); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
991 |
if (parent != null) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
992 |
// the parent Logger is still there so clear the |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
993 |
// parent Logger's weak ref to us |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
994 |
parent.removeChildLogger(this); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
995 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
996 |
parentRef = null; // clear our weak ref to the parent Logger |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
997 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
998 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
999 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1000 |
// set the node field to the specified value |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1001 |
void setNode(LogNode node) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1002 |
this.node = node; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1003 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1004 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1005 |
// set the parentRef field to the specified value |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1006 |
void setParentRef(WeakReference<Logger> parentRef) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1007 |
this.parentRef = parentRef; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1008 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1009 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1010 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1011 |
// Package-level method. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1012 |
// Drain some Logger objects that have been GC'ed. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1013 |
// |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1014 |
// drainLoggerRefQueueBounded() is called by addLogger() below |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1015 |
// and by Logger.getAnonymousLogger(String) so we'll drain up to |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1016 |
// MAX_ITERATIONS GC'ed Loggers for every Logger we add. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1017 |
// |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1018 |
// On a WinXP VMware client, a MAX_ITERATIONS value of 400 gives |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1019 |
// us about a 50/50 mix in increased weak ref counts versus |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1020 |
// decreased weak ref counts in the AnonLoggerWeakRefLeak test. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1021 |
// Here are stats for cleaning up sets of 400 anonymous Loggers: |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1022 |
// - test duration 1 minute |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1023 |
// - sample size of 125 sets of 400 |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1024 |
// - average: 1.99 ms |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1025 |
// - minimum: 0.57 ms |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1026 |
// - maximum: 25.3 ms |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1027 |
// |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1028 |
// The same config gives us a better decreased weak ref count |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1029 |
// than increased weak ref count in the LoggerWeakRefLeak test. |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1030 |
// Here are stats for cleaning up sets of 400 named Loggers: |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1031 |
// - test duration 2 minutes |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1032 |
// - sample size of 506 sets of 400 |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1033 |
// - average: 0.57 ms |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1034 |
// - minimum: 0.02 ms |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1035 |
// - maximum: 10.9 ms |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1036 |
// |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1037 |
private final static int MAX_ITERATIONS = 400; |
21960
277d5c6b2172
8029281: Synchronization issues in Logger and LogManager
dfuchs
parents:
21304
diff
changeset
|
1038 |
final void drainLoggerRefQueueBounded() { |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1039 |
for (int i = 0; i < MAX_ITERATIONS; i++) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1040 |
if (loggerRefQueue == null) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1041 |
// haven't finished loading LogManager yet |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1042 |
break; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1043 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1044 |
|
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1045 |
LoggerWeakRef ref = (LoggerWeakRef) loggerRefQueue.poll(); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1046 |
if (ref == null) { |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1047 |
break; |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1048 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1049 |
// a Logger object has been GC'ed so clean it up |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1050 |
ref.dispose(); |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1051 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1052 |
} |
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1053 |
|
2 | 1054 |
/** |
1055 |
* Add a named logger. This does nothing and returns false if a logger |
|
1056 |
* with the same name is already registered. |
|
1057 |
* <p> |
|
1058 |
* The Logger factory methods call this method to register each |
|
1059 |
* newly created Logger. |
|
1060 |
* <p> |
|
1061 |
* The application should retain its own reference to the Logger |
|
1062 |
* object to avoid it being garbage collected. The LogManager |
|
1063 |
* may only retain a weak reference. |
|
1064 |
* |
|
1065 |
* @param logger the new logger. |
|
1066 |
* @return true if the argument logger was registered successfully, |
|
1067 |
* false if a logger of that name already exists. |
|
1068 |
* @exception NullPointerException if the logger name is null. |
|
1069 |
*/ |
|
16098 | 1070 |
public boolean addLogger(Logger logger) { |
2 | 1071 |
final String name = logger.getName(); |
1072 |
if (name == null) { |
|
1073 |
throw new NullPointerException(); |
|
1074 |
} |
|
17164 | 1075 |
drainLoggerRefQueueBounded(); |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1076 |
LoggerContext cx = getUserContext(); |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1077 |
if (cx.addLocalLogger(logger)) { |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1078 |
// Do we have a per logger handler too? |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1079 |
// Note: this will add a 200ms penalty |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1080 |
loadLoggerHandlers(logger, name, name + ".handlers"); |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1081 |
return true; |
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1082 |
} else { |
16098 | 1083 |
return false; |
2 | 1084 |
} |
1085 |
} |
|
1086 |
||
1087 |
// Private method to set a level on a logger. |
|
1088 |
// If necessary, we raise privilege before doing the call. |
|
1089 |
private static void doSetLevel(final Logger logger, final Level level) { |
|
1090 |
SecurityManager sm = System.getSecurityManager(); |
|
1091 |
if (sm == null) { |
|
1092 |
// There is no security manager, so things are easy. |
|
1093 |
logger.setLevel(level); |
|
1094 |
return; |
|
1095 |
} |
|
1096 |
// There is a security manager. Raise privilege before |
|
1097 |
// calling setLevel. |
|
1098 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1099 |
@Override |
2 | 1100 |
public Object run() { |
1101 |
logger.setLevel(level); |
|
1102 |
return null; |
|
1103 |
}}); |
|
1104 |
} |
|
1105 |
||
1106 |
// Private method to set a parent on a logger. |
|
1107 |
// If necessary, we raise privilege before doing the setParent call. |
|
1108 |
private static void doSetParent(final Logger logger, final Logger parent) { |
|
1109 |
SecurityManager sm = System.getSecurityManager(); |
|
1110 |
if (sm == null) { |
|
1111 |
// There is no security manager, so things are easy. |
|
1112 |
logger.setParent(parent); |
|
1113 |
return; |
|
1114 |
} |
|
1115 |
// There is a security manager. Raise privilege before |
|
1116 |
// calling setParent. |
|
1117 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1118 |
@Override |
2 | 1119 |
public Object run() { |
1120 |
logger.setParent(parent); |
|
1121 |
return null; |
|
1122 |
}}); |
|
1123 |
} |
|
1124 |
||
1125 |
/** |
|
1126 |
* Method to find a named logger. |
|
1127 |
* <p> |
|
1128 |
* Note that since untrusted code may create loggers with |
|
1129 |
* arbitrary names this method should not be relied on to |
|
1130 |
* find Loggers for security sensitive logging. |
|
6675
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1131 |
* It is also important to note that the Logger associated with the |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1132 |
* String {@code name} may be garbage collected at any time if there |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1133 |
* is no strong reference to the Logger. The caller of this method |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1134 |
* must check the return value for null in order to properly handle |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1135 |
* the case where the Logger has been garbage collected. |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
23899
diff
changeset
|
1136 |
* |
2 | 1137 |
* @param name name of the logger |
1138 |
* @return matching logger or null if none is found |
|
1139 |
*/ |
|
16098 | 1140 |
public Logger getLogger(String name) { |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1141 |
return getUserContext().findLogger(name); |
2 | 1142 |
} |
1143 |
||
1144 |
/** |
|
1145 |
* Get an enumeration of known logger names. |
|
1146 |
* <p> |
|
1147 |
* Note: Loggers may be added dynamically as new classes are loaded. |
|
1148 |
* This method only reports on the loggers that are currently registered. |
|
6675
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1149 |
* It is also important to note that this method only returns the name |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1150 |
* of a Logger, not a strong reference to the Logger itself. |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1151 |
* The returned String does nothing to prevent the Logger from being |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1152 |
* garbage collected. In particular, if the returned name is passed |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1153 |
* to {@code LogManager.getLogger()}, then the caller must check the |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1154 |
* return value from {@code LogManager.getLogger()} for null to properly |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1155 |
* handle the case where the Logger has been garbage collected in the |
c86763d8f1c7
6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents:
5964
diff
changeset
|
1156 |
* time since its name was returned by this method. |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
23899
diff
changeset
|
1157 |
* |
2 | 1158 |
* @return enumeration of logger name strings |
1159 |
*/ |
|
16098 | 1160 |
public Enumeration<String> getLoggerNames() { |
16105
fe7392acb767
8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents:
16100
diff
changeset
|
1161 |
return getUserContext().getLoggerNames(); |
2 | 1162 |
} |
1163 |
||
1164 |
/** |
|
1165 |
* Reinitialize the logging properties and reread the logging configuration. |
|
1166 |
* <p> |
|
1167 |
* The same rules are used for locating the configuration properties |
|
1168 |
* as are used at startup. So normally the logging properties will |
|
1169 |
* be re-read from the same file that was used at startup. |
|
1170 |
* <P> |
|
1171 |
* Any log level definitions in the new configuration file will be |
|
1172 |
* applied using Logger.setLevel(), if the target Logger exists. |
|
1173 |
* <p> |
|
26859
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1174 |
* Any {@linkplain #addConfigurationListener registered configuration |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1175 |
* listener} will be invoked after the properties are read. |
2 | 1176 |
* |
1177 |
* @exception SecurityException if a security manager exists and if |
|
1178 |
* the caller does not have LoggingPermission("control"). |
|
1179 |
* @exception IOException if there are IO problems reading the configuration. |
|
1180 |
*/ |
|
1181 |
public void readConfiguration() throws IOException, SecurityException { |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1182 |
checkPermission(); |
2 | 1183 |
|
1184 |
// if a configuration class is specified, load it and use it. |
|
1185 |
String cname = System.getProperty("java.util.logging.config.class"); |
|
1186 |
if (cname != null) { |
|
1187 |
try { |
|
1188 |
// Instantiate the named class. It is its constructor's |
|
1189 |
// responsibility to initialize the logging configuration, by |
|
1190 |
// calling readConfiguration(InputStream) with a suitable stream. |
|
1191 |
try { |
|
11274
7e7196757acd
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents:
9700
diff
changeset
|
1192 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname); |
2 | 1193 |
clz.newInstance(); |
1194 |
return; |
|
1195 |
} catch (ClassNotFoundException ex) { |
|
11274
7e7196757acd
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents:
9700
diff
changeset
|
1196 |
Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname); |
2 | 1197 |
clz.newInstance(); |
1198 |
return; |
|
1199 |
} |
|
1200 |
} catch (Exception ex) { |
|
1201 |
System.err.println("Logging configuration class \"" + cname + "\" failed"); |
|
1202 |
System.err.println("" + ex); |
|
1203 |
// keep going and useful config file. |
|
1204 |
} |
|
1205 |
} |
|
1206 |
||
1207 |
String fname = System.getProperty("java.util.logging.config.file"); |
|
1208 |
if (fname == null) { |
|
1209 |
fname = System.getProperty("java.home"); |
|
1210 |
if (fname == null) { |
|
1211 |
throw new Error("Can't find java.home ??"); |
|
1212 |
} |
|
1213 |
File f = new File(fname, "lib"); |
|
1214 |
f = new File(f, "logging.properties"); |
|
1215 |
fname = f.getCanonicalPath(); |
|
1216 |
} |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1217 |
try (final InputStream in = new FileInputStream(fname)) { |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1218 |
final BufferedInputStream bin = new BufferedInputStream(in); |
2 | 1219 |
readConfiguration(bin); |
1220 |
} |
|
1221 |
} |
|
1222 |
||
1223 |
/** |
|
1224 |
* Reset the logging configuration. |
|
1225 |
* <p> |
|
1226 |
* For all named loggers, the reset operation removes and closes |
|
1227 |
* all Handlers and (except for the root logger) sets the level |
|
1228 |
* to null. The root logger's level is set to Level.INFO. |
|
1229 |
* |
|
1230 |
* @exception SecurityException if a security manager exists and if |
|
1231 |
* the caller does not have LoggingPermission("control"). |
|
1232 |
*/ |
|
1233 |
||
1234 |
public void reset() throws SecurityException { |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1235 |
checkPermission(); |
2 | 1236 |
synchronized (this) { |
1237 |
props = new Properties(); |
|
1238 |
// Since we are doing a reset we no longer want to initialize |
|
1239 |
// the global handlers, if they haven't been initialized yet. |
|
1240 |
initializedGlobalHandlers = true; |
|
1241 |
} |
|
16098 | 1242 |
for (LoggerContext cx : contexts()) { |
1243 |
Enumeration<String> enum_ = cx.getLoggerNames(); |
|
1244 |
while (enum_.hasMoreElements()) { |
|
1245 |
String name = enum_.nextElement(); |
|
1246 |
Logger logger = cx.findLogger(name); |
|
1247 |
if (logger != null) { |
|
1248 |
resetLogger(logger); |
|
1249 |
} |
|
1250 |
} |
|
2 | 1251 |
} |
1252 |
} |
|
1253 |
||
1254 |
// Private method to reset an individual target logger. |
|
16098 | 1255 |
private void resetLogger(Logger logger) { |
2 | 1256 |
// Close all the Logger's handlers. |
1257 |
Handler[] targets = logger.getHandlers(); |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
22046
diff
changeset
|
1258 |
for (Handler h : targets) { |
2 | 1259 |
logger.removeHandler(h); |
1260 |
try { |
|
1261 |
h.close(); |
|
1262 |
} catch (Exception ex) { |
|
1263 |
// Problems closing a handler? Keep going... |
|
1264 |
} |
|
1265 |
} |
|
16098 | 1266 |
String name = logger.getName(); |
2 | 1267 |
if (name != null && name.equals("")) { |
1268 |
// This is the root logger. |
|
1269 |
logger.setLevel(defaultLevel); |
|
1270 |
} else { |
|
1271 |
logger.setLevel(null); |
|
1272 |
} |
|
1273 |
} |
|
1274 |
||
1275 |
// get a list of whitespace separated classnames from a property. |
|
1276 |
private String[] parseClassNames(String propertyName) { |
|
1277 |
String hands = getProperty(propertyName); |
|
1278 |
if (hands == null) { |
|
1279 |
return new String[0]; |
|
1280 |
} |
|
1281 |
hands = hands.trim(); |
|
1282 |
int ix = 0; |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1283 |
final List<String> result = new ArrayList<>(); |
2 | 1284 |
while (ix < hands.length()) { |
1285 |
int end = ix; |
|
1286 |
while (end < hands.length()) { |
|
1287 |
if (Character.isWhitespace(hands.charAt(end))) { |
|
1288 |
break; |
|
1289 |
} |
|
1290 |
if (hands.charAt(end) == ',') { |
|
1291 |
break; |
|
1292 |
} |
|
1293 |
end++; |
|
1294 |
} |
|
1295 |
String word = hands.substring(ix, end); |
|
1296 |
ix = end+1; |
|
1297 |
word = word.trim(); |
|
1298 |
if (word.length() == 0) { |
|
1299 |
continue; |
|
1300 |
} |
|
1301 |
result.add(word); |
|
1302 |
} |
|
1303 |
return result.toArray(new String[result.size()]); |
|
1304 |
} |
|
1305 |
||
1306 |
/** |
|
1307 |
* Reinitialize the logging properties and reread the logging configuration |
|
1308 |
* from the given stream, which should be in java.util.Properties format. |
|
26859
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1309 |
* Any {@linkplain #addConfigurationListener registered configuration |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1310 |
* listener} will be invoked after the properties are read. |
2 | 1311 |
* <p> |
1312 |
* Any log level definitions in the new configuration file will be |
|
1313 |
* applied using Logger.setLevel(), if the target Logger exists. |
|
1314 |
* |
|
1315 |
* @param ins stream to read properties from |
|
1316 |
* @exception SecurityException if a security manager exists and if |
|
1317 |
* the caller does not have LoggingPermission("control"). |
|
1318 |
* @exception IOException if there are problems reading from the stream. |
|
1319 |
*/ |
|
1320 |
public void readConfiguration(InputStream ins) throws IOException, SecurityException { |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1321 |
checkPermission(); |
2 | 1322 |
reset(); |
1323 |
||
1324 |
// Load the properties |
|
1325 |
props.load(ins); |
|
1326 |
// Instantiate new configuration objects. |
|
1327 |
String names[] = parseClassNames("config"); |
|
1328 |
||
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
22046
diff
changeset
|
1329 |
for (String word : names) { |
2 | 1330 |
try { |
11274
7e7196757acd
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents:
9700
diff
changeset
|
1331 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word); |
2 | 1332 |
clz.newInstance(); |
1333 |
} catch (Exception ex) { |
|
1334 |
System.err.println("Can't load config class \"" + word + "\""); |
|
1335 |
System.err.println("" + ex); |
|
1336 |
// ex.printStackTrace(); |
|
1337 |
} |
|
1338 |
} |
|
1339 |
||
1340 |
// Set levels on any pre-existing loggers, based on the new properties. |
|
1341 |
setLevelsOnExistingLoggers(); |
|
1342 |
||
26859
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1343 |
try { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1344 |
invokeConfigurationListeners(); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1345 |
} finally { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1346 |
// Note that we need to reinitialize global handles when |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1347 |
// they are first referenced. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1348 |
synchronized (this) { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1349 |
initializedGlobalHandlers = false; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1350 |
} |
2 | 1351 |
} |
1352 |
} |
|
1353 |
||
1354 |
/** |
|
1355 |
* Get the value of a logging property. |
|
1356 |
* The method returns null if the property is not found. |
|
1357 |
* @param name property name |
|
1358 |
* @return property value |
|
1359 |
*/ |
|
1360 |
public String getProperty(String name) { |
|
1361 |
return props.getProperty(name); |
|
1362 |
} |
|
1363 |
||
1364 |
// Package private method to get a String property. |
|
1365 |
// If the property is not defined we return the given |
|
1366 |
// default value. |
|
1367 |
String getStringProperty(String name, String defaultValue) { |
|
1368 |
String val = getProperty(name); |
|
1369 |
if (val == null) { |
|
1370 |
return defaultValue; |
|
1371 |
} |
|
1372 |
return val.trim(); |
|
1373 |
} |
|
1374 |
||
1375 |
// Package private method to get an integer property. |
|
1376 |
// If the property is not defined or cannot be parsed |
|
1377 |
// we return the given default value. |
|
1378 |
int getIntProperty(String name, int defaultValue) { |
|
1379 |
String val = getProperty(name); |
|
1380 |
if (val == null) { |
|
1381 |
return defaultValue; |
|
1382 |
} |
|
1383 |
try { |
|
1384 |
return Integer.parseInt(val.trim()); |
|
1385 |
} catch (Exception ex) { |
|
1386 |
return defaultValue; |
|
1387 |
} |
|
1388 |
} |
|
1389 |
||
27191
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1390 |
// Package private method to get a long property. |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1391 |
// If the property is not defined or cannot be parsed |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1392 |
// we return the given default value. |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1393 |
long getLongProperty(String name, long defaultValue) { |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1394 |
String val = getProperty(name); |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1395 |
if (val == null) { |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1396 |
return defaultValue; |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1397 |
} |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1398 |
try { |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1399 |
return Long.parseLong(val.trim()); |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1400 |
} catch (Exception ex) { |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1401 |
return defaultValue; |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1402 |
} |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1403 |
} |
45a3002de56f
8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents:
26859
diff
changeset
|
1404 |
|
2 | 1405 |
// Package private method to get a boolean property. |
1406 |
// If the property is not defined or cannot be parsed |
|
1407 |
// we return the given default value. |
|
1408 |
boolean getBooleanProperty(String name, boolean defaultValue) { |
|
1409 |
String val = getProperty(name); |
|
1410 |
if (val == null) { |
|
1411 |
return defaultValue; |
|
1412 |
} |
|
1413 |
val = val.toLowerCase(); |
|
1414 |
if (val.equals("true") || val.equals("1")) { |
|
1415 |
return true; |
|
1416 |
} else if (val.equals("false") || val.equals("0")) { |
|
1417 |
return false; |
|
1418 |
} |
|
1419 |
return defaultValue; |
|
1420 |
} |
|
1421 |
||
1422 |
// Package private method to get a Level property. |
|
1423 |
// If the property is not defined or cannot be parsed |
|
1424 |
// we return the given default value. |
|
1425 |
Level getLevelProperty(String name, Level defaultValue) { |
|
1426 |
String val = getProperty(name); |
|
1427 |
if (val == null) { |
|
1428 |
return defaultValue; |
|
1429 |
} |
|
16098 | 1430 |
Level l = Level.findLevel(val.trim()); |
1431 |
return l != null ? l : defaultValue; |
|
2 | 1432 |
} |
1433 |
||
1434 |
// Package private method to get a filter property. |
|
1435 |
// We return an instance of the class named by the "name" |
|
1436 |
// property. If the property is not defined or has problems |
|
1437 |
// we return the defaultValue. |
|
1438 |
Filter getFilterProperty(String name, Filter defaultValue) { |
|
1439 |
String val = getProperty(name); |
|
1440 |
try { |
|
1441 |
if (val != null) { |
|
11274
7e7196757acd
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents:
9700
diff
changeset
|
1442 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val); |
2 | 1443 |
return (Filter) clz.newInstance(); |
1444 |
} |
|
1445 |
} catch (Exception ex) { |
|
1446 |
// We got one of a variety of exceptions in creating the |
|
1447 |
// class or creating an instance. |
|
1448 |
// Drop through. |
|
1449 |
} |
|
1450 |
// We got an exception. Return the defaultValue. |
|
1451 |
return defaultValue; |
|
1452 |
} |
|
1453 |
||
1454 |
||
1455 |
// Package private method to get a formatter property. |
|
1456 |
// We return an instance of the class named by the "name" |
|
1457 |
// property. If the property is not defined or has problems |
|
1458 |
// we return the defaultValue. |
|
1459 |
Formatter getFormatterProperty(String name, Formatter defaultValue) { |
|
1460 |
String val = getProperty(name); |
|
1461 |
try { |
|
1462 |
if (val != null) { |
|
11274
7e7196757acd
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents:
9700
diff
changeset
|
1463 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val); |
2 | 1464 |
return (Formatter) clz.newInstance(); |
1465 |
} |
|
1466 |
} catch (Exception ex) { |
|
1467 |
// We got one of a variety of exceptions in creating the |
|
1468 |
// class or creating an instance. |
|
1469 |
// Drop through. |
|
1470 |
} |
|
1471 |
// We got an exception. Return the defaultValue. |
|
1472 |
return defaultValue; |
|
1473 |
} |
|
1474 |
||
1475 |
// Private method to load the global handlers. |
|
1476 |
// We do the real work lazily, when the global handlers |
|
1477 |
// are first used. |
|
1478 |
private synchronized void initializeGlobalHandlers() { |
|
1479 |
if (initializedGlobalHandlers) { |
|
1480 |
return; |
|
1481 |
} |
|
1482 |
||
1483 |
initializedGlobalHandlers = true; |
|
1484 |
||
1485 |
if (deathImminent) { |
|
1486 |
// Aaargh... |
|
1487 |
// The VM is shutting down and our exit hook has been called. |
|
1488 |
// Avoid allocating global handlers. |
|
1489 |
return; |
|
1490 |
} |
|
1491 |
loadLoggerHandlers(rootLogger, null, "handlers"); |
|
1492 |
} |
|
1493 |
||
22110
06e486bc20b6
8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents:
22086
diff
changeset
|
1494 |
static final Permission controlPermission = new LoggingPermission("control", null); |
2 | 1495 |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1496 |
void checkPermission() { |
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1497 |
SecurityManager sm = System.getSecurityManager(); |
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1498 |
if (sm != null) |
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1499 |
sm.checkPermission(controlPermission); |
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1500 |
} |
2 | 1501 |
|
1502 |
/** |
|
1503 |
* Check that the current context is trusted to modify the logging |
|
1504 |
* configuration. This requires LoggingPermission("control"). |
|
1505 |
* <p> |
|
1506 |
* If the check fails we throw a SecurityException, otherwise |
|
1507 |
* we return normally. |
|
1508 |
* |
|
1509 |
* @exception SecurityException if a security manager exists and if |
|
1510 |
* the caller does not have LoggingPermission("control"). |
|
1511 |
*/ |
|
1512 |
public void checkAccess() throws SecurityException { |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
13578
diff
changeset
|
1513 |
checkPermission(); |
2 | 1514 |
} |
1515 |
||
1516 |
// Nested class to represent a node in our tree of named loggers. |
|
1517 |
private static class LogNode { |
|
1518 |
HashMap<String,LogNode> children; |
|
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1519 |
LoggerWeakRef loggerRef; |
2 | 1520 |
LogNode parent; |
16098 | 1521 |
final LoggerContext context; |
2 | 1522 |
|
16098 | 1523 |
LogNode(LogNode parent, LoggerContext context) { |
2 | 1524 |
this.parent = parent; |
16098 | 1525 |
this.context = context; |
2 | 1526 |
} |
1527 |
||
1528 |
// Recursive method to walk the tree below a node and set |
|
1529 |
// a new parent logger. |
|
1530 |
void walkAndSetParent(Logger parent) { |
|
1531 |
if (children == null) { |
|
1532 |
return; |
|
1533 |
} |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
22046
diff
changeset
|
1534 |
for (LogNode node : children.values()) { |
5964
0496aa46ae9f
6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents:
5506
diff
changeset
|
1535 |
LoggerWeakRef ref = node.loggerRef; |
2 | 1536 |
Logger logger = (ref == null) ? null : ref.get(); |
1537 |
if (logger == null) { |
|
1538 |
node.walkAndSetParent(parent); |
|
1539 |
} else { |
|
1540 |
doSetParent(logger, parent); |
|
1541 |
} |
|
1542 |
} |
|
1543 |
} |
|
1544 |
} |
|
1545 |
||
1546 |
// We use a subclass of Logger for the root logger, so |
|
1547 |
// that we only instantiate the global handlers when they |
|
1548 |
// are first needed. |
|
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1549 |
private final class RootLogger extends Logger { |
2 | 1550 |
private RootLogger() { |
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1551 |
// We do not call the protected Logger two args constructor here, |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1552 |
// to avoid calling LogManager.getLogManager() from within the |
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1553 |
// RootLogger constructor. |
23899 | 1554 |
super("", null, null, LogManager.this, true); |
2 | 1555 |
} |
1556 |
||
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1557 |
@Override |
2 | 1558 |
public void log(LogRecord record) { |
1559 |
// Make sure that the global handlers have been instantiated. |
|
1560 |
initializeGlobalHandlers(); |
|
1561 |
super.log(record); |
|
1562 |
} |
|
1563 |
||
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1564 |
@Override |
2 | 1565 |
public void addHandler(Handler h) { |
1566 |
initializeGlobalHandlers(); |
|
1567 |
super.addHandler(h); |
|
1568 |
} |
|
1569 |
||
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1570 |
@Override |
2 | 1571 |
public void removeHandler(Handler h) { |
1572 |
initializeGlobalHandlers(); |
|
1573 |
super.removeHandler(h); |
|
1574 |
} |
|
1575 |
||
19825
a7e79bc2e437
8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents:
19809
diff
changeset
|
1576 |
@Override |
23899 | 1577 |
Handler[] accessCheckedHandlers() { |
2 | 1578 |
initializeGlobalHandlers(); |
23899 | 1579 |
return super.accessCheckedHandlers(); |
2 | 1580 |
} |
1581 |
} |
|
1582 |
||
1583 |
||
1584 |
// Private method to be called when the configuration has |
|
1585 |
// changed to apply any level settings to any pre-existing loggers. |
|
1586 |
synchronized private void setLevelsOnExistingLoggers() { |
|
11274
7e7196757acd
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents:
9700
diff
changeset
|
1587 |
Enumeration<?> enum_ = props.propertyNames(); |
2 | 1588 |
while (enum_.hasMoreElements()) { |
1589 |
String key = (String)enum_.nextElement(); |
|
1590 |
if (!key.endsWith(".level")) { |
|
1591 |
// Not a level definition. |
|
1592 |
continue; |
|
1593 |
} |
|
1594 |
int ix = key.length() - 6; |
|
1595 |
String name = key.substring(0, ix); |
|
1596 |
Level level = getLevelProperty(key, null); |
|
1597 |
if (level == null) { |
|
1598 |
System.err.println("Bad level value for property: " + key); |
|
1599 |
continue; |
|
1600 |
} |
|
16098 | 1601 |
for (LoggerContext cx : contexts()) { |
1602 |
Logger l = cx.findLogger(name); |
|
1603 |
if (l == null) { |
|
1604 |
continue; |
|
1605 |
} |
|
1606 |
l.setLevel(level); |
|
2 | 1607 |
} |
1608 |
} |
|
1609 |
} |
|
1610 |
||
1611 |
// Management Support |
|
1612 |
private static LoggingMXBean loggingMXBean = null; |
|
1613 |
/** |
|
1614 |
* String representation of the |
|
9013
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1615 |
* {@link javax.management.ObjectName} for the management interface |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1616 |
* for the logging facility. |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1617 |
* |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1618 |
* @see java.lang.management.PlatformLoggingMXBean |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1619 |
* @see java.util.logging.LoggingMXBean |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1620 |
* |
2 | 1621 |
* @since 1.5 |
1622 |
*/ |
|
1623 |
public final static String LOGGING_MXBEAN_NAME |
|
1624 |
= "java.util.logging:type=Logging"; |
|
1625 |
||
1626 |
/** |
|
1627 |
* Returns <tt>LoggingMXBean</tt> for managing loggers. |
|
9013
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1628 |
* An alternative way to manage loggers is through the |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1629 |
* {@link java.lang.management.PlatformLoggingMXBean} interface |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1630 |
* that can be obtained by calling: |
4172
bceea7bc12b7
6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents:
3861
diff
changeset
|
1631 |
* <pre> |
9013
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1632 |
* PlatformLoggingMXBean logging = {@link java.lang.management.ManagementFactory#getPlatformMXBean(Class) |
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1633 |
* ManagementFactory.getPlatformMXBean}(PlatformLoggingMXBean.class); |
4172
bceea7bc12b7
6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents:
3861
diff
changeset
|
1634 |
* </pre> |
2 | 1635 |
* |
1636 |
* @return a {@link LoggingMXBean} object. |
|
1637 |
* |
|
9013
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1638 |
* @see java.lang.management.PlatformLoggingMXBean |
2 | 1639 |
* @since 1.5 |
1640 |
*/ |
|
9013
eedac0b9f552
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents:
7803
diff
changeset
|
1641 |
public static synchronized LoggingMXBean getLoggingMXBean() { |
2 | 1642 |
if (loggingMXBean == null) { |
1643 |
loggingMXBean = new Logging(); |
|
1644 |
} |
|
1645 |
return loggingMXBean; |
|
1646 |
} |
|
26859
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1647 |
|
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1648 |
/** |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1649 |
* Adds a configuration listener to be invoked each time the logging |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1650 |
* configuration is read. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1651 |
* If the listener is already registered the method does nothing. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1652 |
* <p> |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1653 |
* The listener is invoked with privileges that are restricted by the |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1654 |
* calling context of this method. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1655 |
* The order in which the listeners are invoked is unspecified. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1656 |
* <p> |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1657 |
* It is recommended that listeners do not throw errors or exceptions. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1658 |
* |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1659 |
* If a listener terminates with an uncaught error or exception then |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1660 |
* the first exception will be propagated to the caller of |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1661 |
* {@link #readConfiguration()} (or {@link #readConfiguration(java.io.InputStream)}) |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1662 |
* after all listeners have been invoked. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1663 |
* |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1664 |
* @implNote If more than one listener terminates with an uncaught error or |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1665 |
* exception, an implementation may record the additional errors or |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1666 |
* exceptions as {@linkplain Throwable#addSuppressed(java.lang.Throwable) |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1667 |
* suppressed exceptions}. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1668 |
* |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1669 |
* @param listener A configuration listener that will be invoked after the |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1670 |
* configuration changed. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1671 |
* @return This LogManager. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1672 |
* @throws SecurityException if a security manager exists and if the |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1673 |
* caller does not have LoggingPermission("control"). |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1674 |
* @throws NullPointerException if the listener is null. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1675 |
* |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1676 |
* @since 1.9 |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1677 |
*/ |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1678 |
public LogManager addConfigurationListener(Runnable listener) { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1679 |
final Runnable r = Objects.requireNonNull(listener); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1680 |
checkPermission(); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1681 |
final SecurityManager sm = System.getSecurityManager(); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1682 |
final AccessControlContext acc = |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1683 |
sm == null ? null : AccessController.getContext(); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1684 |
final PrivilegedAction<Void> pa = |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1685 |
acc == null ? null : () -> { r.run() ; return null; }; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1686 |
final Runnable pr = |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1687 |
acc == null ? r : () -> AccessController.doPrivileged(pa, acc); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1688 |
// Will do nothing if already registered. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1689 |
listeners.putIfAbsent(r, pr); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1690 |
return this; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1691 |
} |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1692 |
|
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1693 |
/** |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1694 |
* Removes a previously registered configuration listener. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1695 |
* |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1696 |
* Returns silently if the listener is not found. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1697 |
* |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1698 |
* @param listener the configuration listener to remove. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1699 |
* @throws NullPointerException if the listener is null. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1700 |
* @throws SecurityException if a security manager exists and if the |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1701 |
* caller does not have LoggingPermission("control"). |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1702 |
* |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1703 |
* @since 1.9 |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1704 |
*/ |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1705 |
public void removeConfigurationListener(Runnable listener) { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1706 |
final Runnable key = Objects.requireNonNull(listener); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1707 |
checkPermission(); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1708 |
listeners.remove(key); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1709 |
} |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1710 |
|
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1711 |
private void invokeConfigurationListeners() { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1712 |
Throwable t = null; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1713 |
|
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1714 |
// We're using an IdentityHashMap because we want to compare |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1715 |
// keys using identity (==). |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1716 |
// We don't want to loop within a block synchronized on 'listeners' |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1717 |
// to avoid invoking listeners from yet another synchronized block. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1718 |
// So we're taking a snapshot of the values list to avoid the risk of |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1719 |
// ConcurrentModificationException while looping. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1720 |
// |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1721 |
for (Runnable c : listeners.values().toArray(new Runnable[0])) { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1722 |
try { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1723 |
c.run(); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1724 |
} catch (ThreadDeath death) { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1725 |
throw death; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1726 |
} catch (Error | RuntimeException x) { |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1727 |
if (t == null) t = x; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1728 |
else t.addSuppressed(x); |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1729 |
} |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1730 |
} |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1731 |
// Listeners are not supposed to throw exceptions, but if that |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1732 |
// happens, we will rethrow the first error or exception that is raised |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1733 |
// after all listeners have been invoked. |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1734 |
if (t instanceof Error) throw (Error)t; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1735 |
if (t instanceof RuntimeException) throw (RuntimeException)t; |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1736 |
} |
8df035e321b8
8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents:
25859
diff
changeset
|
1737 |
|
2 | 1738 |
} |