Search in sources :

Example 21 with LogManager

use of java.util.logging.LogManager in project jersey by jersey.

the class JerseyTest method getRootLoggers.

/**
     * Retrieves a list of root loggers.
     *
     * @return list of root loggers.
     */
private Set<Logger> getRootLoggers() {
    final LogManager logManager = LogManager.getLogManager();
    final Enumeration<String> loggerNames = logManager.getLoggerNames();
    final Set<Logger> rootLoggers = new HashSet<>();
    while (loggerNames.hasMoreElements()) {
        Logger logger = logManager.getLogger(loggerNames.nextElement());
        if (logger != null) {
            while (logger.getParent() != null) {
                logger = logger.getParent();
            }
            rootLoggers.add(logger);
        }
    }
    return rootLoggers;
}
Also used : Logger(java.util.logging.Logger) LogManager(java.util.logging.LogManager) HashSet(java.util.HashSet)

Example 22 with LogManager

use of java.util.logging.LogManager in project jetty.project by eclipse.

the class JavaUtilLogTest method setJUL.

@BeforeClass
public static void setJUL() {
    LogManager lmgr = LogManager.getLogManager();
    java.util.logging.Logger root = lmgr.getLogger("");
    // Remember original handlers
    originalHandlers = root.getHandlers();
    // Remove original handlers
    for (Handler existing : originalHandlers) {
        root.removeHandler(existing);
    }
    // Set test/capturing handler
    jul = new CapturingJULHandler();
    root.addHandler(jul);
}
Also used : Handler(java.util.logging.Handler) LogManager(java.util.logging.LogManager) BeforeClass(org.junit.BeforeClass)

Example 23 with LogManager

use of java.util.logging.LogManager in project tomcat by apache.

the class SetParentClassLoaderRule method stop.

/**
     * Stop an existing server instance.
     */
