1. /*
  2. * @(#)ConsoleHandler.java 1.9 03/01/23
  3. *
  4. * Copyright 2003 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.net.*;
  10. /**
  11. * This <tt>Handler</tt> publishes log records to <tt>System.err</tt>.
  12. * By default the <tt>SimpleFormatter</tt> is used to generate brief summaries.
  13. * <p>
  14. * <b>Configuration:</b>
  15. * By default each <tt>ConsoleHandler</tt> is initialized using the following
  16. * <tt>LogManager</tt> configuration properties. If properties are not defined
  17. * (or have invalid values) then the specified default values are used.
  18. * <ul>
  19. * <li> java.util.logging.ConsoleHandler.level
  20. * specifies the default level for the <tt>Handler</tt>
  21. * (defaults to <tt>Level.INFO</tt>).
  22. * <li> java.util.logging.ConsoleHandler.filter
  23. * specifies the name of a <tt>Filter</tt> class to use
  24. * (defaults to no <tt>Filter</tt>).
  25. * <li> java.util.logging.ConsoleHandler.formatter
  26. * specifies the name of a <tt>Formatter</tt> class to use
  27. * (defaults to <tt>java.util.logging.SimpleFormatter</tt>).
  28. * <li> java.util.logging.ConsoleHandler.encoding
  29. * the name of the character set encoding to use (defaults to
  30. * the default platform encoding).
  31. * </ul>
  32. * <p>
  33. * @version 1.9, 01/23/03
  34. * @since 1.4
  35. */
  36. public class ConsoleHandler extends StreamHandler {
  37. // Private method to configure a ConsoleHandler from LogManager
  38. // properties and/or default values as specified in the class
  39. // javadoc.
  40. private void configure() {
  41. LogManager manager = LogManager.getLogManager();
  42. String cname = ConsoleHandler.class.getName();
  43. setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
  44. setFilter(manager.getFilterProperty(cname +".filter", null));
  45. setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
  46. try {
  47. setEncoding(manager.getStringProperty(cname +".encoding", null));
  48. } catch (Exception ex) {
  49. try {
  50. setEncoding(null);
  51. } catch (Exception ex2) {
  52. // doing a setEncoding with null should always work.
  53. // assert false;
  54. }
  55. }
  56. }
  57. /**
  58. * Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>.
  59. * <p>
  60. * The <tt>ConsoleHandler</tt> is configured based on
  61. * <tt>LogManager</tt> properties (or their default values).
  62. *
  63. */
  64. public ConsoleHandler() {
  65. sealed = false;
  66. configure();
  67. setOutputStream(System.err);
  68. sealed = true;
  69. }
  70. /**
  71. * Publish a <tt>LogRecord</tt>.
  72. * <p>
  73. * The logging request was made initially to a <tt>Logger</tt> object,
  74. * which initialized the <tt>LogRecord</tt> and forwarded it here.
  75. * <p>
  76. * @param record description of the log event
  77. */
  78. public void publish(LogRecord record) {
  79. super.publish(record);
  80. flush();
  81. }
  82. /**
  83. * Override <tt>StreamHandler.close</tt> to do a flush but not
  84. * to close the output stream. That is, we do <b>not</b>
  85. * close <tt>System.err</tt>.
  86. */
  87. public void close() {
  88. flush();
  89. }
  90. }