use of java.util.logging.LogManager in project ACS by ACS-Community.
the class LogRecordCollectingLogger method getCollectingLogger.
public static synchronized <T extends LogRecordCollectingLogger> T getCollectingLogger(String name, Class<T> loggerClass) {
LogManager manager = LogManager.getLogManager();
Logger logger = manager.getLogger(name);
if (logger != null && !loggerClass.isInstance(logger)) {
throw new ClassCastException("Logger '" + name + "' is already used and is of wrong type " + logger.getClass().getName());
}
T myLogger = (T) logger;
if (myLogger == null) {
try {
Constructor<T> ctor = loggerClass.getDeclaredConstructor(String.class, String.class);
myLogger = ctor.newInstance(name, null);
manager.addLogger(myLogger);
myLogger = (T) manager.getLogger(name);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myLogger.setLevel(Level.FINEST);
myLogger.setUseParentHandlers(false);
Handler logHandler = new ConsoleHandler();
logHandler.setLevel(Level.FINEST);
myLogger.addHandler(logHandler);
return myLogger;
}
use of java.util.logging.LogManager in project jdk8u_jdk by JetBrains.
the class TestIsLoggable method main.
// The purpose of this test is to verify that the various log methods in
// Logger now call Logger.isLoggable().
// To do that - we're going to use a subclass of Logger, ThreadLogger, which
// only overrides isLoggable() - and compare the level it is given to a level
// it finds in a map indexed with the current thread id.
// We will register a TestHandler with our ThreadLogger which will store
// the messages in a messages map. This will allow us to verify whether the
// logging method we're testing has or hasn't logged.
//
// The TestCase enum above allows us to test a combination of every possible
// log method with every possible level inside a loop - with the
// exception of exiting/entering/throwing that we will be testing
// outside of that loop.
//
public static void main(String... args) {
LogManager manager = LogManager.getLogManager();
ThreadLogger logger = new ThreadLogger("foo.bar");
//manager.addLogger(logger);
TestHandler handler = new TestHandler();
logger.addHandler(handler);
//By default, logger's level is Level.INFO
final List<Level> loggable = LEVELS.subList(0, LEVELS.indexOf(Level.INFO) + 1);
// the test code...
for (Level level : LEVELS) {
if (logger.isLoggable(level) != loggable.contains(level)) {
throw new RuntimeException(level + ": unexpected result for isLoggable(): expected " + (loggable.contains(level)));
}
}
// Test that entering/exiting/throwing call isLoggable()
// Here we test the default behavior: this call shouldn't log anything
// because by default the logger level is Level.INFO and these
// methods log at Level.FINER.
// So by default - these methods don't log anything. We check it here.
logger.entering("blah", "blah");
logger.entering("blah", "blah", "blah");
logger.entering("blah", "blah", new Object[] { "blah" });
if (!handler.messages.isEmpty()) {
throw new RuntimeException("Expected empty, got " + handler.messages);
}
logger.exiting("blah", "blah");
logger.exiting("blah", "blah", "blah");
logger.exiting("blah", "blah", new Object[] { "blah" });
if (!handler.messages.isEmpty()) {
throw new RuntimeException("Expected empty, got " + handler.messages);
}
logger.throwing("blah", "blah", new Exception("blah"));
if (!handler.messages.isEmpty()) {
throw new RuntimeException("Expected empty, got " + handler.messages);
}
// Now we're going to put each level in turn in the threadMap.
// This means that isLoggable(Level.FINER) should now return true if the
// level in the map is not one of the level in the 'stronger' list below
// (here stronger=stronger than FINER)
final List<Level> stronger = LEVELS.subList(0, LEVELS.indexOf(Level.FINER));
for (Level l : LEVELS) {
logger.threadMap.put(Thread.currentThread().getId(), l);
// Check that our implementation of isLoggable(level) now returns true
// if 'level' is stronger or equals to 'l' - here we're just checking
// that our implementation of ThreadLogger.isLoggable() returns what
// we want - we're just testing the test code...
final List<Level> loggableLevels = LEVELS.subList(0, LEVELS.indexOf(l) + 1);
for (Level level : LEVELS) {
if (logger.isLoggable(level) != loggableLevels.contains(level)) {
throw new RuntimeException(level + ": unexpected result for isLoggable(): expected " + (loggableLevels.contains(level)));
}
}
// These methods should now start to log when the level we put in
// the map is weaker or equals to Level.FINER.
// This validates that these methods now call ThreadLogger.isLoggable()
// since the default level for our logger is still Level.INFO.
// If the methods didn't call ThreadLogger.isLoggable() they wouldn't
// log anything, whatever we put in the threadMap...
logger.entering("blah", "blah");
logger.entering("blah", "blah", "blah");
logger.entering("blah", "blah", new Object[] { "blah" });
if (stronger.contains(l)) {
if (!handler.messages.isEmpty()) {
throw new RuntimeException(l + ": Expected empty, got " + handler.messages);
}
} else {
if (handler.messages.size() != 3) {
throw new RuntimeException(l + ": Expected size 3, got " + handler.messages);
}
}
logger.exiting("blah", "blah");
logger.exiting("blah", "blah", "blah");
logger.exiting("blah", "blah", new Object[] { "blah" });
if (stronger.contains(l)) {
if (!handler.messages.isEmpty()) {
throw new RuntimeException(l + ": Expected empty, got " + handler.messages);
}
} else {
if (handler.messages.size() != 6) {
throw new RuntimeException(l + ": Expected size 6, got " + handler.messages);
}
}
logger.throwing("blah", "blah", new Exception("blah"));
if (stronger.contains(l)) {
if (!handler.messages.isEmpty()) {
throw new RuntimeException(l + ": Expected empty, got " + handler.messages);
}
} else {
if (handler.messages.size() != 7) {
throw new RuntimeException(l + ": Expected size 7, got " + handler.messages);
}
}
if (!stronger.contains(l)) {
System.out.println(l + ": Logger.entering/exiting/throwing: " + handler.messages);
}
handler.messages.clear();
}
// Cleanup so that we can start the next test with a clean plate...
handler.messages.clear();
logger.threadMap.clear();
//
for (LogTest testCase : LogTest.values()) {
// Each test case is a combination of:
// 1. A level to put in the threadMap.
// 2. A log method to call
final String method = testCase.method();
// We check that here.
for (Level level : LEVELS) {
if (logger.isLoggable(level) != loggable.contains(level)) {
throw new RuntimeException(level + ": unexpected result for isLoggable(): expected " + (loggable.contains(level)));
}
}
// weaker than Level.INFO.
for (Level l : LEVELS.subList(LEVELS.indexOf(Level.INFO) + 1, LEVELS.size())) {
final String test = method + l + ": ";
testCase.log(l, logger, "blah");
if (!handler.messages.isEmpty()) {
throw new RuntimeException(test + "Expected empty, got " + handler.messages);
}
}
// Let's put Level.OFF in the threadMap. Nothing should be logged,
// whichever level is used...
logger.threadMap.put(Thread.currentThread().getId(), Level.OFF);
// Check that isLoggable() now always return false.
for (Level level : LEVELS) {
if (logger.isLoggable(level)) {
throw new RuntimeException(level + ": unexpected result for isLoggable(): expected " + false);
}
}
// or equal to INFO.
for (Level l : LEVELS) {
final String test = "[threadMap=OFF] " + method + l + ": ";
testCase.log(l, logger, "blah");
if (!handler.messages.isEmpty()) {
throw new RuntimeException(test + "Expected empty, got " + handler.messages);
}
}
System.out.println("[threadMap=OFF] " + method + "logged " + handler.messages);
// Now put the testcase's level in the threadMap.
logger.threadMap.put(Thread.currentThread().getId(), testCase.threshold());
// The levels for which logging should happen are those that are stronger
// or equals to the testcase's thresholds.
final List<Level> loggableLevels = LEVELS.subList(0, LEVELS.indexOf(testCase.threshold()) + 1);
// what we put in the map.
for (Level level : LEVELS) {
if (logger.isLoggable(level) != loggableLevels.contains(level)) {
throw new RuntimeException(level + ": unexpected result for isLoggable(): expected " + (loggableLevels.contains(level)));
}
}
//
for (Level l : testCase.weaker()) {
final String test = method + l + ": ";
testCase.log(l, logger, "blah");
if (!handler.messages.isEmpty()) {
throw new RuntimeException(test + "Expected empty, got " + handler.messages);
}
}
// Then we check that it will log for the testcase threshold.
final String test2 = method + testCase.threshold() + ": ";
testCase.log(logger, testCase.threshold() + " blah");
if (handler.messages.isEmpty()) {
throw new RuntimeException(test2 + "Expected 1 message, but list is empty");
}
if (!handler.messages.contains(testCase.threshold() + " blah")) {
throw new RuntimeException(test2 + " blah not found: " + handler.messages);
}
handler.messages.clear();
// log for the others).
for (Level l : LEVELS) {
final String test = method + l + ": ";
testCase.log(l, logger, l + ": blah");
if (testCase.loggable().contains(l)) {
if (!handler.messages.contains(l + ": blah")) {
throw new RuntimeException(test + "blah not found: " + handler.messages);
}
} else {
if (handler.messages.contains(l + ": blah")) {
throw new RuntimeException(test + "blah found: " + handler.messages);
}
}
}
if (handler.messages.size() != testCase.loggable().size()) {
throw new RuntimeException(method + " Sizes don't match: expected " + testCase.loggable().size() + " got " + handler.messages);
}
// Some visual feedback on what happened.
System.out.println(method + "logged " + handler.messages);
// Cleanup for next step.
// Since we're iterating over all possible levels we can be
// sure that we haven't missed anything.
// For instance - it could be argued that logger.severe() will
// always log. But since we have 1 case where we put Level.OFF in
// the map and we have verified that severe() didn't log in that
// case, but that it logged in any other case, then we know
// beyond doubt that it called our implementation of isLoggable().
logger.threadMap.clear();
handler.messages.clear();
}
}
use of java.util.logging.LogManager in project jdk8u_jdk by JetBrains.
the class TestAppletLoggerContext method testSix.
/**
* This test is designed to test the behavior of additional LogManager instances.
* It must be noted that if the security manager is off, then calling
* Bridge.changeContext() has actually no effect - which explains why we have
* some differences between the cases security manager on & security manager
* off.
**/
public static void testSix() {
for (int i = 0; i < 3; i++) {
Bridge.desactivate();
LogManager manager = new LogManager() {
};
Logger logger1 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
Logger logger1b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
assertNull(logger1);
assertNull(logger1b);
Logger global = new Bridge.CustomLogger(Logger.GLOBAL_LOGGER_NAME);
manager.addLogger(global);
Logger logger2 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
Logger logger2b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
assertNotNull(logger2);
assertNotNull(logger2b);
assertEquals(logger2, global);
assertEquals(logger2b, global);
assertNull(manager.getLogger(""));
assertNull(manager.getLogger(""));
for (int j = 0; j < 3; j++) {
Bridge.changeContext();
// this is not a supported configuration:
// We are in an applet context with several log managers.
// We however need to check our assumptions...
// Applet context => root logger and global logger should also be null.
Logger expected = (System.getSecurityManager() == null ? global : null);
Logger logger3 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
Logger logger3b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
assertEquals(expected, logger3);
assertEquals(expected, logger3b);
Logger global2 = new Bridge.CustomLogger(Logger.GLOBAL_LOGGER_NAME);
manager.addLogger(global2);
Logger logger4 = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
Logger logger4b = manager.getLogger(Logger.GLOBAL_LOGGER_NAME);
assertNotNull(logger4);
assertNotNull(logger4b);
expected = (System.getSecurityManager() == null ? global : global2);
;
assertEquals(logger4, expected);
assertEquals(logger4b, expected);
Logger logger5 = manager.getLogger("");
Logger logger5b = manager.getLogger("");
Logger expectedRoot = null;
assertEquals(logger5, expectedRoot);
assertEquals(logger5b, expectedRoot);
}
}
}
use of java.util.logging.LogManager in project adempiere by adempiere.
the class CLogMgt method shutdown.
// enable
/**
* Shutdown Logging system
*/
public static void shutdown() {
LogManager mgr = LogManager.getLogManager();
mgr.reset();
}
use of java.util.logging.LogManager in project adempiere by adempiere.
the class CLogger method getCLogger.
/**
* Get Logger
* @param className class name
* @return Logger
*/
public static synchronized CLogger getCLogger(String className) {
// CLogMgt.initialize();
LogManager manager = LogManager.getLogManager();
if (className == null)
className = "";
Logger result = manager.getLogger(className);
if (result != null && result instanceof CLogger)
return (CLogger) result;
//
CLogger newLogger = new CLogger(className, null);
newLogger.setLevel(CLogMgt.getLevel());
manager.addLogger(newLogger);
return newLogger;
}
Aggregations