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

The following examples show how to use java.util.logging.Logger#getFilter() . 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 File: ProtocolNegotiatorsTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void engineLog() {
  ChannelHandler handler = new ServerTlsHandler(grpcHandler, sslContext, null);
  pipeline.addLast(handler);
  channelHandlerCtx = pipeline.context(handler);

  Logger logger = Logger.getLogger(ProtocolNegotiators.class.getName());
  Filter oldFilter = logger.getFilter();
  try {
    logger.setFilter(new Filter() {
      @Override
      public boolean isLoggable(LogRecord record) {
        // We still want to the log method to be exercised, just not printed to stderr.
        return false;
      }
    });

    ProtocolNegotiators.logSslEngineDetails(
        Level.INFO, channelHandlerCtx, "message", new Exception("bad"));
  } finally {
    logger.setFilter(oldFilter);
  }
}
 
Example 2
Source File: PerfMarkTest.java    From perfmark with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  Class<?> implClz = Class.forName("io.perfmark.impl.SecretPerfMarkImpl$PerfMarkImpl");
  Field propertyField = implClz.getDeclaredField("START_ENABLED_PROPERTY");
  propertyField.setAccessible(true);
  String startEnabledProperty = (String) propertyField.get(null);
  Logger logger = Logger.getLogger(PerfMark.class.getName());
  Filter oldFilter = logger.getFilter();
  // This causes a cycle in case PerfMark tries to log during init.
  // Also, it silences initial nagging about missing generators.
  logger.setFilter(
      new Filter() {
        @Override
        public boolean isLoggable(LogRecord record) {
          PerfMark.startTask("isLoggable");
          try {
            return false;
          } finally {
            PerfMark.stopTask("isLoggable");
          }
        }
      });
  // Try to get PerfMark to accidentally log that it is enabled.  We are careful to not
  // accidentally cause class initialization early here, as START_ENABLED_PROPERTY is a
  // constant.
  String oldProperty = System.getProperty(startEnabledProperty);
  System.setProperty(startEnabledProperty, "true");
  try {
    Class.forName(PerfMark.class.getName());
  } finally {
    if (oldProperty == null) {
      System.clearProperty(startEnabledProperty);
    } else {
      System.setProperty(startEnabledProperty, oldProperty);
    }
    logger.setFilter(oldFilter);
  }
}
 
Example 3
Source File: EngineLogger.java    From birt with 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 4
Source File: ProtocolNegotiatorsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void engineLog() {
  ChannelHandler handler = new ServerTlsHandler(sslContext, grpcHandler);
  pipeline.addLast(handler);
  channelHandlerCtx = pipeline.context(handler);

  Logger logger = Logger.getLogger(ProtocolNegotiators.class.getName());
  Filter oldFilter = logger.getFilter();
  try {
    logger.setFilter(new Filter() {
      @Override
      public boolean isLoggable(LogRecord record) {
        // We still want to the log method to be exercised, just not printed to stderr.
        return false;
      }
    });

    ProtocolNegotiators.logSslEngineDetails(
        Level.INFO, channelHandlerCtx, "message", new Exception("bad"));
  } finally {
    logger.setFilter(oldFilter);
  }
}
 
