本文整理汇总了Java中java.util.logging.Logger.getParent方法的典型用法代码示例。如果您正苦于以下问题:Java Logger.getParent方法的具体用法?Java Logger.getParent怎么用?Java Logger.getParent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.logging.Logger
的用法示例。
在下文中一共展示了Logger.getParent方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initLogging
import java.util.logging.Logger; //导入方法依赖的package包/类
public static void initLogging(boolean time) {
Logger log = Logger.getAnonymousLogger();
while (log != null) {
for (Handler handler : log.getHandlers()) {
log.removeHandler(handler);
}
if (log.getParent() == null) {
if (time) {
log.addHandler(new TimeLogHandler());
} else {
log.addHandler(new SimpleLogHandler());
}
}
log = log.getParent();
}
}
示例2: addConsoleLogging
import java.util.logging.Logger; //导入方法依赖的package包/类
public static void addConsoleLogging() {
Logger log = Logger.getAnonymousLogger();
while (log != null) {
if (log.getParent() == null) {
log.addHandler(new Handler() {
@Override
public void close() throws SecurityException {
}
@Override
public void flush() {
}
@Override
public void publish(LogRecord record) {
System.out.println("[" + record.getLevel() + "] " + record.getMessage());
}
});
}
log = log.getParent();
}
}
示例3: find
import java.util.logging.Logger; //导入方法依赖的package包/类
public static @Nullable ClassLogger find(@Nullable Logger parent, Class<?> klass, @Nullable String instanceKey) {
if(parent == null) {
parent = Logger.getLogger("");
}
String name = getName(klass, instanceKey);
if(parent instanceof ClassLogger && Objects.equals(parent.getName(), name)) {
// If the given parent logger looks exactly like the logger
// we are supposed to return, just use it. This makes it easy
// to replace a parent logger with a child once only e.g.
//
// logger = ClassLogger.get(logger, getClass(), "myInstance")
return (ClassLogger) parent;
}
LogManager lm = LogManager.getLogManager();
Logger logger = lm.getLogger(name);
if(logger instanceof ClassLogger) {
if(parent != logger.getParent()) {
throw new IllegalStateException("Already registred logger " + name + " has a different parent than the one requested:\n old = " + logger.getParent() + "\n new = " + parent);
}
return (ClassLogger) logger;
} else {
return null;
}
}
示例4: getVerbosity
import java.util.logging.Logger; //导入方法依赖的package包/类
private static int getVerbosity(Logger logger) throws ProcessingException {
Level level = null;
do {
if (logger == null) {
processingException("logger is null");
}
level = logger.getLevel();
logger = logger.getParent();
}
while (level == null);
int levelValue = level.intValue();
if (levelValue == Level.SEVERE.intValue()) {
return 0;
}
if (levelValue == Level.WARNING.intValue()) {
return 1;
}
if (levelValue == Level.INFO.intValue()) {
return 2;
}
return 3;
}
示例5: levelOf
import java.util.logging.Logger; //导入方法依赖的package包/类
private Level levelOf(Logger log) {
Level lvl = log.getLevel();
if (lvl != null) {
return lvl;
} else {
Logger par = log.getParent();
if (par != null) {
return levelOf(par);
} else {
return Level.INFO;
}
}
}
示例6: addFileLogging
import java.util.logging.Logger; //导入方法依赖的package包/类
public static void addFileLogging(final String pathToFile) {
Logger log = Logger.getAnonymousLogger();
try {
final PrintWriter writer = new PrintWriter(new FileWriter(new File(pathToFile)));
while (log != null) {
if (log.getParent() == null) {
log.addHandler(new Handler() {
@Override
public void close() throws SecurityException {
writer.close();
}
@Override
public void flush() {
writer.flush();
}
@Override
public void publish(LogRecord record) {
writer.println("[" + record.getLevel() + "] " + record.getMessage());
}
});
}
log = log.getParent();
}
} catch (IOException e) {
throw new RuntimeException("Could not open file " + new File(pathToFile).getAbsolutePath() + " for logging!", e);
}
}
示例7: getEffectiveLevel
import java.util.logging.Logger; //导入方法依赖的package包/类
public static Level getEffectiveLevel(Logger logger) {
if(logger.getLevel() != null) {
return logger.getLevel();
}
if(logger.getParent() != null) {
return getEffectiveLevel(logger.getParent());
}
return null;
}
示例8: debugLogger
import java.util.logging.Logger; //导入方法依赖的package包/类
static void debugLogger(Logger logger, Logger expectedParent, TestHandler handler) {
final String logName = logger.getName();
final String prefix = " " + logName;
System.err.println("Logger " + logName
+ " logged with bundle name " + handler.lastBundleName
+ " (" + handler.lastBundle + ")");
System.err.println(prefix + ".getResourceBundleName() is "
+ logger.getResourceBundleName());
System.err.println(prefix + ".getResourceBundle() is "
+ logger.getResourceBundle());
final Logger parent = logger.getParent();
final String pname = parent == null ? null : parent.getName();
final String pclass = parent == null ? ""
: ("(" + parent.getClass().getName() + ")");
final String presn = parent == null ? null
: parent.getResourceBundleName();
final ResourceBundle pres = parent == null ? null
: parent.getResourceBundle();
System.err.println(prefix + ".getParent() is "
+ pname + (pname == null ? ""
: (" " + pclass + ": " + parent)));
System.err.println(" expected parent is :" + expectedParent);
System.err.println(prefix + ".parent.getResourceBundleName() is "
+ presn);
System.err.println(prefix + ".parent.getResourceBundle() is "
+ pres);
System.err.println(" expected parent getResourceBundleName() is "
+ expectedParent.getResourceBundleName());
System.err.println(" expected parent.getResourceBundle() is "
+ expectedParent.getResourceBundle());
}
示例9: main
import java.util.logging.Logger; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
System.out.println("Creating loggers.");
// These loggers will be created in the default user context.
final Logger foo1 = Logger.getLogger( "foo" );
final Logger bar1 = Logger.getLogger( "foo.bar" );
if (bar1.getParent() != foo1) {
throw new RuntimeException("Parent logger of bar1 "+bar1+" is not "+foo1);
}
System.out.println("bar1.getParent() is the same as foo1");
// Set a security manager
System.setSecurityManager(new SecurityManager());
System.out.println("Now running with security manager");
// Triggers the creation of the main AppContext
ByteArrayInputStream is = new ByteArrayInputStream(new byte[] { 0, 1 });
ImageIO.read(is); // triggers calls to system loggers & creation of main AppContext
// verify that we're still using the default user context
final Logger bar2 = Logger.getLogger( "foo.bar" );
if (bar1 != bar2) {
throw new RuntimeException("bar2 "+bar2+" is not the same as bar1 "+bar1);
}
System.out.println("bar2 is the same as bar1");
if (bar2.getParent() != foo1) {
throw new RuntimeException("Parent logger of bar2 "+bar2+" is not foo1 "+foo1);
}
System.out.println("bar2.getParent() is the same as foo1");
final Logger foo2 = Logger.getLogger("foo");
if (foo1 != foo2) {
throw new RuntimeException("foo2 "+foo2+" is not the same as foo1 "+foo1);
}
System.out.println("foo2 is the same as foo1");
System.out.println("Test passed.");
}
示例10: testParent
import java.util.logging.Logger; //导入方法依赖的package包/类
public static void testParent(Logger logger) {
Logger l = logger;
while (l.getParent() != null) {
l = l.getParent();
}
assertEquals("", l.getName());
}
示例11: main
import java.util.logging.Logger; //导入方法依赖的package包/类
public static void main(String[] args) {
System.setSecurityManager(new SecurityManager());
Logger anonymous = Logger.getAnonymousLogger();
final TestHandler handler = new TestHandler();
final TestFilter filter = new TestFilter();
final ResourceBundle bundle = ResourceBundle.getBundle(TestBundle.class.getName());
anonymous.setLevel(Level.FINEST);
anonymous.addHandler(handler);
anonymous.setFilter(filter);
anonymous.setUseParentHandlers(true);
anonymous.setResourceBundle(bundle);
if (anonymous.getLevel() != Level.FINEST) {
throw new RuntimeException("Unexpected level: " + anonymous.getLevel());
} else {
System.out.println("Got expected level: " + anonymous.getLevel());
}
if (!Arrays.asList(anonymous.getHandlers()).contains(handler)) {
throw new RuntimeException("Expected handler not found in: "
+ Arrays.asList(anonymous.getHandlers()));
} else {
System.out.println("Got expected handler in: " + Arrays.asList(anonymous.getHandlers()));
}
if (anonymous.getFilter() != filter) {
throw new RuntimeException("Unexpected filter: " + anonymous.getFilter());
} else {
System.out.println("Got expected filter: " + anonymous.getFilter());
}
if (!anonymous.getUseParentHandlers()) {
throw new RuntimeException("Unexpected flag: " + anonymous.getUseParentHandlers());
} else {
System.out.println("Got expected flag: " + anonymous.getUseParentHandlers());
}
if (anonymous.getResourceBundle() != bundle) {
throw new RuntimeException("Unexpected bundle: " + anonymous.getResourceBundle());
} else {
System.out.println("Got expected bundle: " + anonymous.getResourceBundle());
}
try {
anonymous.setParent(Logger.getLogger("foo.bar"));
throw new RuntimeException("Expected SecurityException not raised!");
} catch (SecurityException x) {
System.out.println("Got expected exception: " + x);
}
if (anonymous.getParent() != Logger.getLogger("")) {
throw new RuntimeException("Unexpected parent: " + anonymous.getParent());
} else {
System.out.println("Got expected parent: " + anonymous.getParent());
}
}
示例12: setParentFor
import java.util.logging.Logger; //导入方法依赖的package包/类
public void setParentFor(Logger otherLogger) {
if (otherLogger.getParent() != logger) {
otherLogger.setParent(logger);
}
}