java.lang.System.LoggerFinder Java Examples

The following examples show how to use java.lang.System.LoggerFinder. 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: LoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 7 votes vote down vote up
static Logger getLogger(LoggerFinder provider, String name, Module caller) {
    Logger logger;
    try {
        logger = Logger.class.cast(lazyGetLogger.invoke(null, name, caller));
    } catch (Throwable x) {
        Throwable t = (x instanceof InvocationTargetException) ?
                ((InvocationTargetException)x).getTargetException() : x;
        if (t instanceof RuntimeException) {
            throw (RuntimeException)t;
        } else if (t instanceof Exception) {
            throw new RuntimeException(t);
        } else {
            throw (Error)t;
        }
    }
    // The method above does not throw exception...
    // call the provider here to verify that an exception would have
    // been thrown by the provider.
    if (logger != null && caller == Thread.class.getModule()) {
        Logger log = provider.getLogger(name, caller);
    }
    return logger;
}
 
Example #2
Source File: LazyLoggers.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static LoggerFinder accessLoggerFinder() {
    LoggerFinder prov = provider;
    if (prov == null) {
        // no need to lock: it doesn't matter if we call
        // getLoggerFinder() twice - since LoggerFinder already caches
        // the result.
        // This is just an optimization to avoid the cost of calling
        // doPrivileged every time.
        final SecurityManager sm = System.getSecurityManager();
        prov = sm == null ? LoggerFinder.getLoggerFinder() :
            AccessController.doPrivileged(
                    (PrivilegedAction<LoggerFinder>)LoggerFinder::getLoggerFinder);
        provider = prov;
    }
    return prov;
}
 
