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

The following examples show how to use java.util.logging.Logger#getParent() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sis   File: Loggers.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the finest level of registered handlers for the given logger.
 * This method verifies also in the parent handlers if the logger use them.
 */
private static Level getHandlerLevel(Logger logger) {
    Level level = Level.OFF;
    while (logger != null) {
        for (final Handler handler : logger.getHandlers()) {
            final Level c = handler.getLevel();
            if (c != null && c.intValue() < level.intValue()) {
                level = c;
            }
        }
        if (!logger.getUseParentHandlers()) {
            break;
        }
        logger = logger.getParent();
    }
    return level;
}
 
Example 2
Source Project: birt   File: LoggerSettingTest.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private void verifyResult(Level level, int handlerNum)
{
	// find the first logger in hierarchy with level set
	Logger bl = Logger.getLogger("org.eclipse.birt.report.engine.api.impl");
	while(bl != null && bl.getLevel() == null) {
		bl = bl.getParent();
	}
	assertNotNull(bl);

	if(level == null) {
		if(bl.getLevel() != null) {
			assertEquals(Level.INFO, bl.getLevel());
		}
		assertTrue(bl.getHandlers().length <= 1);
	} else {
		assertEquals(level, bl.getLevel());
		assertEquals( handlerNum, bl.getHandlers().length );
	}
}
 
Example 3
/**
 * Retrieves a list of root loggers.
 *
 * @return list of root loggers.
 */
private Set<Logger> getRootLoggers() {
    final LogManager logManager = LogManager.getLogManager();
    final Enumeration<String> loggerNames = logManager.getLoggerNames();

    final Set<Logger> rootLoggers = Sets.newHashSet();

    while (loggerNames.hasMoreElements()) {
        Logger logger = logManager.getLogger(loggerNames.nextElement());
        if (logger != null) {
            while (logger.getParent() != null) {
                logger = logger.getParent();
            }
            rootLoggers.add(logger);
        }
    }

    return rootLoggers;
}
 
Example 4
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 5
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 6
public static void testParent(Logger logger) {
    Logger l = logger;
    while (l.getParent() != null) {
        l = l.getParent();
    }
    assertEquals("", l.getName());
}
 
Example 7
Source Project: ignite   File: JavaLogger.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns first found handler of specified class type or {@code null} if that handler isn't configured.
 *
 * @param log Logger.
 * @param cls Class.
 * @param <T> Class type.
 * @return First found handler of specified class type or {@code null} if that handler isn't configured.
 */
private static <T> T findHandler(Logger log, Class<T> cls) {
    while (log != null) {
        for (Handler hnd : log.getHandlers()) {
            if (cls.isInstance(hnd))
                return (T)hnd;
        }

        log = log.getParent();
    }

    return null;
}
 
Example 8
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
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
public static Level getEffectiveLevel(Logger logger) {
    if(logger.getLevel() != null) {
        return logger.getLevel();
    }

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

    return null;
}
 
Example 11
Source Project: birt   File: EngineLogger.java    License: Eclipse Public License 1.0 5 votes vote down vote up
private void publishToLogger( Logger logger, LogRecord record )
{
	if ( !logger.isLoggable( record.getLevel( ) ) )
	{
		return;
	}
	synchronized ( logger )
	{
		Filter filter = logger.getFilter( );
		if ( filter != null && !filter.isLoggable( record ) )
		{
			return;
		}
	}
	// Post the LogRecord to all our Handlers, and then to
	// our parents' handlers, all the way up the tree.

	while ( logger != null )
	{
		Handler targets[] = logger.getHandlers( );

		if ( targets != null )
		{
			for ( int i = 0; i < targets.length; i++ )
			{
				targets[i].publish( record );
			}
		}

		if ( !logger.getUseParentHandlers( ) )
		{
			break;
		}

		logger = logger.getParent( );
	}
}
 
