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

The following examples show how to use java.util.logging.Logger#setFilter() . 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: HostDeviceApplication.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    Logger logger = Logger.getLogger("host.dplugin");
    if (BuildConfig.DEBUG) {
        AndroidHandler handler = new AndroidHandler(logger.getName());
        handler.setFormatter(new SimpleFormatter());
        handler.setLevel(Level.ALL);
        logger.addHandler(handler);
        logger.setLevel(Level.ALL);
    } else {
        logger.setLevel(Level.OFF);
        logger.setFilter((record) -> false);
    }
    registerActivityLifecycleCallbacks(this);
}
 
Example 2
Source File: Ideas_2009_05_06.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * The logger reference is lost at the end of the method (it doesn't escape
 * the method), so if you have a garbage collection cycle just after the
 * call to initLogging, the logger configuration is lost (because Logger
 * only keeps weak references).
 */

@ExpectWarning("LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE")
public static void initLogging() throws SecurityException, IOException {
    Logger logger = Logger.getLogger("edu.umd.cs.findbugs");
    logger.addHandler(new FileHandler()); // call to change logger
                                          // configuration
    logger.setUseParentHandlers(false); // another call to change logger
                                        // configuration
    logger.setLevel(Level.FINER);
    logger.setFilter(new Filter() {

        @Override
        public boolean isLoggable(LogRecord arg0) {
            return true;
        }
    });
}
 
Example 3
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 4
Source File: DConnectApplication.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
private void setupLogger(final String name) {
    Logger logger = Logger.getLogger(name);
    if (BuildConfig.DEBUG) {
        AndroidHandler handler = new AndroidHandler(logger.getName());
        handler.setFormatter(new SimpleFormatter());
        handler.setLevel(Level.ALL);
        logger.addHandler(handler);
        logger.setLevel(Level.ALL);
        logger.setUseParentHandlers(false);
    } else {
        logger.setLevel(Level.OFF);
        logger.setFilter((record) -> false);
    }
}
 
Example 5
Source File: WindowManagerImplTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void checkEDTAssert (final boolean assertionsEnabled) {
    Logger logger = Logger.getLogger(WindowManagerImpl.class.getName());

    logger.setFilter(new java.util.logging.Filter() {
        public boolean isLoggable(LogRecord record) {
            Level level = record.getLevel();

            if (assertionsEnabled && !level.equals(Level.WARNING)) {
                checkOK = false;
                failMsg = "Logging on Level WARNING expected when assertions are enabled";
                return true;
            }

            if (!assertionsEnabled && !level.equals(Level.FINE)) {
                checkOK = false;
                failMsg = "Logging on Level FINE expected when assertions are disabled";
                return true;
            }

            checkOK = true;

            // don't log anything if test passes
            return false;
        }
    });

    WindowManagerImpl.warnIfNotInEDT();
}
 
