Logger.java 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /* Logger.java -- a class for logging messages
  2. Copyright (C) 2002, 2004, 2006, 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.logging;
  32. import java.util.List;
  33. import java.util.MissingResourceException;
  34. import java.util.ResourceBundle;
  35. import java.security.AccessController;
  36. import java.security.PrivilegedAction;
  37. /**
  38. * A Logger is used for logging information about events. Usually, there
  39. * is a seprate logger for each subsystem or component, although there
  40. * is a shared instance for components that make only occasional use of
  41. * the logging framework.
  42. *
  43. * <p>It is common to name a logger after the name of a corresponding
  44. * Java package. Loggers are organized into a hierarchical namespace;
  45. * for example, the logger <code>"org.gnu.foo"</code> is the
  46. * <em>parent</em> of logger <code>"org.gnu.foo.bar"</code>.
  47. *
  48. * <p>A logger for a named subsystem can be obtained through {@link
  49. * java.util.logging.Logger#getLogger(java.lang.String)}. However,
  50. * only code which has been granted the permission to control the
  51. * logging infrastructure will be allowed to customize that logger.
  52. * Untrusted code can obtain a private, anonymous logger through
  53. * {@link #getAnonymousLogger()} if it wants to perform any
  54. * modifications to the logger.
  55. *
  56. * <p>FIXME: Write more documentation.
  57. *
  58. * @author Sascha Brawer (brawer@acm.org)
  59. */
  60. public class Logger
  61. {
  62. static final Logger root = new Logger("", null);
  63. /**
  64. * A logger provided to applications that make only occasional use
  65. * of the logging framework, typically early prototypes. Serious
  66. * products are supposed to create and use their own Loggers, so
  67. * they can be controlled individually.
  68. */
  69. public static final Logger global;
  70. static
  71. {
  72. // Our class might be initialized from an unprivileged context
  73. global = (Logger) AccessController.doPrivileged
  74. (new PrivilegedAction()
  75. {
  76. public Object run()
  77. {
  78. return getLogger("global");
  79. }
  80. });
  81. }
  82. /**
  83. * The name of the Logger, or <code>null</code> if the logger is
  84. * anonymous.
  85. *
  86. * <p>A previous version of the GNU Classpath implementation granted
  87. * untrusted code the permission to control any logger whose name
  88. * was null. However, test code revealed that the Sun J2SE 1.4
  89. * reference implementation enforces the security control for any
  90. * logger that was not created through getAnonymousLogger, even if
  91. * it has a null name. Therefore, a separate flag {@link
  92. * Logger#anonymous} was introduced.
  93. */
  94. private final String name;
  95. /**
  96. * The name of the resource bundle used for localization.
  97. *
  98. * <p>This variable cannot be declared as <code>final</code>
  99. * because its value can change as a result of calling
  100. * getLogger(String,String).
  101. */
  102. private String resourceBundleName;
  103. /**
  104. * The resource bundle used for localization.
  105. *
  106. * <p>This variable cannot be declared as <code>final</code>
  107. * because its value can change as a result of calling
  108. * getLogger(String,String).
  109. */
  110. private ResourceBundle resourceBundle;
  111. private Filter filter;
  112. private final List handlerList = new java.util.ArrayList(4);
  113. private Handler[] handlers = new Handler[0];
  114. /**
  115. * Indicates whether or not this logger is anonymous. While
  116. * a LoggingPermission is required for any modifications to
  117. * a normal logger, untrusted code can obtain an anonymous logger
  118. * and modify it according to its needs.
  119. *
  120. * <p>A previous version of the GNU Classpath implementation
  121. * granted access to every logger whose name was null.
  122. * However, test code revealed that the Sun J2SE 1.4 reference
  123. * implementation enforces the security control for any logger
  124. * that was not created through getAnonymousLogger, even
  125. * if it has a null name.
  126. */
  127. private boolean anonymous;
  128. private boolean useParentHandlers;
  129. private Level level;
  130. private Logger parent;
  131. /**
  132. * Constructs a Logger for a subsystem. Most applications do not
  133. * need to create new Loggers explicitly; instead, they should call
  134. * the static factory methods
  135. * {@link #getLogger(java.lang.String,java.lang.String) getLogger}
  136. * (with ResourceBundle for localization) or
  137. * {@link #getLogger(java.lang.String) getLogger} (without
  138. * ResourceBundle), respectively.
  139. *
  140. * @param name the name for the logger, for example "java.awt"
  141. * or "com.foo.bar". The name should be based on
  142. * the name of the package issuing log records
  143. * and consist of dot-separated Java identifiers.
  144. *
  145. * @param resourceBundleName the name of a resource bundle
  146. * for localizing messages, or <code>null</code>
  147. * to indicate that messages do not need to be localized.
  148. *
  149. * @throws java.util.MissingResourceException if
  150. * <code>resourceBundleName</code> is not <code>null</code>
  151. * and no such bundle could be located.
  152. */
  153. protected Logger(String name, String resourceBundleName)
  154. throws MissingResourceException
  155. {
  156. this.name = name;
  157. this.resourceBundleName = resourceBundleName;
  158. if (resourceBundleName == null)
  159. resourceBundle = null;
  160. else
  161. resourceBundle = ResourceBundle.getBundle(resourceBundleName);
  162. level = null;
  163. /* This is null when the root logger is being constructed,
  164. * and the root logger afterwards.
  165. */
  166. parent = root;
  167. useParentHandlers = (parent != null);
  168. }
  169. /**
  170. * Finds a registered logger for a subsystem, or creates one in
  171. * case no logger has been registered yet.
  172. *
  173. * @param name the name for the logger, for example "java.awt"
  174. * or "com.foo.bar". The name should be based on
  175. * the name of the package issuing log records
  176. * and consist of dot-separated Java identifiers.
  177. *
  178. * @throws IllegalArgumentException if a logger for the subsystem
  179. * identified by <code>name</code> has already been created,
  180. * but uses a a resource bundle for localizing messages.
  181. *
  182. * @throws NullPointerException if <code>name</code> is
  183. * <code>null</code>.
  184. *
  185. * @return a logger for the subsystem specified by <code>name</code>
  186. * that does not localize messages.
  187. */
  188. public static Logger getLogger(String name)
  189. {
  190. return getLogger(name, null);
  191. }
  192. /**
  193. * Finds a registered logger for a subsystem, or creates one in case
  194. * no logger has been registered yet.
  195. *
  196. * <p>If a logger with the specified name has already been
  197. * registered, the behavior depends on the resource bundle that is
  198. * currently associated with the existing logger.
  199. *
  200. * <ul><li>If the existing logger uses the same resource bundle as
  201. * specified by <code>resourceBundleName</code>, the existing logger
  202. * is returned.</li>
  203. *
  204. * <li>If the existing logger currently does not localize messages,
  205. * the existing logger is modified to use the bundle specified by
  206. * <code>resourceBundleName</code>. The existing logger is then
  207. * returned. Therefore, all subsystems currently using this logger
  208. * will produce localized messages from now on.</li>
  209. *
  210. * <li>If the existing logger already has an associated resource
  211. * bundle, but a different one than specified by
  212. * <code>resourceBundleName</code>, an
  213. * <code>IllegalArgumentException</code> is thrown.</li></ul>
  214. *
  215. * @param name the name for the logger, for example "java.awt"
  216. * or "org.gnu.foo". The name should be based on
  217. * the name of the package issuing log records
  218. * and consist of dot-separated Java identifiers.
  219. *
  220. * @param resourceBundleName the name of a resource bundle
  221. * for localizing messages, or <code>null</code>
  222. * to indicate that messages do not need to be localized.
  223. *
  224. * @return a logger for the subsystem specified by <code>name</code>.
  225. *
  226. * @throws java.util.MissingResourceException if
  227. * <code>resourceBundleName</code> is not <code>null</code>
  228. * and no such bundle could be located.
  229. *
  230. * @throws IllegalArgumentException if a logger for the subsystem
  231. * identified by <code>name</code> has already been created,
  232. * but uses a different resource bundle for localizing
  233. * messages.
  234. *
  235. * @throws NullPointerException if <code>name</code> is
  236. * <code>null</code>.
  237. */
  238. public static Logger getLogger(String name, String resourceBundleName)
  239. {
  240. LogManager lm = LogManager.getLogManager();
  241. Logger result;
  242. if (name == null)
  243. throw new NullPointerException();
  244. /* Without synchronized(lm), it could happen that another thread
  245. * would create a logger between our calls to getLogger and
  246. * addLogger. While addLogger would indicate this by returning
  247. * false, we could not be sure that this other logger was still
  248. * existing when we called getLogger a second time in order
  249. * to retrieve it -- note that LogManager is only allowed to
  250. * keep weak references to registered loggers, so Loggers
  251. * can be garbage collected at any time in general, and between
  252. * our call to addLogger and our second call go getLogger
  253. * in particular.
  254. *
  255. * Of course, we assume here that LogManager.addLogger etc.
  256. * are synchronizing on the global LogManager object. There
  257. * is a comment in the implementation of LogManager.addLogger
  258. * referring to this comment here, so that any change in
  259. * the synchronization of LogManager will be reflected here.
  260. */
  261. synchronized (lm)
  262. {
  263. result = lm.getLogger(name);
  264. if (result == null)
  265. {
  266. boolean couldBeAdded;
  267. result = new Logger(name, resourceBundleName);
  268. couldBeAdded = lm.addLogger(result);
  269. if (!couldBeAdded)
  270. throw new IllegalStateException("cannot register new logger");
  271. }
  272. else
  273. {
  274. /* The logger already exists. Make sure it uses
  275. * the same resource bundle for localizing messages.
  276. */
  277. String existingBundleName = result.getResourceBundleName();
  278. /* The Sun J2SE 1.4 reference implementation will return the
  279. * registered logger object, even if it does not have a resource
  280. * bundle associated with it. However, it seems to change the
  281. * resourceBundle of the registered logger to the bundle
  282. * whose name was passed to getLogger.
  283. */
  284. if ((existingBundleName == null) && (resourceBundleName != null))
  285. {
  286. /* If ResourceBundle.getBundle throws an exception, the
  287. * existing logger will be unchanged. This would be
  288. * different if the assignment to resourceBundleName
  289. * came first.
  290. */
  291. result.resourceBundle = ResourceBundle.getBundle(resourceBundleName);
  292. result.resourceBundleName = resourceBundleName;
  293. return result;
  294. }
  295. if ((existingBundleName != resourceBundleName)
  296. && ((existingBundleName == null)
  297. || !existingBundleName.equals(resourceBundleName)))
  298. {
  299. throw new IllegalArgumentException();
  300. }
  301. }
  302. }
  303. return result;
  304. }
  305. /**
  306. * Creates a new, unnamed logger. Unnamed loggers are not
  307. * registered in the namespace of the LogManager, and no special
  308. * security permission is required for changing their state.
  309. * Therefore, untrusted applets are able to modify their private
  310. * logger instance obtained through this method.
  311. *
  312. * <p>The parent of the newly created logger will the the root
  313. * logger, from which the level threshold and the handlers are
  314. * inherited.
  315. */
  316. public static Logger getAnonymousLogger()
  317. {
  318. return getAnonymousLogger(null);
  319. }
  320. /**
  321. * Creates a new, unnamed logger. Unnamed loggers are not
  322. * registered in the namespace of the LogManager, and no special
  323. * security permission is required for changing their state.
  324. * Therefore, untrusted applets are able to modify their private
  325. * logger instance obtained through this method.
  326. *
  327. * <p>The parent of the newly created logger will the the root
  328. * logger, from which the level threshold and the handlers are
  329. * inherited.
  330. *
  331. * @param resourceBundleName the name of a resource bundle
  332. * for localizing messages, or <code>null</code>
  333. * to indicate that messages do not need to be localized.
  334. *
  335. * @throws java.util.MissingResourceException if
  336. * <code>resourceBundleName</code> is not <code>null</code>
  337. * and no such bundle could be located.
  338. */
  339. public static Logger getAnonymousLogger(String resourceBundleName)
  340. throws MissingResourceException
  341. {
  342. Logger result;
  343. result = new Logger(null, resourceBundleName);
  344. result.anonymous = true;
  345. return result;
  346. }
  347. /**
  348. * Returns the name of the resource bundle that is being used for
  349. * localizing messages.
  350. *
  351. * @return the name of the resource bundle used for localizing messages,
  352. * or <code>null</code> if the parent's resource bundle
  353. * is used for this purpose.
  354. */
  355. public synchronized String getResourceBundleName()
  356. {
  357. return resourceBundleName;
  358. }
  359. /**
  360. * Returns the resource bundle that is being used for localizing
  361. * messages.
  362. *
  363. * @return the resource bundle used for localizing messages,
  364. * or <code>null</code> if the parent's resource bundle
  365. * is used for this purpose.
  366. */
  367. public synchronized ResourceBundle getResourceBundle()
  368. {
  369. return resourceBundle;
  370. }
  371. /**
  372. * Returns the severity level threshold for this <code>Handler</code>.
  373. * All log records with a lower severity level will be discarded;
  374. * a log record of the same or a higher level will be published
  375. * unless an installed <code>Filter</code> decides to discard it.
  376. *
  377. * @return the severity level below which all log messages will be
  378. * discarded, or <code>null</code> if the logger inherits
  379. * the threshold from its parent.
  380. */
  381. public synchronized Level getLevel()
  382. {
  383. return level;
  384. }
  385. /**
  386. * Returns whether or not a message of the specified level
  387. * would be logged by this logger.
  388. *
  389. * @throws NullPointerException if <code>level</code>
  390. * is <code>null</code>.
  391. */
  392. public synchronized boolean isLoggable(Level level)
  393. {
  394. if (this.level != null)
  395. return this.level.intValue() <= level.intValue();
  396. if (parent != null)
  397. return parent.isLoggable(level);
  398. else
  399. return false;
  400. }
  401. /**
  402. * Sets the severity level threshold for this <code>Handler</code>.
  403. * All log records with a lower severity level will be discarded
  404. * immediately. A log record of the same or a higher level will be
  405. * published unless an installed <code>Filter</code> decides to
  406. * discard it.
  407. *
  408. * @param level the severity level below which all log messages
  409. * will be discarded, or <code>null</code> to
  410. * indicate that the logger should inherit the
  411. * threshold from its parent.
  412. *
  413. * @throws SecurityException if this logger is not anonymous, a
  414. * security manager exists, and the caller is not granted
  415. * the permission to control the logging infrastructure by
  416. * having LoggingPermission("control"). Untrusted code can
  417. * obtain an anonymous logger through the static factory method
  418. * {@link #getAnonymousLogger(java.lang.String) getAnonymousLogger}.
  419. */
  420. public synchronized void setLevel(Level level)
  421. {
  422. /* An application is allowed to control an anonymous logger
  423. * without having the permission to control the logging
  424. * infrastructure.
  425. */
  426. if (!anonymous)
  427. LogManager.getLogManager().checkAccess();
  428. this.level = level;
  429. }
  430. public synchronized Filter getFilter()
  431. {
  432. return filter;
  433. }
  434. /**
  435. * @throws SecurityException if this logger is not anonymous, a
  436. * security manager exists, and the caller is not granted
  437. * the permission to control the logging infrastructure by
  438. * having LoggingPermission("control"). Untrusted code can
  439. * obtain an anonymous logger through the static factory method
  440. * {@link #getAnonymousLogger(java.lang.String) getAnonymousLogger}.
  441. */
  442. public synchronized void setFilter(Filter filter)
  443. throws SecurityException
  444. {
  445. /* An application is allowed to control an anonymous logger
  446. * without having the permission to control the logging
  447. * infrastructure.
  448. */
  449. if (!anonymous)
  450. LogManager.getLogManager().checkAccess();
  451. this.filter = filter;
  452. }
  453. /**
  454. * Returns the name of this logger.
  455. *
  456. * @return the name of this logger, or <code>null</code> if
  457. * the logger is anonymous.
  458. */
  459. public String getName()
  460. {
  461. /* Note that the name of a logger cannot be changed during
  462. * its lifetime, so no synchronization is needed.
  463. */
  464. return name;
  465. }
  466. /**
  467. * Passes a record to registered handlers, provided the record
  468. * is considered as loggable both by {@link #isLoggable(Level)}
  469. * and a possibly installed custom {@link #setFilter(Filter) filter}.
  470. *
  471. * <p>If the logger has been configured to use parent handlers,
  472. * the record will be forwarded to the parent of this logger
  473. * in addition to being processed by the handlers registered with
  474. * this logger.
  475. *
  476. * <p>The other logging methods in this class are convenience methods
  477. * that merely create a new LogRecord and pass it to this method.
  478. * Therefore, subclasses usually just need to override this single
  479. * method for customizing the logging behavior.
  480. *
  481. * @param record the log record to be inspected and possibly forwarded.
  482. */
  483. public synchronized void log(LogRecord record)
  484. {
  485. if (!isLoggable(record.getLevel()))
  486. return;
  487. if ((filter != null) && !filter.isLoggable(record))
  488. return;
  489. /* If no logger name has been set for the log record,
  490. * use the name of this logger.
  491. */
  492. if (record.getLoggerName() == null)
  493. record.setLoggerName(name);
  494. /* Avoid that some other thread is changing the logger hierarchy
  495. * while we are traversing it.
  496. */
  497. synchronized (LogManager.getLogManager())
  498. {
  499. Logger curLogger = this;
  500. do
  501. {
  502. /* The Sun J2SE 1.4 reference implementation seems to call the
  503. * filter only for the logger whose log method is called,
  504. * never for any of its parents. Also, parent loggers publish
  505. * log record whatever their level might be. This is pretty
  506. * weird, but GNU Classpath tries to be as compatible as
  507. * possible to the reference implementation.
  508. */
  509. for (int i = 0; i < curLogger.handlers.length; i++)
  510. curLogger.handlers[i].publish(record);
  511. if (curLogger.getUseParentHandlers() == false)
  512. break;
  513. curLogger = curLogger.getParent();
  514. }
  515. while (parent != null);
  516. }
  517. }
  518. public void log(Level level, String message)
  519. {
  520. if (isLoggable(level))
  521. log(level, message, (Object[]) null);
  522. }
  523. public synchronized void log(Level level,
  524. String message,
  525. Object param)
  526. {
  527. if (isLoggable(level))
  528. {
  529. StackTraceElement caller = getCallerStackFrame();
  530. logp(level,
  531. caller != null ? caller.getClassName() : "<unknown>",
  532. caller != null ? caller.getMethodName() : "<unknown>",
  533. message,
  534. param);
  535. }
  536. }
  537. public synchronized void log(Level level,
  538. String message,
  539. Object[] params)
  540. {
  541. if (isLoggable(level))
  542. {
  543. StackTraceElement caller = getCallerStackFrame();
  544. logp(level,
  545. caller != null ? caller.getClassName() : "<unknown>",
  546. caller != null ? caller.getMethodName() : "<unknown>",
  547. message,
  548. params);
  549. }
  550. }
  551. public synchronized void log(Level level,
  552. String message,
  553. Throwable thrown)
  554. {
  555. if (isLoggable(level))
  556. {
  557. StackTraceElement caller = getCallerStackFrame();
  558. logp(level,
  559. caller != null ? caller.getClassName() : "<unknown>",
  560. caller != null ? caller.getMethodName() : "<unknown>",
  561. message,
  562. thrown);
  563. }
  564. }
  565. public synchronized void logp(Level level,
  566. String sourceClass,
  567. String sourceMethod,
  568. String message)
  569. {
  570. logp(level, sourceClass, sourceMethod, message,
  571. (Object[]) null);
  572. }
  573. public synchronized void logp(Level level,
  574. String sourceClass,
  575. String sourceMethod,
  576. String message,
  577. Object param)
  578. {
  579. logp(level, sourceClass, sourceMethod, message,
  580. new Object[] { param });
  581. }
  582. private synchronized ResourceBundle findResourceBundle()
  583. {
  584. if (resourceBundle != null)
  585. return resourceBundle;
  586. if (parent != null)
  587. return parent.findResourceBundle();
  588. return null;
  589. }
  590. private synchronized void logImpl(Level level,
  591. String sourceClass,
  592. String sourceMethod,
  593. String message,
  594. Object[] params)
  595. {
  596. LogRecord rec = new LogRecord(level, message);
  597. rec.setResourceBundle(findResourceBundle());
  598. rec.setSourceClassName(sourceClass);
  599. rec.setSourceMethodName(sourceMethod);
  600. rec.setParameters(params);
  601. log(rec);
  602. }
  603. public synchronized void logp(Level level,
  604. String sourceClass,
  605. String sourceMethod,
  606. String message,
  607. Object[] params)
  608. {
  609. logImpl(level, sourceClass, sourceMethod, message, params);
  610. }
  611. public synchronized void logp(Level level,
  612. String sourceClass,
  613. String sourceMethod,
  614. String message,
  615. Throwable thrown)
  616. {
  617. LogRecord rec = new LogRecord(level, message);
  618. rec.setResourceBundle(resourceBundle);
  619. rec.setSourceClassName(sourceClass);
  620. rec.setSourceMethodName(sourceMethod);
  621. rec.setThrown(thrown);
  622. log(rec);
  623. }
  624. public synchronized void logrb(Level level,
  625. String sourceClass,
  626. String sourceMethod,
  627. String bundleName,
  628. String message)
  629. {
  630. logrb(level, sourceClass, sourceMethod, bundleName,
  631. message, (Object[]) null);
  632. }
  633. public synchronized void logrb(Level level,
  634. String sourceClass,
  635. String sourceMethod,
  636. String bundleName,
  637. String message,
  638. Object param)
  639. {
  640. logrb(level, sourceClass, sourceMethod, bundleName,
  641. message, new Object[] { param });
  642. }
  643. public synchronized void logrb(Level level,
  644. String sourceClass,
  645. String sourceMethod,
  646. String bundleName,
  647. String message,
  648. Object[] params)
  649. {
  650. LogRecord rec = new LogRecord(level, message);
  651. rec.setResourceBundleName(bundleName);
  652. rec.setSourceClassName(sourceClass);
  653. rec.setSourceMethodName(sourceMethod);
  654. rec.setParameters(params);
  655. log(rec);
  656. }
  657. public synchronized void logrb(Level level,
  658. String sourceClass,
  659. String sourceMethod,
  660. String bundleName,
  661. String message,
  662. Throwable thrown)
  663. {
  664. LogRecord rec = new LogRecord(level, message);
  665. rec.setResourceBundleName(bundleName);
  666. rec.setSourceClassName(sourceClass);
  667. rec.setSourceMethodName(sourceMethod);
  668. rec.setThrown(thrown);
  669. log(rec);
  670. }
  671. public synchronized void entering(String sourceClass,
  672. String sourceMethod)
  673. {
  674. if (isLoggable(Level.FINER))
  675. logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
  676. }
  677. public synchronized void entering(String sourceClass,
  678. String sourceMethod,
  679. Object param)
  680. {
  681. if (isLoggable(Level.FINER))
  682. logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", param);
  683. }
  684. public synchronized void entering(String sourceClass,
  685. String sourceMethod,
  686. Object[] params)
  687. {
  688. if (isLoggable(Level.FINER))
  689. {
  690. StringBuffer buf = new StringBuffer(80);
  691. buf.append("ENTRY");
  692. for (int i = 0; i < params.length; i++)
  693. {
  694. buf.append(" {");
  695. buf.append(i);
  696. buf.append('}');
  697. }
  698. logp(Level.FINER, sourceClass, sourceMethod, buf.toString(), params);
  699. }
  700. }
  701. public synchronized void exiting(String sourceClass,
  702. String sourceMethod)
  703. {
  704. if (isLoggable(Level.FINER))
  705. logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
  706. }
  707. public synchronized void exiting(String sourceClass,
  708. String sourceMethod,
  709. Object result)
  710. {
  711. if (isLoggable(Level.FINER))
  712. logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
  713. }
  714. public synchronized void throwing(String sourceClass,
  715. String sourceMethod,
  716. Throwable thrown)
  717. {
  718. if (isLoggable(Level.FINER))
  719. logp(Level.FINER, sourceClass, sourceMethod, "THROW", thrown);
  720. }
  721. /**
  722. * Logs a message with severity level SEVERE, indicating a serious
  723. * failure that prevents normal program execution. Messages at this
  724. * level should be understandable to an inexperienced, non-technical
  725. * end user. Ideally, they explain in simple words what actions the
  726. * user can take in order to resolve the problem.
  727. *
  728. * @see Level#SEVERE
  729. *
  730. * @param message the message text, also used as look-up key if the
  731. * logger is localizing messages with a resource
  732. * bundle. While it is possible to pass
  733. * <code>null</code>, this is not recommended, since
  734. * a logging message without text is unlikely to be
  735. * helpful.
  736. */
  737. public synchronized void severe(String message)
  738. {
  739. if (isLoggable(Level.SEVERE))
  740. log(Level.SEVERE, message);
  741. }
  742. /**
  743. * Logs a message with severity level WARNING, indicating a
  744. * potential problem that does not prevent normal program execution.
  745. * Messages at this level should be understandable to an
  746. * inexperienced, non-technical end user. Ideally, they explain in
  747. * simple words what actions the user can take in order to resolve
  748. * the problem.
  749. *
  750. * @see Level#WARNING
  751. *
  752. * @param message the message text, also used as look-up key if the
  753. * logger is localizing messages with a resource
  754. * bundle. While it is possible to pass
  755. * <code>null</code>, this is not recommended, since
  756. * a logging message without text is unlikely to be
  757. * helpful.
  758. */
  759. public synchronized void warning(String message)
  760. {
  761. if (isLoggable(Level.WARNING))
  762. log(Level.WARNING, message);
  763. }
  764. /**
  765. * Logs a message with severity level INFO. {@link Level#INFO} is
  766. * intended for purely informational messages that do not indicate
  767. * error or warning situations. In the default logging
  768. * configuration, INFO messages will be written to the system
  769. * console. For this reason, the INFO level should be used only for
  770. * messages that are important to end users and system
  771. * administrators. Messages at this level should be understandable
  772. * to an inexperienced, non-technical user.
  773. *
  774. * @param message the message text, also used as look-up key if the
  775. * logger is localizing messages with a resource
  776. * bundle. While it is possible to pass
  777. * <code>null</code>, this is not recommended, since
  778. * a logging message without text is unlikely to be
  779. * helpful.
  780. */
  781. public synchronized void info(String message)
  782. {
  783. if (isLoggable(Level.INFO))
  784. log(Level.INFO, message);
  785. }
  786. /**
  787. * Logs a message with severity level CONFIG. {@link Level#CONFIG} is
  788. * intended for static configuration messages, for example about the
  789. * windowing environment, the operating system version, etc.
  790. *
  791. * @param message the message text, also used as look-up key if the
  792. * logger is localizing messages with a resource bundle. While
  793. * it is possible to pass <code>null</code>, this is not
  794. * recommended, since a logging message without text is unlikely
  795. * to be helpful.
  796. */
  797. public synchronized void config(String message)
  798. {
  799. if (isLoggable(Level.CONFIG))
  800. log(Level.CONFIG, message);
  801. }
  802. /**
  803. * Logs a message with severity level FINE. {@link Level#FINE} is
  804. * intended for messages that are relevant for developers using
  805. * the component generating log messages. Examples include minor,
  806. * recoverable failures, or possible inefficiencies.
  807. *
  808. * @param message the message text, also used as look-up key if the
  809. * logger is localizing messages with a resource
  810. * bundle. While it is possible to pass
  811. * <code>null</code>, this is not recommended, since
  812. * a logging message without text is unlikely to be
  813. * helpful.
  814. */
  815. public synchronized void fine(String message)
  816. {
  817. if (isLoggable(Level.FINE))
  818. log(Level.FINE, message);
  819. }
  820. /**
  821. * Logs a message with severity level FINER. {@link Level#FINER} is
  822. * intended for rather detailed tracing, for example entering a
  823. * method, returning from a method, or throwing an exception.
  824. *
  825. * @param message the message text, also used as look-up key if the
  826. * logger is localizing messages with a resource
  827. * bundle. While it is possible to pass
  828. * <code>null</code>, this is not recommended, since
  829. * a logging message without text is unlikely to be
  830. * helpful.
  831. */
  832. public synchronized void finer(String message)
  833. {
  834. if (isLoggable(Level.FINER))
  835. log(Level.FINER, message);
  836. }
  837. /**
  838. * Logs a message with severity level FINEST. {@link Level#FINEST}
  839. * is intended for highly detailed tracing, for example reaching a
  840. * certain point inside the body of a method.
  841. *
  842. * @param message the message text, also used as look-up key if the
  843. * logger is localizing messages with a resource
  844. * bundle. While it is possible to pass
  845. * <code>null</code>, this is not recommended, since
  846. * a logging message without text is unlikely to be
  847. * helpful.
  848. */
  849. public synchronized void finest(String message)
  850. {
  851. if (isLoggable(Level.FINEST))
  852. log(Level.FINEST, message);
  853. }
  854. /**
  855. * Adds a handler to the set of handlers that get notified
  856. * when a log record is to be published.
  857. *
  858. * @param handler the handler to be added.
  859. *
  860. * @throws NullPointerException if <code>handler</code>
  861. * is <code>null</code>.
  862. *
  863. * @throws SecurityException if this logger is not anonymous, a
  864. * security manager exists, and the caller is not granted
  865. * the permission to control the logging infrastructure by
  866. * having LoggingPermission("control"). Untrusted code can
  867. * obtain an anonymous logger through the static factory method
  868. * {@link #getAnonymousLogger(java.lang.String) getAnonymousLogger}.
  869. */
  870. public synchronized void addHandler(Handler handler)
  871. throws SecurityException
  872. {
  873. if (handler == null)
  874. throw new NullPointerException();
  875. /* An application is allowed to control an anonymous logger
  876. * without having the permission to control the logging
  877. * infrastructure.
  878. */
  879. if (!anonymous)
  880. LogManager.getLogManager().checkAccess();
  881. if (!handlerList.contains(handler))
  882. {
  883. handlerList.add(handler);
  884. handlers = getHandlers();
  885. }
  886. }
  887. /**
  888. * Removes a handler from the set of handlers that get notified
  889. * when a log record is to be published.
  890. *
  891. * @param handler the handler to be removed.
  892. *
  893. * @throws SecurityException if this logger is not anonymous, a
  894. * security manager exists, and the caller is not granted the
  895. * permission to control the logging infrastructure by having
  896. * LoggingPermission("control"). Untrusted code can obtain an
  897. * anonymous logger through the static factory method {@link
  898. * #getAnonymousLogger(java.lang.String) getAnonymousLogger}.
  899. *
  900. * @throws NullPointerException if <code>handler</code>
  901. * is <code>null</code>.
  902. */
  903. public synchronized void removeHandler(Handler handler)
  904. throws SecurityException
  905. {
  906. /* An application is allowed to control an anonymous logger
  907. * without having the permission to control the logging
  908. * infrastructure.
  909. */
  910. if (!anonymous)
  911. LogManager.getLogManager().checkAccess();
  912. if (handler == null)
  913. throw new NullPointerException();
  914. handlerList.remove(handler);
  915. handlers = getHandlers();
  916. }
  917. /**
  918. * Returns the handlers currently registered for this Logger.
  919. * When a log record has been deemed as being loggable,
  920. * it will be passed to all registered handlers for
  921. * publication. In addition, if the logger uses parent handlers
  922. * (see {@link #getUseParentHandlers() getUseParentHandlers}
  923. * and {@link #setUseParentHandlers(boolean) setUseParentHandlers},
  924. * the log record will be passed to the parent's handlers.
  925. */
  926. public synchronized Handler[] getHandlers()
  927. {
  928. /* We cannot return our internal handlers array
  929. * because we do not have any guarantee that the
  930. * caller would not change the array entries.
  931. */
  932. return (Handler[]) handlerList.toArray(new Handler[handlerList.size()]);
  933. }
  934. /**
  935. * Returns whether or not this Logger forwards log records to
  936. * handlers registered for its parent loggers.
  937. *
  938. * @return <code>false</code> if this Logger sends log records
  939. * merely to Handlers registered with itself;
  940. * <code>true</code> if this Logger sends log records
  941. * not only to Handlers registered with itself, but also
  942. * to those Handlers registered with parent loggers.
  943. */
  944. public synchronized boolean getUseParentHandlers()
  945. {
  946. return useParentHandlers;
  947. }
  948. /**
  949. * Sets whether or not this Logger forwards log records to
  950. * handlers registered for its parent loggers.
  951. *
  952. * @param useParentHandlers <code>false</code> to let this
  953. * Logger send log records merely to Handlers registered
  954. * with itself; <code>true</code> to let this Logger
  955. * send log records not only to Handlers registered
  956. * with itself, but also to those Handlers registered with
  957. * parent loggers.
  958. *
  959. * @throws SecurityException if this logger is not anonymous, a
  960. * security manager exists, and the caller is not granted
  961. * the permission to control the logging infrastructure by
  962. * having LoggingPermission("control"). Untrusted code can
  963. * obtain an anonymous logger through the static factory method
  964. * {@link #getAnonymousLogger(java.lang.String) getAnonymousLogger}.
  965. *
  966. */
  967. public synchronized void setUseParentHandlers(boolean useParentHandlers)
  968. {
  969. /* An application is allowed to control an anonymous logger
  970. * without having the permission to control the logging
  971. * infrastructure.
  972. */
  973. if (!anonymous)
  974. LogManager.getLogManager().checkAccess();
  975. this.useParentHandlers = useParentHandlers;
  976. }
  977. /**
  978. * Returns the parent of this logger. By default, the parent is
  979. * assigned by the LogManager by inspecting the logger's name.
  980. *
  981. * @return the parent of this logger (as detemined by the LogManager
  982. * by inspecting logger names), the root logger if no other
  983. * logger has a name which is a prefix of this logger's name, or
  984. * <code>null</code> for the root logger.
  985. */
  986. public synchronized Logger getParent()
  987. {
  988. return parent;
  989. }
  990. /**
  991. * Sets the parent of this logger. Usually, applications do not
  992. * call this method directly. Instead, the LogManager will ensure
  993. * that the tree of loggers reflects the hierarchical logger
  994. * namespace. Basically, this method should not be public at all,
  995. * but the GNU implementation follows the API specification.
  996. *
  997. * @throws NullPointerException if <code>parent</code> is
  998. * <code>null</code>.
  999. *
  1000. * @throws SecurityException if this logger is not anonymous, a
  1001. * security manager exists, and the caller is not granted
  1002. * the permission to control the logging infrastructure by
  1003. * having LoggingPermission("control"). Untrusted code can
  1004. * obtain an anonymous logger through the static factory method
  1005. * {@link #getAnonymousLogger(java.lang.String) getAnonymousLogger}.
  1006. */
  1007. public synchronized void setParent(Logger parent)
  1008. {
  1009. if (parent == null)
  1010. throw new NullPointerException();
  1011. if (this == root)
  1012. throw new IllegalArgumentException(
  1013. "the root logger can only have a null parent");
  1014. /* An application is allowed to control an anonymous logger
  1015. * without having the permission to control the logging
  1016. * infrastructure.
  1017. */
  1018. if (!anonymous)
  1019. LogManager.getLogManager().checkAccess();
  1020. this.parent = parent;
  1021. }
  1022. /**
  1023. * Gets the StackTraceElement of the first class that is not this class.
  1024. * That should be the initial caller of a logging method.
  1025. * @return caller of the initial logging method or null if unknown.
  1026. */
  1027. private native StackTraceElement getCallerStackFrame();
  1028. /**
  1029. * Reset and close handlers attached to this logger. This function is package
  1030. * private because it must only be avaiable to the LogManager.
  1031. */
  1032. void resetLogger()
  1033. {
  1034. for (int i = 0; i < handlers.length; i++)
  1035. {
  1036. handlers[i].close();
  1037. handlerList.remove(handlers[i]);
  1038. }
  1039. handlers = getHandlers();
  1040. }
  1041. }