SimpleFormatter.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* SimpleFormatter.java --
  2. A class for formatting log records into short human-readable messages
  3. Copyright (C) 2002, 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 gnu.java.lang.CPStringBuilder;
  34. import java.io.PrintWriter;
  35. import java.io.StringWriter;
  36. import java.text.DateFormat;
  37. import java.util.Date;
  38. /**
  39. * A <code>SimpleFormatter</code> formats log records into
  40. * short human-readable messages, typically one or two lines.
  41. *
  42. * @author Sascha Brawer (brawer@acm.org)
  43. */
  44. public class SimpleFormatter
  45. extends Formatter
  46. {
  47. /**
  48. * Constructs a SimpleFormatter.
  49. */
  50. public SimpleFormatter()
  51. {
  52. }
  53. /**
  54. * An instance of a DateFormatter that is used for formatting
  55. * the time of a log record into a human-readable string,
  56. * according to the rules of the current locale. The value
  57. * is set after the first invocation of format, since it is
  58. * common that a JVM will instantiate a SimpleFormatter without
  59. * ever using it.
  60. */
  61. private DateFormat dateFormat;
  62. /**
  63. * The character sequence that is used to separate lines in the
  64. * generated stream. Somewhat surprisingly, the Sun J2SE 1.4
  65. * reference implementation always uses UNIX line endings, even on
  66. * platforms that have different line ending conventions (i.e.,
  67. * DOS). The GNU implementation does not replicate this bug.
  68. *
  69. * @see Sun bug parade, bug #4462871,
  70. * "java.util.logging.SimpleFormatter uses hard-coded line separator".
  71. */
  72. static final String lineSep = System.getProperty("line.separator");
  73. /**
  74. * Formats a log record into a String.
  75. *
  76. * @param record the log record to be formatted.
  77. *
  78. * @return a short human-readable message, typically one or two
  79. * lines. Lines are separated using the default platform line
  80. * separator.
  81. *
  82. * @throws NullPointerException if <code>record</code>
  83. * is <code>null</code>.
  84. */
  85. public String format(LogRecord record)
  86. {
  87. CPStringBuilder buf = new CPStringBuilder(180);
  88. if (dateFormat == null)
  89. dateFormat = DateFormat.getDateTimeInstance();
  90. buf.append(dateFormat.format(new Date(record.getMillis())));
  91. buf.append(' ');
  92. buf.append(record.getSourceClassName());
  93. buf.append(' ');
  94. buf.append(record.getSourceMethodName());
  95. buf.append(lineSep);
  96. buf.append(record.getLevel());
  97. buf.append(": ");
  98. buf.append(formatMessage(record));
  99. buf.append(lineSep);
  100. Throwable throwable = record.getThrown();
  101. if (throwable != null)
  102. {
  103. StringWriter sink = new StringWriter();
  104. throwable.printStackTrace(new PrintWriter(sink, true));
  105. buf.append(sink.toString());
  106. }
  107. return buf.toString();
  108. }
  109. }