1. /*
  2. * @(#)SimpleFormatter.java 1.14 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.logging;
  8. import java.io.*;
  9. import java.text.*;
  10. import java.util.Date;
  11. /**
  12. * Print a brief summary of the LogRecord in a human readable
  13. * format. The summary will typically be 1 or 2 lines.
  14. *
  15. * @version 1.14, 12/19/03
  16. * @since 1.4
  17. */
  18. public class SimpleFormatter extends Formatter {
  19. Date dat = new Date();
  20. private final static String format = "{0,date} {0,time}";
  21. private MessageFormat formatter;
  22. private Object args[] = new Object[1];
  23. // Line separator string. This is the value of the line.separator
  24. // property at the moment that the SimpleFormatter was created.
  25. private String lineSeparator = (String) java.security.AccessController.doPrivileged(
  26. new sun.security.action.GetPropertyAction("line.separator"));
  27. /**
  28. * Format the given LogRecord.
  29. * @param record the log record to be formatted.
  30. * @return a formatted log record
  31. */
  32. public synchronized String format(LogRecord record) {
  33. StringBuffer sb = new StringBuffer();
  34. // Minimize memory allocations here.
  35. dat.setTime(record.getMillis());
  36. args[0] = dat;
  37. StringBuffer text = new StringBuffer();
  38. if (formatter == null) {
  39. formatter = new MessageFormat(format);
  40. }
  41. formatter.format(args, text, null);
  42. sb.append(text);
  43. sb.append(" ");
  44. if (record.getSourceClassName() != null) {
  45. sb.append(record.getSourceClassName());
  46. } else {
  47. sb.append(record.getLoggerName());
  48. }
  49. if (record.getSourceMethodName() != null) {
  50. sb.append(" ");
  51. sb.append(record.getSourceMethodName());
  52. }
  53. sb.append(lineSeparator);
  54. String message = formatMessage(record);
  55. sb.append(record.getLevel().getLocalizedName());
  56. sb.append(": ");
  57. sb.append(message);
  58. sb.append(lineSeparator);
  59. if (record.getThrown() != null) {
  60. try {
  61. StringWriter sw = new StringWriter();
  62. PrintWriter pw = new PrintWriter(sw);
  63. record.getThrown().printStackTrace(pw);
  64. pw.close();
  65. sb.append(sw.toString());
  66. } catch (Exception ex) {
  67. }
  68. }
  69. return sb.toString();
  70. }
  71. }