Example 5
Source File: TestAnonymousLogger.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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 6
Source File: ManagedChannelOrphanWrapperTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void orphanedChannelsAreLogged() {
  ManagedChannel mc = new TestManagedChannel();
  String channelString = mc.toString();
  final ReferenceQueue<ManagedChannelOrphanWrapper> refqueue =
      new ReferenceQueue<>();
  ConcurrentMap<ManagedChannelReference, ManagedChannelReference> refs =
      new ConcurrentHashMap<>();

  assertEquals(0, refs.size());
  @SuppressWarnings("UnusedVariable")
  ManagedChannelOrphanWrapper channel = new ManagedChannelOrphanWrapper(mc, refqueue, refs);
  assertEquals(1, refs.size());

  // Try to capture the log output but without causing terminal noise.  Adding the filter must
  // be done before clearing the ref or else it might be missed.
  final List<LogRecord> records = new ArrayList<>(1);
  Logger orphanLogger = Logger.getLogger(ManagedChannelOrphanWrapper.class.getName());
  Filter oldFilter = orphanLogger.getFilter();
  orphanLogger.setFilter(new Filter() {

    @Override
    public boolean isLoggable(LogRecord record) {
      synchronized (records) {
        records.add(record);
      }
      return false;
    }
  });

  try {
    channel = null;
    final AtomicInteger numOrphans = new AtomicInteger();
    GcFinalization.awaitDone(
        new FinalizationPredicate() {
          @Override
          public boolean isDone() {
            numOrphans.getAndAdd(ManagedChannelReference.cleanQueue(refqueue));
            return numOrphans.get() > 0;
          }
        });
    assertEquals("unexpected extra orphans", 1, numOrphans.get());

    LogRecord lr;
    synchronized (records) {
      assertEquals(1, records.size());
      lr = records.get(0);
    }
    assertThat(lr.getMessage()).contains("shutdown");
    assertThat(lr.getParameters()).asList().containsExactly(channelString).inOrder();
    assertEquals(Level.SEVERE, lr.getLevel());
    assertEquals(0, refs.size());
  } finally {
    orphanLogger.setFilter(oldFilter);
  }
}
 
Example 7
Source File: TestAnonymousLogger.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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 8
Source File: TestAnonymousLogger.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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 9
Source File: TestAnonymousLogger.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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 10
Source File: TestAnonymousLogger.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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 11
Source File: LoggerFilter.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
public LoggerFilter(final Logger l) {
	this.l = l;
	oldFilter = l.getFilter();
	l.setFilter(this);
}
 
Example 12
Source File: TestAnonymousLogger.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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
Source File: TestAnonymousLogger.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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 14
Source File: TestAnonymousLogger.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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 15
Source File: TestAnonymousLogger.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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 16
Source File: TestAnonymousLogger.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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 17
Source File: TestAnonymousLogger.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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 18
Source File: TestAnonymousLogger.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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 File: TestAnonymousLogger.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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 20
Source File: ManagedChannelOrphanWrapperTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void orphanedChannelsAreLogged() throws Exception {
  ManagedChannel mc = mock(ManagedChannel.class);
  String channelString = mc.toString();
  ReferenceQueue<ManagedChannelOrphanWrapper> refqueue =
      new ReferenceQueue<ManagedChannelOrphanWrapper>();
  ConcurrentMap<ManagedChannelReference, ManagedChannelReference> refs =
      new ConcurrentHashMap<ManagedChannelReference, ManagedChannelReference>();

  assertEquals(0, refs.size());
  ManagedChannelOrphanWrapper channel = new ManagedChannelOrphanWrapper(mc, refqueue, refs);
  assertEquals(1, refs.size());

  // Try to capture the log output but without causing terminal noise.  Adding the filter must
  // be done before clearing the ref or else it might be missed.
  final List<LogRecord> records = new ArrayList<>(1);
  Logger orphanLogger = Logger.getLogger(ManagedChannelOrphanWrapper.class.getName());
  Filter oldFilter = orphanLogger.getFilter();
  orphanLogger.setFilter(new Filter() {

    @Override
    public boolean isLoggable(LogRecord record) {
      synchronized (records) {
        records.add(record);
      }
      return false;
    }
  });

  // TODO(carl-mastrangelo): consider using com.google.common.testing.GcFinalization instead.
  try {
    channel = null;
    boolean success = false;
    for (int retry = 0; retry < 3; retry++) {
      System.gc();
      System.runFinalization();
      int orphans = ManagedChannelReference.cleanQueue(refqueue);
      if (orphans == 1) {
        success = true;
        break;
      }
      assertEquals("unexpected extra orphans", 0, orphans);
      Thread.sleep(100L * (1L << retry));
    }
    assertTrue("Channel was not garbage collected", success);

    LogRecord lr;
    synchronized (records) {
      assertEquals(1, records.size());
      lr = records.get(0);
    }
    assertThat(lr.getMessage()).contains("shutdown");
    assertThat(lr.getParameters()).asList().containsExactly(channelString).inOrder();
    assertEquals(Level.SEVERE, lr.getLevel());
    assertEquals(0, refs.size());
  } finally {
    orphanLogger.setFilter(oldFilter);
  }
}