1. /*
  2. * @(#)Logging.java 1.5 04/04/18
  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.util.Enumeration;
  9. import java.util.List;
  10. import java.util.ArrayList;
  11. import sun.management.MXBeanSupport;
  12. /**
  13. * Logging is the implementation class of LoggingMXBean.
  14. *
  15. * The <tt>LoggingMXBean</tt> interface provides a standard
  16. * method for management access to the individual
  17. * java.util.Logger objects available at runtime.
  18. *
  19. * @author Ron Mann
  20. * @author Mandy Chung
  21. * @version 1.5, 04/18/04
  22. * @since 1.5
  23. *
  24. * @see javax.management
  25. * @see java.util.Logger
  26. * @see java.util.LogManager
  27. */
  28. class Logging extends MXBeanSupport implements LoggingMXBean {
  29. private static LogManager logManager = LogManager.getLogManager();
  30. /** Contructor of Logging which is the implementation class
  31. * of LoggingMXBean.
  32. */
  33. Logging() {
  34. super(LoggingMXBean.class);
  35. }
  36. public List<String> getLoggerNames() {
  37. Enumeration loggers = logManager.getLoggerNames();
  38. ArrayList<String> array = new ArrayList<String>();
  39. for (; loggers.hasMoreElements();) {
  40. array.add((String) loggers.nextElement());
  41. }
  42. return array;
  43. }
  44. private static String EMPTY_STRING = "";
  45. public String getLoggerLevel(String loggerName) {
  46. Logger l = logManager.getLogger(loggerName);
  47. if (l == null) {
  48. return null;
  49. }
  50. Level level = l.getLevel();
  51. if (level == null) {
  52. return EMPTY_STRING;
  53. } else {
  54. return level.getName();
  55. }
  56. }
  57. public void setLoggerLevel(String loggerName, String levelName) {
  58. if (loggerName == null) {
  59. throw new NullPointerException("loggerName is null");
  60. }
  61. Logger logger = logManager.getLogger(loggerName);
  62. if (logger == null) {
  63. throw new IllegalArgumentException("Logger " + loggerName +
  64. "does not exist");
  65. }
  66. Level level = null;
  67. if (levelName != null) {
  68. // parse will throw IAE if logLevel is invalid
  69. level = Level.parse(levelName);
  70. }
  71. logger.setLevel(level);
  72. }
  73. public String getParentLoggerName( String loggerName ) {
  74. Logger l = logManager.getLogger( loggerName );
  75. if (l == null) {
  76. return null;
  77. }
  78. Logger p = l.getParent();
  79. if (p == null) {
  80. // root logger
  81. return EMPTY_STRING;
  82. } else {
  83. return p.getName();
  84. }
  85. }
  86. }