Path: blob/master/src/java.logging/share/classes/java/util/logging/Logging.java
41159 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util.logging;2627import java.util.Enumeration;28import java.util.List;29import java.util.ArrayList;3031/**32* Logging is the implementation class of LoggingMXBean.33*34* The {@code LoggingMXBean} interface provides a standard35* method for management access to the individual36* {@code Logger} objects available at runtime.37*38* @author Ron Mann39* @author Mandy Chung40* @since 1.541*42* @see javax.management43* @see Logger44* @see LogManager45*/46@SuppressWarnings("deprecation") // implements LoggingMXBean47final class Logging implements LoggingMXBean {4849private static LogManager logManager = LogManager.getLogManager();5051/** Constructor of Logging which is the implementation class52* of LoggingMXBean.53*/54private Logging() {55}5657@Override58public List<String> getLoggerNames() {59Enumeration<String> loggers = logManager.getLoggerNames();60ArrayList<String> array = new ArrayList<>();6162for (; loggers.hasMoreElements();) {63array.add(loggers.nextElement());64}65return array;66}6768private static String EMPTY_STRING = "";69@Override70public String getLoggerLevel(String loggerName) {71Logger l = logManager.getLogger(loggerName);72if (l == null) {73return null;74}7576Level level = l.getLevel();77if (level == null) {78return EMPTY_STRING;79} else {80return level.getLevelName();81}82}8384@Override85public void setLoggerLevel(String loggerName, String levelName) {86if (loggerName == null) {87throw new NullPointerException("loggerName is null");88}8990Logger logger = logManager.getLogger(loggerName);91if (logger == null) {92throw new IllegalArgumentException("Logger " + loggerName +93" does not exist");94}9596Level level = null;97if (levelName != null) {98// parse will throw IAE if logLevel is invalid99level = Level.findLevel(levelName);100if (level == null) {101throw new IllegalArgumentException("Unknown level \"" + levelName + "\"");102}103}104105logger.setLevel(level);106}107108@Override109public String getParentLoggerName( String loggerName ) {110Logger l = logManager.getLogger( loggerName );111if (l == null) {112return null;113}114115Logger p = l.getParent();116if (p == null) {117// root logger118return EMPTY_STRING;119} else {120return p.getName();121}122}123124static Logging getInstance() {125return INSTANCE;126}127128private static final Logging INSTANCE = new Logging();129130}131132133