Example #3
Source File: DefaultPlatformLoggerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected, boolean expectNotNull) {
    LogEvent actual =  eventQueue.poll();
    if (actual == null && !expectNotNull) return;
    if (actual != null && !expectNotNull) {
        throw new RuntimeException("Unexpected log event found for " + desc
            + "\n\tgot: " + actual);
    }
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #4
Source File: PlatformLoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected, boolean expectNotNull) {
    LogEvent actual =  eventQueue.poll();
    if (actual == null && !expectNotNull) return;
    if (actual != null && !expectNotNull) {
        throw new RuntimeException("Unexpected log event found for " + desc
            + "\n\tgot: " + actual);
    }
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #5
Source File: DefaultLoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected, boolean expectNotNull) {
    LogEvent actual =  eventQueue.poll();
    if (actual == null && !expectNotNull) return;
    if (actual != null && !expectNotNull) {
        throw new RuntimeException("Unexpected log event found for " + desc
            + "\n\tgot: " + actual);
    }
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #6
Source File: LoggerFinderLoaderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void test(LoggerFinder provider, boolean hasRequiredPermissions) {

        ResourceBundle loggerBundle = ResourceBundle.getBundle(MyLoggerBundle.class.getName());
        final Map<Logger, String> loggerDescMap = new HashMap<>();

        System.Logger sysLogger = accessSystemLogger.getLogger("foo");
        loggerDescMap.put(sysLogger, "accessSystemLogger.getLogger(\"foo\")");
        System.Logger localizedSysLogger = accessSystemLogger.getLogger("fox", loggerBundle);
        loggerDescMap.put(localizedSysLogger, "accessSystemLogger.getLogger(\"fox\", loggerBundle)");
        System.Logger appLogger = System.getLogger("bar");
        loggerDescMap.put(appLogger,"System.getLogger(\"bar\")");
        System.Logger localizedAppLogger = System.getLogger("baz", loggerBundle);
        loggerDescMap.put(localizedAppLogger,"System.getLogger(\"baz\", loggerBundle)");

        testLogger(provider, loggerDescMap, "foo", null, sysLogger);
        testLogger(provider, loggerDescMap, "foo", loggerBundle, localizedSysLogger);
        testLogger(provider, loggerDescMap, "foo", null, appLogger);
        testLogger(provider, loggerDescMap, "foo", loggerBundle, localizedAppLogger);
    }
 
Example #7
Source File: LoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected, boolean expectNotNull) {
    LogEvent actual =  eventQueue.poll();
    if (actual == null && !expectNotNull) return;
    if (actual != null && !expectNotNull) {
        throw new RuntimeException("Unexpected log event found for " + desc
            + "\n\tgot: " + actual);
    }
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #8
Source File: LazyLoggers.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static LoggerFinder accessLoggerFinder() {
    LoggerFinder prov = provider;
    if (prov == null) {
        // no need to lock: it doesn't matter if we call
        // getLoggerFinder() twice - since LoggerFinder already caches
        // the result.
        // This is just an optimization to avoid the cost of calling
        // doPrivileged every time.
        final SecurityManager sm = System.getSecurityManager();
        prov = sm == null ? LoggerFinder.getLoggerFinder() :
            AccessController.doPrivileged(
                    (PrivilegedAction<LoggerFinder>)LoggerFinder::getLoggerFinder);
        provider = prov;
    }
    return prov;
}
 
Example #9
Source File: LoggerFinderAPI.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void testGetLocalizedLogger(String desc, String name, ResourceBundle bundle,
                            Module mod, Class<? extends Throwable> thrown) {
    try {
        LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
        Logger logger = finder.getLocalizedLogger(name, bundle, mod);
        if (thrown != null) {
            throw new AssertionError("Exception " + thrown.getName()
                    + " not thrown for "
                    + "LoggerFinder.getLocalizedLogger"
                    + " with " + desc);
        }
        // Make sure we don't fail if tests are run in parallel
        synchronized(RecordStream.LOCK) {
            LOG_STREAM.startRecording();
            try {
                logger.log(Level.INFO, "{0} with {1}: PASSED",
                          "LoggerFinder.getLocalizedLogger",
                          desc);
            } finally {
                byte[] logged = LOG_STREAM.stopRecording();
                check(logged, "testGetLocalizedLogger", desc, bundle,
                      "LoggerFinder.getLocalizedLogger");
            }
        }
    } catch (Throwable x) {
        if (thrown != null && thrown.isInstance(x)) {
            System.out.printf("Got expected exception for %s with %s: %s\n",
                    "LoggerFinder.getLocalizedLogger", desc, String.valueOf(x));
        } else throw x;
    }
}
 
Example #10
Source File: LoggerFinderAPI.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void testGetLogger(String desc, String name, Module mod, Class<? extends Throwable> thrown) {
    try {
        LoggerFinder finder = System.LoggerFinder.getLoggerFinder();
        Logger logger = finder.getLogger(name, mod);
        if (thrown != null) {
            throw new AssertionError("Exception " + thrown.getName()
                    + " not thrown for "
                    + "LoggerFinder.getLogger"
                    + " with " + desc);
        }
        // Make sure we don't fail if tests are run in parallel
        synchronized(RecordStream.LOCK) {
            LOG_STREAM.startRecording();
            try {
                logger.log(Level.INFO, "{0} with {1}: PASSED",
                           "LoggerFinder.getLogger",
                           desc);
            } finally {
                byte[] logged = LOG_STREAM.stopRecording();
                check(logged, "testGetLogger", desc, null,
                      "LoggerFinder.getLogger");
            }
        }
    } catch (Throwable x) {
        if (thrown != null && thrown.isInstance(x)) {
            System.out.printf("Got expected exception for %s with %s: %s\n",
                    "LoggerFinder.getLogger", desc, String.valueOf(x));
        } else throw x;
    }
}
 
Example #11
Source File: LoggerFinderBackendTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] argv) throws Exception {

        final AtomicInteger nb = new AtomicInteger(0);
        final boolean hidesProvider = Boolean.getBoolean("test.logger.hidesProvider");
        System.out.println(ClassLoader.getSystemClassLoader());
        final BackendTesterFactory factory;
        if (java.lang.System.LoggerFinder.getLoggerFinder() instanceof CustomLoggerFinder) {
            if (hidesProvider) {
                System.err.println("Custom backend "
                        + java.lang.System.LoggerFinder.getLoggerFinder()
                        + " should have been hidden!");
                throw new RuntimeException(
                        "Custom backend should have been hidden: "
                        + "check value of java.system.class.loader property");
            }
            System.out.println("Using custom backend");
            factory = new CustomBackendTesterFactory();
        } else {
            if (!hidesProvider) {
                System.err.println("Default JUL backend "
                        + java.lang.System.LoggerFinder.getLoggerFinder()
                        + " should have been hidden!");
                throw new RuntimeException(
                        "Default JUL backend should have been hidden: "
                        + "check value of java.system.class.loader property");
            }
            System.out.println("Using JUL backend");
            factory = new JULBackendTesterFactory();
        }

        testBackend(nb, factory);
    }
 