Example 12
Source Project: Canova   File: Util.java    License: Apache License 2.0 5 votes vote down vote up
public static Level disableLogging() {
    Logger logger = Logger.getLogger("org.apache.uima");
    while (logger.getLevel() == null) {
        logger = logger.getParent();
    }
    Level level = logger.getLevel();
    logger.setLevel(Level.OFF);
    return level;
}
 
Example 13
public static void testParent(Logger logger) {
    Logger l = logger;
    while (l.getParent() != null) {
        l = l.getParent();
    }
    assertEquals("", l.getName());
}
 
Example 14
Source Project: flogger   File: AbstractBackend.java    License: Apache License 2.0 5 votes vote down vote up
private static void publish(Logger logger, LogRecord record) {
  // Annoyingly this method appears to copy the array every time it is called, but there's
  // nothing much we can do about this (and there could be synchronization issues even if we
  // could access things directly because handlers can be changed at any time). Most of the
  // time this returns the singleton empty array however, so it's not as bad as all that.
  for (Handler handler : logger.getHandlers()) {
    handler.publish(record);
  }
  if (logger.getUseParentHandlers()) {
    logger = logger.getParent();
    if (logger != null) {
      publish(logger, record);
    }
  }
}
 
Example 15
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 16
public static void testParent(Logger logger) {
    Logger l = logger;
    while (l.getParent() != null) {
        l = l.getParent();
    }
    assertEquals("", l.getName());
}
 
Example 17
Source Project: netbeans   File: EmbedderFactory.java    License: Apache License 2.0 5 votes vote down vote up
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 18
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 19
Source Project: sis   File: Loggers.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Returns a map of effective logging levels for SIS loggers. The effective logging level take in account the level
 * of parent loggers and the level of handlers. For example if a logger level is set to {@link Level#FINE} but no
 * handler have a level finer than {@link Level#INFO}, then the effective logging level will be {@link Level#INFO}.
 *
 * <p>This method does not report the loggers that have an effective level identical to its parent logger.</p>
 *
 * @return the effective logging levels of SIS loggers.
 */
public static SortedMap<String,Level> getEffectiveLevels() {
    final SortedMap<String,Level> levels = new TreeMap<>();
    for (final Field field : Loggers.class.getDeclaredFields()) {
        if (Modifier.isStatic(field.getModifiers()) && field.getType() == String.class) try {
            levels.put((String) field.get(null), null);
        } catch (IllegalAccessException e) {
            /*
             * Should never happen, unless we added some fields and forgot to update this method.
             * In such case forget the problematic fields and search the next one. This is okay
             * since this method is only for information purpose.
             */
            Logging.unexpectedException(Logging.getLogger(SYSTEM), Loggers.class, "getEffectiveLevels", e);
        }
    }
    /*
     * Process the loggers in alphabetical order. The intent is to process parent loggers before child.
     * The first logger in the map should be the SIS root logger, "org.apache.sis".
     */
    final Iterator<Map.Entry<String,Level>> it = levels.entrySet().iterator();
    while (it.hasNext()) {
        final Map.Entry<String,Level> entry = it.next();
        final String name = entry.getKey();
        final Logger logger = Logging.getLogger(name);
        Level level = getEffectiveLevel(logger);
        final Level h = getHandlerLevel(logger);
        if (h.intValue() > level.intValue()) {
            level = h;                              // Take in account the logging level of handlers.
        }
        entry.setValue(level);
        /*
         * Now verify if the level is identical to the effective level of parent logger.
         * If they are identical, then we remove the entry in order to report only the changes.
         */
        Logger parent = logger;
        while ((parent = parent.getParent()) != null) {
            final Level p = levels.get(parent.getName());
            if (p != null) {
                if (p.equals(level)) {
                    it.remove();
                }
                break;
            }
        }
    }
    return levels;
}
 
Example 20
Source Project: gemfirexd-oss   File: LogWrapper.java    License: Apache License 2.0 4 votes vote down vote up
public void setParentFor(Logger otherLogger) {
  if (otherLogger.getParent() != logger) {
    otherLogger.setParent(logger);
  }
}