/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * Custom LogManager implementation to verify that the implementation delegates * to the LogManager subclass to register both system logger and user logger. * * The LogManager implementation is the one configuring the logger's property * such as level, handler, etc.
*/ publicclass CustomLogManager extends LogManager { static LogManager INSTANCE;
Map<String,Logger> namedLoggers = new HashMap<>();
Properties props = initConfig(); public CustomLogManager() { if (INSTANCE != null) { thrownew RuntimeException("CustomLogManager already created");
}
INSTANCE = this;
}
privateboolean useParentHandlers(String loggerName) {
String s = props.getProperty(loggerName + ".useParentHandlers"); if (s == null) returntrue; // default is true
s = s.toLowerCase(); if (s.equals("true") || s.equals("1")) { returntrue;
} elseif (s.equals("false") || s.equals("0")) { returnfalse;
} returntrue;
}
publicsynchronizedboolean addLogger(Logger logger) {
String name = logger.getName(); if (namedLoggers.containsKey(name)) { returnfalse;
}
namedLoggers.put(name, logger); // set level if (props.get(name + ".level") != null) {
logger.setLevel(Level.parse(props.getProperty(name + ".level")));
} // add handlers if (props.get(name + ".handlers") != null && logger.getHandlers().length == 0) {
logger.addHandler(new CustomHandler());
} if (!useParentHandlers(name)) {
logger.setUseParentHandlers(false);
} // add parent loggers int ix = 1; for (;;) { int ix2 = name.indexOf(".", ix); if (ix2 < 0) { break;
}
String pname = name.substring(0, ix2); if (props.get(pname + ".level") != null ||
props.get(pname + ".handlers") != null) { // This pname has a level/handlers definition. // Make sure it exists. // // The test doesn't set the parent for simplicity. if (!namedLoggers.containsKey(pname)) {
Logger parent = Logger.getLogger(pname); if (!useParentHandlers(pname)) {
parent.setUseParentHandlers(false);
}
}
}
ix = ix2 + 1;
} returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.