Example #12
Source File: LoggerFinderBackendTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static java.lang.System.Logger getSystemLogger(String name,
        ResourceBundle bundle, Module caller) throws Exception {
    try {
        LoggerFinder provider = LoggerFinder.class.cast(accessLoggerFinder.invoke(null));
        return provider.getLocalizedLogger(name, bundle, caller);
    } catch (InvocationTargetException x) {
        Throwable t = x.getTargetException();
        if (t instanceof Exception) {
            throw (Exception)t;
        } else {
            throw (Error)t;
        }
    }
}
 
Example #13
Source File: SystemClassLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
boolean accept(String name) {
    final boolean res = !name.endsWith(LoggerFinder.class.getName());
    if (res == false) {
        System.out.println("Hiding " + name);
    }
    return res;
}
 
Example #14
Source File: BaseDefaultLoggerFinderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static TestLoggerFinder getLoggerFinder(Class<?> expectedClass) {
    LoggerFinder provider = null;
    try {
        TestLoggerFinder.sequencer.incrementAndGet();
        provider = LoggerFinder.getLoggerFinder();
    } catch(AccessControlException a) {
        throw a;
    }
    ErrorStream.errorStream.store();
    System.out.println("*** Actual LoggerFinder class is: " + provider.getClass().getName());
    expectedClass.cast(provider);
    return TestLoggerFinder.class.cast(provider);
}
 
Example #15
Source File: PlatformLoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected) {
    LogEvent actual =  eventQueue.poll();
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #16
Source File: LoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected) {
    LogEvent actual =  eventQueue.poll();
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #17
Source File: LoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static Logger getLogger(LoggerFinder provider, String name, ResourceBundle bundle, Module caller) {
    if (getClassLoader(caller) != null) {
        return System.getLogger(name,bundle);
    } else {
        return provider.getLocalizedLogger(name, bundle, caller);
    }
}
 
Example #18
Source File: DefaultLoggerBridgeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected) {
    LogEvent actual =  eventQueue.poll();
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #19
Source File: DefaultPlatformLoggerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void checkLogEvent(LoggerFinder provider, String desc,
        LogEvent expected) {
    LogEvent actual =  eventQueue.poll();
    if (!expected.equals(actual)) {
        throw new RuntimeException("mismatch for " + desc
                + "\n\texpected=" + expected
                + "\n\t  actual=" + actual);
    } else {
        verbose("Got expected results for "
                + desc + "\n\t" + expected);
    }
}
 
Example #20
Source File: DefaultPlatformLoggerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void test(LoggerFinder provider, boolean hasRequiredPermissions,
        java.util.logging.Logger appSink, java.util.logging.Logger sysSink) throws Exception {

    // No way to give a resource bundle to a platform logger.
    // ResourceBundle loggerBundle = ResourceBundle.getBundle(MyLoggerBundle.class.getName());
    final Map<PlatformLogger, String> loggerDescMap = new HashMap<>();

    PlatformLogger platform = PlatformLogger.getLogger("foo");
    loggerDescMap.put(platform, "PlatformLogger.getLogger(\"foo\")");

    testLogger(provider, loggerDescMap, "foo", null, platform, sysSink);
}
 
Example #21
Source File: DefaultPlatformLoggerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    LoggerFinder provider = LoggerFinder.getLoggerFinder();
    java.util.logging.Logger appSink = LoggingProviderImpl.getLogManagerAccess()
            .demandLoggerFor(LogManager.getLogManager(), "foo",
                    DefaultPlatformLoggerTest.class.getModule());
    java.util.logging.Logger sysSink = LoggingProviderImpl.getLogManagerAccess()
            .demandLoggerFor(LogManager.getLogManager(),"foo", Thread.class.getModule());
    java.util.logging.Logger sink = Logger.getLogger("foo");
    sink.addHandler(new MyHandler());
    sink.setUseParentHandlers(VERBOSE);

    System.out.println("\n*** Without Security Manager\n");
    test(provider, true, appSink, sysSink);
    System.out.println("Tetscase count: " + sequencer.get());

    Policy.setPolicy(new SimplePolicy(allowAll, allowControl));
    System.setSecurityManager(new SecurityManager());

    System.out.println("\n*** With Security Manager, without permissions\n");
    test(provider, false, appSink, sysSink);
    System.out.println("Tetscase count: " + sequencer.get());

    System.out.println("\n*** With Security Manager, with control permission\n");
    allowControl.get().set(true);
    test(provider, true, appSink, sysSink);

    System.out.println("\nPASSED: Tested " + sequencer.get() + " cases.");
}
 
