Java Code Examples for java.util.logging.Logger.getParent()

The following are Jave code examples for showing how to use getParent() of the java.util.logging.Logger class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Pogamut3   File: SimpleLogging.java   View Source Code Vote up 6 votes
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();
	}		
}
 
Example 2
Project: Pogamut3   File: SimpleLogging.java   View Source Code Vote up 6 votes
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();
	}	
}
 
Example 3
Project: ProjectAres   File: ClassLogger.java   View Source Code Vote up 6 votes
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;
    }
}
 
Example 4
Project: alvisnlp   File: AbstractAlvisRE.java   View Source Code Vote up 6 votes
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;
}
 
Example 5
Project: incubator-netbeans   File: EmbedderFactory.java   View Source Code Vote up 5 votes
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;
        }
    }
}
 
Example 6
Project: Pogamut3   File: SimpleLogging.java   View Source Code Vote up 5 votes
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);
	}
	
}
 
Example 7
Project: ProjectAres   File: Logging.java   View Source Code Vote up 5 votes
public static Level getEffectiveLevel(Logger logger) {
    if(logger.getLevel() != null) {
        return logger.getLevel();
    }

    if(logger.getParent() != null) {
        return getEffectiveLevel(logger.getParent());
    }

    return null;
}
 
Example 8
Project: jdk8u-jdk   File: TestSetResourceBundle.java   View Source Code Vote up 5 votes
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());
}
 
Example 9
Project: jdk8u-jdk   File: TestLoggingWithMainAppContext.java   View Source Code Vote up 5 votes
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.");
}
 
Example 10
Project: jdk8u-jdk   File: TestAppletLoggerContext.java   View Source Code Vote up 5 votes
public static void testParent(Logger logger) {
    Logger l = logger;
    while (l.getParent() != null) {
        l = l.getParent();
    }
    assertEquals("", l.getName());
}
 
Example 11
Project: openjdk-jdk10   File: TestSetResourceBundle.java   View Source Code Vote up 5 votes
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());
}
 
Example 12
Project: openjdk-jdk10   File: TestAnonymousLogger.java   View Source Code Vote up 5 votes
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());
    }
}
 
Example 13
Project: openjdk-jdk10   File: TestLoggingWithMainAppContext.java   View Source Code Vote up 5 votes
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.");
}
 
Example 14
Project: openjdk-jdk10   File: TestAppletLoggerContext.java   View Source Code Vote up 5 votes
public static void testParent(Logger logger) {
    Logger l = logger;
    while (l.getParent() != null) {
        l = l.getParent();
    }
    assertEquals("", l.getName());
}
 
Example 15
Project: monarch   File: LogWrapper.java   View Source Code Vote up 4 votes
public void setParentFor(Logger otherLogger) {
  if (otherLogger.getParent() != logger) {
    otherLogger.setParent(logger);
  }
}
 
Example 16
Project: jdk8u-jdk   File: TestAnonymousLogger.java   View Source Code Vote up 4 votes
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());
    }
}