LogRecord.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* LogRecord.java --
  2. A class for the state associated with individual logging events
  3. Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util.logging;
  33. import java.util.ResourceBundle;
  34. /**
  35. * A <code>LogRecord</code> contains the state for an individual
  36. * event to be logged.
  37. *
  38. * <p>As soon as a LogRecord instance has been handed over to the
  39. * logging framework, applications should not manipulate it anymore.
  40. *
  41. * @author Sascha Brawer (brawer@acm.org)
  42. */
  43. public class LogRecord
  44. implements java.io.Serializable
  45. {
  46. /**
  47. * The severity level of this <code>LogRecord</code>.
  48. */
  49. private Level level;
  50. /**
  51. * The sequence number of this <code>LogRecord</code>.
  52. */
  53. private long sequenceNumber;
  54. /**
  55. * The name of the class that issued the logging request, or
  56. * <code>null</code> if this information could not be obtained.
  57. */
  58. private String sourceClassName;
  59. /**
  60. * The name of the method that issued the logging request, or
  61. * <code>null</code> if this information could not be obtained.
  62. */
  63. private String sourceMethodName;
  64. /**
  65. * The message for this <code>LogRecord</code> before
  66. * any localization or formatting.
  67. */
  68. private String message;
  69. /**
  70. * An identifier for the thread in which this <code>LogRecord</code>
  71. * was created. The identifier is not necessarily related to any
  72. * thread identifiers used by the operating system.
  73. */
  74. private int threadID;
  75. /**
  76. * The time when this <code>LogRecord</code> was created,
  77. * in milliseconds since the beginning of January 1, 1970.
  78. */
  79. private long millis;
  80. /**
  81. * The Throwable associated with this <code>LogRecord</code>, or
  82. * <code>null</code> if the logged event is not related to an
  83. * exception or error.
  84. */
  85. private Throwable thrown;
  86. /**
  87. * The name of the logger where this <code>LogRecord</code> has
  88. * originated, or <code>null</code> if this <code>LogRecord</code>
  89. * does not originate from a <code>Logger</code>.
  90. */
  91. private String loggerName;
  92. /**
  93. * The name of the resource bundle used for localizing log messages,
  94. * or <code>null</code> if no bundle has been specified.
  95. */
  96. private String resourceBundleName;
  97. private transient Object[] parameters;
  98. private transient ResourceBundle bundle;
  99. /**
  100. * Constructs a <code>LogRecord</code> given a severity level and
  101. * an unlocalized message text. In addition, the sequence number,
  102. * creation time (as returned by <code>getMillis()</code>) and
  103. * thread ID are assigned. All other properties are set to
  104. * <code>null</code>.
  105. *
  106. * @param level the severity level, for example <code>Level.WARNING</code>.
  107. *
  108. * @param message the message text (which will be used as key
  109. * for looking up the localized message text
  110. * if a resource bundle has been associated).
  111. */
  112. public LogRecord(Level level, String message)
  113. {
  114. this.level = level;
  115. this.message = message;
  116. this.millis = System.currentTimeMillis();
  117. /* A subclass of java.lang.Thread could override hashCode(),
  118. * in which case the result would not be guaranteed anymore
  119. * to be unique among all threads. While System.identityHashCode
  120. * is not necessarily unique either, it at least cannot be
  121. * overridden by user code. However, is might be a good idea
  122. * to use something better for generating thread IDs.
  123. */
  124. this.threadID = System.identityHashCode(Thread.currentThread());
  125. sequenceNumber = allocateSeqNum();
  126. }
  127. /**
  128. * Determined with the serialver tool of the Sun J2SE 1.4.
  129. */
  130. static final long serialVersionUID = 5372048053134512534L;
  131. private void readObject(java.io.ObjectInputStream in)
  132. throws java.io.IOException, java.lang.ClassNotFoundException
  133. {
  134. in.defaultReadObject();
  135. /* We assume that future versions will be downwards compatible,
  136. * so we can ignore the versions.
  137. */
  138. byte majorVersion = in.readByte();
  139. byte minorVersion = in.readByte();
  140. int numParams = in.readInt();
  141. if (numParams >= 0)
  142. {
  143. parameters = new Object[numParams];
  144. for (int i = 0; i < numParams; i++)
  145. parameters[i] = in.readObject();
  146. }
  147. }
  148. /**
  149. * @serialData The default fields, followed by a major byte version
  150. * number, followed by a minor byte version number, followed by
  151. * information about the log record parameters. If
  152. * <code>parameters</code> is <code>null</code>, the integer -1 is
  153. * written, otherwise the length of the <code>parameters</code>
  154. * array (which can be zero), followed by the result of calling
  155. * {@link Object#toString() toString()} on the parameter (or
  156. * <code>null</code> if the parameter is <code>null</code>).
  157. *
  158. * <p><strong>Specification Note:</strong> The Javadoc for the
  159. * Sun reference implementation does not specify the version
  160. * number. FIXME: Reverse-engineer the JDK and file a bug
  161. * report with Sun, asking for amendment of the specification.
  162. */
  163. private void writeObject(java.io.ObjectOutputStream out)
  164. throws java.io.IOException
  165. {
  166. out.defaultWriteObject();
  167. /* Major, minor version number: The Javadoc for J2SE1.4 does not
  168. * specify the values.
  169. */
  170. out.writeByte(0);
  171. out.writeByte(0);
  172. if (parameters == null)
  173. out.writeInt(-1);
  174. else
  175. {
  176. out.writeInt(parameters.length);
  177. for (int i = 0; i < parameters.length; i++)
  178. {
  179. if (parameters[i] == null)
  180. out.writeObject(null);
  181. else
  182. out.writeObject(parameters[i].toString());
  183. }
  184. }
  185. }
  186. /**
  187. * Returns the name of the logger where this <code>LogRecord</code>
  188. * has originated.
  189. *
  190. * @return the name of the source {@link Logger}, or
  191. * <code>null</code> if this <code>LogRecord</code>
  192. * does not originate from a <code>Logger</code>.
  193. */
  194. public String getLoggerName()
  195. {
  196. return loggerName;
  197. }
  198. /**
  199. * Sets the name of the logger where this <code>LogRecord</code>
  200. * has originated.
  201. *
  202. * <p>As soon as a <code>LogRecord</code> has been handed over
  203. * to the logging framework, applications should not modify it
  204. * anymore. Therefore, this method should only be called on
  205. * freshly constructed LogRecords.
  206. *
  207. * @param name the name of the source logger, or <code>null</code> to
  208. * indicate that this <code>LogRecord</code> does not
  209. * originate from a <code>Logger</code>.
  210. */
  211. public void setLoggerName(String name)
  212. {
  213. loggerName = name;
  214. }
  215. /**
  216. * Returns the resource bundle that is used when the message
  217. * of this <code>LogRecord</code> needs to be localized.
  218. *
  219. * @return the resource bundle used for localization,
  220. * or <code>null</code> if this message does not need
  221. * to be localized.
  222. */
  223. public ResourceBundle getResourceBundle()
  224. {
  225. return bundle;
  226. }
  227. /**
  228. * Sets the resource bundle that is used when the message
  229. * of this <code>LogRecord</code> needs to be localized.
  230. *
  231. * <p>As soon as a <code>LogRecord</code> has been handed over
  232. * to the logging framework, applications should not modify it
  233. * anymore. Therefore, this method should only be called on
  234. * freshly constructed LogRecords.
  235. *
  236. * @param bundle the resource bundle to be used, or
  237. * <code>null</code> to indicate that this
  238. * message does not need to be localized.
  239. */
  240. public void setResourceBundle(ResourceBundle bundle)
  241. {
  242. this.bundle = bundle;
  243. /* FIXME: Is there a way to infer the name
  244. * of a resource bundle from a ResourceBundle object?
  245. */
  246. this.resourceBundleName = null;
  247. }
  248. /**
  249. * Returns the name of the resource bundle that is used when the
  250. * message of this <code>LogRecord</code> needs to be localized.
  251. *
  252. * @return the name of the resource bundle used for localization,
  253. * or <code>null</code> if this message does not need
  254. * to be localized.
  255. */
  256. public String getResourceBundleName()
  257. {
  258. return resourceBundleName;
  259. }
  260. /**
  261. * Sets the name of the resource bundle that is used when the
  262. * message of this <code>LogRecord</code> needs to be localized.
  263. *
  264. * <p>As soon as a <code>LogRecord</code> has been handed over
  265. * to the logging framework, applications should not modify it
  266. * anymore. Therefore, this method should only be called on
  267. * freshly constructed LogRecords.
  268. *
  269. * @param name the name of the resource bundle to be used, or
  270. * <code>null</code> to indicate that this message
  271. * does not need to be localized.
  272. */
  273. public void setResourceBundleName(String name)
  274. {
  275. resourceBundleName = name;
  276. bundle = null;
  277. try
  278. {
  279. if (resourceBundleName != null)
  280. bundle = ResourceBundle.getBundle(resourceBundleName);
  281. }
  282. catch (java.util.MissingResourceException _)
  283. {
  284. }
  285. }
  286. /**
  287. * Returns the level of the LogRecord.
  288. *
  289. * <p>Applications should be aware of the possibility that the
  290. * result is not necessarily one of the standard logging levels,
  291. * since the logging framework allows to create custom subclasses
  292. * of <code>java.util.logging.Level</code>. Therefore, filters
  293. * should perform checks like <code>theRecord.getLevel().intValue()
  294. * == Level.INFO.intValue()</code> instead of <code>theRecord.getLevel()
  295. * == Level.INFO</code>.
  296. */
  297. public Level getLevel()
  298. {
  299. return level;
  300. }
  301. /**
  302. * Sets the severity level of this <code>LogRecord</code> to a new
  303. * value.
  304. *
  305. * <p>As soon as a <code>LogRecord</code> has been handed over
  306. * to the logging framework, applications should not modify it
  307. * anymore. Therefore, this method should only be called on
  308. * freshly constructed LogRecords.
  309. *
  310. * @param level the new severity level, for example
  311. * <code>Level.WARNING</code>.
  312. */
  313. public void setLevel(Level level)
  314. {
  315. this.level = level;
  316. }
  317. /**
  318. * The last used sequence number for any LogRecord.
  319. */
  320. private static long lastSeqNum;
  321. /**
  322. * Allocates a sequence number for a new LogRecord. This class
  323. * method is only called by the LogRecord constructor.
  324. */
  325. private static synchronized long allocateSeqNum()
  326. {
  327. lastSeqNum += 1;
  328. return lastSeqNum;
  329. }
  330. /**
  331. * Returns the sequence number of this <code>LogRecord</code>.
  332. */
  333. public long getSequenceNumber()
  334. {
  335. return sequenceNumber;
  336. }
  337. /**
  338. * Sets the sequence number of this <code>LogRecord</code> to a new
  339. * value.
  340. *
  341. * <p>As soon as a <code>LogRecord</code> has been handed over
  342. * to the logging framework, applications should not modify it
  343. * anymore. Therefore, this method should only be called on
  344. * freshly constructed LogRecords.
  345. *
  346. * @param seqNum the new sequence number.
  347. */
  348. public void setSequenceNumber(long seqNum)
  349. {
  350. this.sequenceNumber = seqNum;
  351. }
  352. /**
  353. * Returns the name of the class where the event being logged
  354. * has had its origin. This information can be passed as
  355. * parameter to some logging calls, and in certain cases, the
  356. * logging framework tries to determine an approximation
  357. * (which may or may not be accurate).
  358. *
  359. * @return the name of the class that issued the logging request,
  360. * or <code>null</code> if this information could not
  361. * be obtained.
  362. */
  363. public String getSourceClassName()
  364. {
  365. if (sourceClassName != null)
  366. return sourceClassName;
  367. /* FIXME: Should infer this information from the call stack. */
  368. return null;
  369. }
  370. /**
  371. * Sets the name of the class where the event being logged
  372. * has had its origin.
  373. *
  374. * <p>As soon as a <code>LogRecord</code> has been handed over
  375. * to the logging framework, applications should not modify it
  376. * anymore. Therefore, this method should only be called on
  377. * freshly constructed LogRecords.
  378. *
  379. * @param sourceClassName the name of the class that issued the
  380. * logging request, or <code>null</code> to indicate that
  381. * this information could not be obtained.
  382. */
  383. public void setSourceClassName(String sourceClassName)
  384. {
  385. this.sourceClassName = sourceClassName;
  386. }
  387. /**
  388. * Returns the name of the method where the event being logged
  389. * has had its origin. This information can be passed as
  390. * parameter to some logging calls, and in certain cases, the
  391. * logging framework tries to determine an approximation
  392. * (which may or may not be accurate).
  393. *
  394. * @return the name of the method that issued the logging request,
  395. * or <code>null</code> if this information could not
  396. * be obtained.
  397. */
  398. public String getSourceMethodName()
  399. {
  400. if (sourceMethodName != null)
  401. return sourceMethodName;
  402. /* FIXME: Should infer this information from the call stack. */
  403. return null;
  404. }
  405. /**
  406. * Sets the name of the method where the event being logged
  407. * has had its origin.
  408. *
  409. * <p>As soon as a <code>LogRecord</code> has been handed over
  410. * to the logging framework, applications should not modify it
  411. * anymore. Therefore, this method should only be called on
  412. * freshly constructed LogRecords.
  413. *
  414. * @param sourceMethodName the name of the method that issued the
  415. * logging request, or <code>null</code> to indicate that
  416. * this information could not be obtained.
  417. */
  418. public void setSourceMethodName(String sourceMethodName)
  419. {
  420. this.sourceMethodName = sourceMethodName;
  421. }
  422. /**
  423. * Returns the message for this <code>LogRecord</code> before
  424. * any localization or parameter substitution.
  425. *
  426. * <p>A {@link Logger} will try to localize the message
  427. * if a resource bundle has been associated with this
  428. * <code>LogRecord</code>. In this case, the logger will call
  429. * <code>getMessage()</code> and use the result as the key
  430. * for looking up the localized message in the bundle.
  431. * If no bundle has been associated, or if the result of
  432. * <code>getMessage()</code> is not a valid key in the
  433. * bundle, the logger will use the raw message text as
  434. * returned by this method.
  435. *
  436. * @return the message text, or <code>null</code> if there
  437. * is no message text.
  438. */
  439. public String getMessage()
  440. {
  441. return message;
  442. }
  443. /**
  444. * Sets the message for this <code>LogRecord</code>.
  445. *
  446. * <p>A <code>Logger</code> will try to localize the message
  447. * if a resource bundle has been associated with this
  448. * <code>LogRecord</code>. In this case, the logger will call
  449. * <code>getMessage()</code> and use the result as the key
  450. * for looking up the localized message in the bundle.
  451. * If no bundle has been associated, or if the result of
  452. * <code>getMessage()</code> is not a valid key in the
  453. * bundle, the logger will use the raw message text as
  454. * returned by this method.
  455. *
  456. * <p>It is possible to set the message to either an empty String or
  457. * <code>null</code>, although this does not make the the message
  458. * very helpful to human users.
  459. *
  460. * @param message the message text (which will be used as key
  461. * for looking up the localized message text
  462. * if a resource bundle has been associated).
  463. */
  464. public void setMessage(String message)
  465. {
  466. this.message = message;
  467. }
  468. /**
  469. * Returns the parameters to the log message.
  470. *
  471. * @return the parameters to the message, or <code>null</code> if
  472. * the message has no parameters.
  473. */
  474. public Object[] getParameters()
  475. {
  476. return parameters;
  477. }
  478. /**
  479. * Sets the parameters to the log message.
  480. *
  481. * <p>As soon as a <code>LogRecord</code> has been handed over
  482. * to the logging framework, applications should not modify it
  483. * anymore. Therefore, this method should only be called on
  484. * freshly constructed LogRecords.
  485. *
  486. * @param parameters the parameters to the message, or <code>null</code>
  487. * to indicate that the message has no parameters.
  488. */
  489. public void setParameters(Object[] parameters)
  490. {
  491. this.parameters = parameters;
  492. }
  493. /**
  494. * Returns an identifier for the thread in which this
  495. * <code>LogRecord</code> was created. The identifier is not
  496. * necessarily related to any thread identifiers used by the
  497. * operating system.
  498. *
  499. * @return an identifier for the source thread.
  500. */
  501. public int getThreadID()
  502. {
  503. return threadID;
  504. }
  505. /**
  506. * Sets the identifier indicating in which thread this
  507. * <code>LogRecord</code> was created. The identifier is not
  508. * necessarily related to any thread identifiers used by the
  509. * operating system.
  510. *
  511. * <p>As soon as a <code>LogRecord</code> has been handed over
  512. * to the logging framework, applications should not modify it
  513. * anymore. Therefore, this method should only be called on
  514. * freshly constructed LogRecords.
  515. *
  516. * @param threadID the identifier for the source thread.
  517. */
  518. public void setThreadID(int threadID)
  519. {
  520. this.threadID = threadID;
  521. }
  522. /**
  523. * Returns the time when this <code>LogRecord</code> was created.
  524. *
  525. * @return the time of creation in milliseconds since the beginning
  526. * of January 1, 1970.
  527. */
  528. public long getMillis()
  529. {
  530. return millis;
  531. }
  532. /**
  533. * Sets the time when this <code>LogRecord</code> was created.
  534. *
  535. * <p>As soon as a <code>LogRecord</code> has been handed over
  536. * to the logging framework, applications should not modify it
  537. * anymore. Therefore, this method should only be called on
  538. * freshly constructed LogRecords.
  539. *
  540. * @param millis the time of creation in milliseconds since the
  541. * beginning of January 1, 1970.
  542. */
  543. public void setMillis(long millis)
  544. {
  545. this.millis = millis;
  546. }
  547. /**
  548. * Returns the Throwable associated with this <code>LogRecord</code>,
  549. * or <code>null</code> if the logged event is not related to an exception
  550. * or error.
  551. */
  552. public Throwable getThrown()
  553. {
  554. return thrown;
  555. }
  556. /**
  557. * Associates this <code>LogRecord</code> with an exception or error.
  558. *
  559. * <p>As soon as a <code>LogRecord</code> has been handed over
  560. * to the logging framework, applications should not modify it
  561. * anymore. Therefore, this method should only be called on
  562. * freshly constructed LogRecords.
  563. *
  564. * @param thrown the exception or error to associate with, or
  565. * <code>null</code> if this <code>LogRecord</code>
  566. * should be made unrelated to an exception or error.
  567. */
  568. public void setThrown(Throwable thrown)
  569. {
  570. this.thrown = thrown;
  571. }
  572. }