Example 6
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 7
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 8
Source File: OldLoggerTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void testGetLogger_WithParent() {
    assertNull(LogManager.getLogManager().getLogger(
            "testGetLogger_WithParent_ParentLogger"));

    // get root of hierarchy
    Logger root = Logger.getLogger("");
    // create the parent logger
    Logger pLog = Logger.getLogger("testGetLogger_WithParent_ParentLogger",
            VALID_RESOURCE_BUNDLE);
    pLog.setLevel(Level.CONFIG);
    pLog.addHandler(new MockHandler());
    pLog.setFilter(new MockFilter());
    pLog.setUseParentHandlers(false);
    // check root parent
    assertEquals("testGetLogger_WithParent_ParentLogger", pLog.getName());
    assertSame(pLog.getParent(), root);

    // child part
    assertNull(LogManager.getLogManager().getLogger(
            "testGetLogger_WithParent_ParentLogger.child"));
    // create the child logger
    Logger child = Logger
            .getLogger("testGetLogger_WithParent_ParentLogger.child");
    assertNull(child.getFilter());
    assertEquals(0, child.getHandlers().length);
    assertNull(child.getLevel());
    assertEquals("testGetLogger_WithParent_ParentLogger.child", child
            .getName());
    assertSame(child.getParent(), pLog);
    assertNull(child.getResourceBundle());
    assertNull(child.getResourceBundleName());
    assertTrue(child.getUseParentHandlers());

    // create not valid child
    Logger notChild = Logger
            .getLogger("testGetLogger_WithParent_ParentLogger1.child");
    assertNull(notChild.getFilter());
    assertEquals(0, notChild.getHandlers().length);
    assertNull(notChild.getLevel());
    assertEquals("testGetLogger_WithParent_ParentLogger1.child", notChild
            .getName());
    assertNotSame(notChild.getParent(), pLog);
    assertNull(notChild.getResourceBundle());
    assertNull(notChild.getResourceBundleName());
    assertTrue(notChild.getUseParentHandlers());
    // verify two level root.parent
    assertEquals("testGetLogger_WithParent_ParentLogger.child", child
            .getName());
    assertSame(child.getParent().getParent(), root);


    // create three level child
    Logger childOfChild = Logger
            .getLogger("testGetLogger_WithParent_ParentLogger.child.child");
    assertNull(childOfChild.getFilter());
    assertEquals(0, childOfChild.getHandlers().length);
    assertSame(child.getParent().getParent(), root);
    assertNull(childOfChild.getLevel());
    assertEquals("testGetLogger_WithParent_ParentLogger.child.child",
            childOfChild.getName());

    assertSame(childOfChild.getParent(), child);
    assertSame(childOfChild.getParent().getParent(), pLog);
    assertSame(childOfChild.getParent().getParent().getParent(), root);
    assertNull(childOfChild.getResourceBundle());
    assertNull(childOfChild.getResourceBundleName());
    assertTrue(childOfChild.getUseParentHandlers());

    // abnormal case : lookup to root parent in a hierarchy without a logger
    // parent created between
    assertEquals("testGetLogger_WithParent_ParentLogger1.child", notChild
            .getName());
    assertSame(child.getParent().getParent(), root);
    assertNotSame(child.getParent(), root);

    // abnormal cases
    assertNotSame(root.getParent(), root);
    Logger twoDot = Logger.getLogger("..");
    assertSame(twoDot.getParent(), root);

}
 
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: 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 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: TomEEMyFacesContainerInitializer.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public void onStartup(final Set<Class<?>> classes, final ServletContext ctx) throws ServletException {
    // try to skip first
    if ("true".equalsIgnoreCase(ctx.getInitParameter("org.apache.myfaces.INITIALIZE_ALWAYS_STANDALONE"))
            || "true".equals(SystemInstance.get().getProperty(OPENEJB_JSF_SKIP, "false"))) {
        return;
    }

    // if mojarra is present skip myfaces startup
    try {
        ctx.getClassLoader().loadClass("com.sun.faces.context.SessionMap");
        return;
    } catch (final ClassNotFoundException | NoClassDefFoundError cnfe) {
        // no-op
    }

    // some message filtering, not a perf killer since this class don't log a lot
    final Logger abstractInitializerLogger = Logger.getLogger(AbstractFacesInitializer.class.getName());
    abstractInitializerLogger.setFilter(new RemoveLogMessage(
            new RemoveLogMessage(abstractInitializerLogger.getFilter(),
                    Level.WARNING, "No mappings of FacesServlet found. Abort initializing MyFaces."),
            Level.WARNING, "No mappings of FacesServlet found. Abort destroy MyFaces."));

    final boolean facesServletPresent = isFacesServletPresent(ctx);
    if (facesServletPresent || isFacesConfigPresent(ctx)) {
        // we found a faces-config.xml or some classes so let's delegate to myfaces

        // since we don't want to call isFacesConfigPresent again (it scan all jars!!!!)
        // forcing classes to not be empty
        Set<Class<?>> passedClasses = classes;
        if (passedClasses == null) {
            passedClasses = new HashSet<Class<?>>();
        }
        if (passedClasses.isEmpty()) {
            passedClasses.add(TomEEMyFacesContainerInitializer.class);
        }

        if (ctx instanceof ApplicationContextFacade) {
            try {
                final ApplicationContext appCtx = (ApplicationContext) get(ApplicationContextFacade.class, ctx);
                final Context tomcatCtx = (Context) get(ApplicationContext.class, appCtx);
                if (!Arrays.asList(tomcatCtx.findApplicationListeners()).contains(StartupServletContextListener.class.getName())) {
                    addListener(ctx);
                }
            } catch (final Exception e) {
                // add it, not important we'll simply get a warning saying it is already here
                addListener(ctx);
            }
        }

        // finally delegating begin sure we'll not call isFacesConfigPresent
        if (!facesServletPresent) {
            delegate.onStartup(classes, ctx);
        } // else already done since there is the servlet
    }
}
 
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);
  }
}