public void stop() {
    try {
        // doesn't get invoked twice
        if (useShutdownHook) {
            Runtime.getRuntime().removeShutdownHook(shutdownHook);
            // If JULI is being used, re-enable JULI's shutdown to ensure
            // log messages are not lost
            LogManager logManager = LogManager.getLogManager();
            if (logManager instanceof ClassLoaderLogManager) {
                ((ClassLoaderLogManager) logManager).setUseShutdownHook(true);
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
    // This will fail on JDK 1.2. Ignoring, as Tomcat can run
    // fine without the shutdown hook.
    }
    // Shut down the server
    try {
        Server s = getServer();
        LifecycleState state = s.getState();
        if (LifecycleState.STOPPING_PREP.compareTo(state) <= 0 && LifecycleState.DESTROYED.compareTo(state) >= 0) {
        // Nothing to do. stop() was already called
        } else {
            s.stop();
            s.destroy();
        }
    } catch (LifecycleException e) {
        log.error("Catalina.stop", e);
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) ClassLoaderLogManager(org.apache.juli.ClassLoaderLogManager) Server(org.apache.catalina.Server) LifecycleState(org.apache.catalina.LifecycleState) LogManager(java.util.logging.LogManager) ClassLoaderLogManager(org.apache.juli.ClassLoaderLogManager)

Example 24 with LogManager

use of java.util.logging.LogManager in project j2objc by google.

the class LoggerTest method test_initHandler.

/*
	 * Test whether privileged code is used to load resource bundles.
	 *
	public void testLoadResourceBundle() {
        //
		SecurityManager oldMan = System.getSecurityManager();
		System.setSecurityManager(new MockNoLoadingClassSecurityManager());
		try {
			Logger.getAnonymousLogger(VALID_RESOURCE_BUNDLE);
		} finally {
			System.setSecurityManager(oldMan);
		}
	}

    public void testLoadResourceBundleNonExistent() {
        try {
            // Try a load a non-existent resource bundle.
            LoggerExtension.loadResourceBundle("missinglogger.properties");
            fail("Expected an exception.");
        } catch (MissingResourceException ex) {
            // Expected exception is precisely a MissingResourceException
            assertTrue(ex.getClass() == MissingResourceException.class);
        }
    }

    /**
     * @tests java.util.logging.Logger#logrb(Level, String, String, String,
     *        String, Object)
     *
    public void test_init_logger()
            throws Exception {
        Properties p = new Properties();
        p.put("testGetLogger_Normal_ANewLogger2.level", "ALL");
        LogManager.getLogManager().readConfiguration(
                EnvironmentHelper.PropertiesToInputStream(p));

        assertNull(LogManager.getLogManager().getLogger(
                "testGetLogger_Normal_ANewLogger2"));
        SecurityManager originalSecurityManager = System.getSecurityManager();
        try {
            System.setSecurityManager(new SecurityManager());
            // should not throw expection
            Logger logger = Logger.getLogger("testGetLogger_Normal_ANewLogger2");
            // should thrpw exception
            try{
                logger.setLevel(Level.ALL);
                fail("should throw SecurityException");
            } catch (SecurityException e){
                // expected
            }
            try{
                logger.setParent(Logger.getLogger("root"));
                fail("should throw SecurityException");
            } catch (SecurityException e){
                // expected
            }
        } finally {
            System.setSecurityManager(originalSecurityManager);
        }
    }

    /*
     * test initHandler
     */
public void test_initHandler() throws Exception {
    InputStream logProps = ClassLoader.getSystemResourceAsStream(LOGGING_CONFIG_FILE);
    LogManager lm = LogManager.getLogManager();
    lm.readConfiguration(logProps);
    Logger log = Logger.getLogger("");
    // can log properly
    Handler[] handlers = log.getHandlers();
    assertEquals(2, handlers.length);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Handler(java.util.logging.Handler) Logger(java.util.logging.Logger) LogManager(java.util.logging.LogManager)

Example 25 with LogManager

use of java.util.logging.LogManager in project ACS by ACS-Community.

the class AcsLogger method createUnconfiguredLogger.

/**
     * Non-standard factory method to be used only for special offline or testing purposes
     * where typically an AcsLogger must be provided by an alternative implementation of ContainerServices.
     * The returned AcsLogger is just like a JDK Logger obtained from {@link Logger#getLogger(String, String)}.
     * <p>
     * Note that we do not supply a {@link LogConfig} and therefore the new AcsLogger cannot register itself 
     * for initial configuration or later configuration change notifications. <br>
     * <b>It is the client's responsibility to configure the log level and parent logger of the returned AcsLogger!</b>
     * 
     * @param name  the logger's name
     * @param resourceBundleName
     * @return <code>AcsLogger</code> that is as close to a normal JDK Logger as possible.
     * @throws IllegalArgumentException 
     *              If a Logger of the given <code>name</code> exists but is not an <code>AcsLogger</code>,
     *              or if an AcsLogger of the given <code>name</code> exists but has a different <code>resourceBundleName</code>.
     */
public static AcsLogger createUnconfiguredLogger(String name, String resourceBundleName) {
    // the following code is copied and modified from Logger.getLogger 
    LogManager manager = LogManager.getLogManager();
    Logger jdkLogger = manager.getLogger(name);
    if (jdkLogger != null && !(jdkLogger instanceof AcsLogger)) {
        throw new IllegalArgumentException("Logger " + name + " already exists but is not of subtype AcsLogger.");
    }
    AcsLogger result = (AcsLogger) jdkLogger;
    if (result == null) {
        // Create a new logger.
        // Note: we may get a MissingResourceException here.
        result = new AcsLogger(name, resourceBundleName, null, true, null);
        manager.addLogger(result);
        result = (AcsLogger) manager.getLogger(name);
    }
    // however we check that the old and new bundle are consistent.
    if (result.getResourceBundleName() != null && !result.getResourceBundleName().equals(resourceBundleName)) {
        throw new IllegalArgumentException(result.getResourceBundleName() + " != " + resourceBundleName);
    }
    return result;
}
Also used : Logger(java.util.logging.Logger) UnnamedLogger(alma.maci.loggingconfig.UnnamedLogger) LogManager(java.util.logging.LogManager)

Aggregations

LogManager (java.util.logging.LogManager)45 Logger (java.util.logging.Logger)19 Handler (java.util.logging.Handler)9 IOException (java.io.IOException)7 ClassLoaderLogManager (org.apache.juli.ClassLoaderLogManager)7 File (java.io.File)6 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 LifecycleException (org.apache.catalina.LifecycleException)6 ConsoleHandler (java.util.logging.ConsoleHandler)4 Level (java.util.logging.Level)4 FileNotFoundException (java.io.FileNotFoundException)3 LifecycleState (org.apache.catalina.LifecycleState)3 Server (org.apache.catalina.Server)3 AgentFormatterDelegate (com.sun.enterprise.v3.logging.AgentFormatterDelegate)2 PropertyChangeListener (java.beans.PropertyChangeListener)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Properties (java.util.Properties)2 Formatter (java.util.logging.Formatter)2 AfterClass (org.junit.AfterClass)2