Example #22
Source File: BootstrapLogger.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public LoggingBackend run() {
    final Iterator<LoggerFinder> iterator =
        ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader())
        .iterator();
    if (iterator.hasNext()) {
        return LoggingBackend.CUSTOM; // Custom Logger Provider is registered
    }
    // No custom logger provider: we will be using the default
    // backend.
    final Iterator<DefaultLoggerFinder> iterator2 =
        ServiceLoader.loadInstalled(DefaultLoggerFinder.class)
        .iterator();
    if (iterator2.hasNext()) {
        // LoggingProviderImpl is registered. The default
        // implementation is java.util.logging
        String cname = System.getProperty("java.util.logging.config.class");
        String fname = System.getProperty("java.util.logging.config.file");
        return (cname != null || fname != null)
            ? LoggingBackend.JUL_WITH_CONFIG
            : LoggingBackend.JUL_DEFAULT;
    } else {
        // SimpleConsoleLogger is used
        return LoggingBackend.NONE;
    }
}
 
Example #23
Source File: BootstrapLogger.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public LoggingBackend run() {
    final Iterator<LoggerFinder> iterator =
        ServiceLoader.load(LoggerFinder.class, ClassLoader.getSystemClassLoader())
        .iterator();
    if (iterator.hasNext()) {
        return LoggingBackend.CUSTOM; // Custom Logger Provider is registered
    }
    // No custom logger provider: we will be using the default
    // backend.
    final Iterator<DefaultLoggerFinder> iterator2 =
        ServiceLoader.loadInstalled(DefaultLoggerFinder.class)
        .iterator();
    if (iterator2.hasNext()) {
        // LoggingProviderImpl is registered. The default
        // implementation is java.util.logging
        String cname = System.getProperty("java.util.logging.config.class");
        String fname = System.getProperty("java.util.logging.config.file");
        return (cname != null || fname != null)
            ? LoggingBackend.JUL_WITH_CONFIG
            : LoggingBackend.JUL_DEFAULT;
    } else {
        // SimpleConsoleLogger is used
        return LoggingBackend.NONE;
    }
}
 
Example #24
Source File: LoggerFinderBackendTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public CustomBackendTester(boolean isSystem,
        Class<? extends java.lang.System.Logger> restrictedTo,
        ResourceBundle localized) {
    super(isSystem, restrictedTo, localized);
    provider = (CustomLoggerFinder)java.lang.System.LoggerFinder.getLoggerFinder();
}
 
Example #25
Source File: CustomLoggerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) {
    if (args.length == 0)
        args = new String[] {
            "NOSECURITY",
            "NOPERMISSIONS",
            "WITHPERMISSIONS"
        };

    // 1. Obtain destination loggers directly from the LoggerFinder
    //   - LoggerFinder.getLogger("foo", type)
    BaseLoggerFinder provider =
            BaseLoggerFinder.class.cast(LoggerFinder.getLoggerFinder());
    BaseLoggerFinder.LoggerImpl appSink =
            BaseLoggerFinder.LoggerImpl.class.cast(provider.getLogger("foo", CustomLoggerTest.class.getModule()));
    BaseLoggerFinder.LoggerImpl sysSink =
            BaseLoggerFinder.LoggerImpl.class.cast(provider.getLogger("foo", Thread.class.getModule()));


    Stream.of(args).map(TestCases::valueOf).forEach((testCase) -> {
        switch (testCase) {
            case NOSECURITY:
                System.out.println("\n*** Without Security Manager\n");
                test(provider, true, appSink, sysSink);
                System.out.println("Tetscase count: " + sequencer.get());
                break;
            case NOPERMISSIONS:
                System.out.println("\n*** With Security Manager, without permissions\n");
                setSecurityManager();
                test(provider, false, appSink, sysSink);
                System.out.println("Tetscase count: " + sequencer.get());
                break;
            case WITHPERMISSIONS:
                System.out.println("\n*** With Security Manager, with control permission\n");
                setSecurityManager();
                final boolean control = allowControl.get().get();
                try {
                    allowControl.get().set(true);
                    test(provider, true, appSink, sysSink);
                } finally {
                    allowControl.get().set(control);
                }
                break;
            default:
                throw new RuntimeException("Unknown test case: " + testCase);
        }
    });
    System.out.println("\nPASSED: Tested " + sequencer.get() + " cases.");
}
 
Example #26
Source File: Log4jSystemLoggerAdapter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
protected LoggerContext getContext() {
    return getContext(StackLocatorUtil.getCallerClass(LoggerFinder.class));
}