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;
}
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);
}
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);
}
}
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);
}
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;
}
Aggregations