Search in sources :

Example 26 with LogManager

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;
}
Also used : ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) Logger(java.util.logging.Logger) LogManager(java.util.logging.LogManager) ConsoleHandler(java.util.logging.ConsoleHandler)

Example 27 with LogManager

use of java.util.logging.LogManager in project OpenAM by OpenRock.

the class SecureFileHandler method initializeVerifier.

/**
     * Initialize SecureLog verifier
     * @param verPass verifier password
     * @param token AM token
     */
public static void initializeVerifier(AMPassword verPass, Object token) {
    /*  Remove the relevant verifier initialization code when deploying
         *  it finally. For the timebeing it will be done in the old way
         *  of doing it in the constructor thru initializeSecurity();
         */
    try {
        setVerPassword(verPass, token);
        LogManager lmanager = LogManagerUtil.getLogManager();
        String logPath = lmanager.getProperty(LogConstants.LOG_LOCATION);
        if (!logPath.endsWith("/"))
            logPath += "/";
        LogManager manager = LogManagerUtil.getLogManager();
        Enumeration e = manager.getLoggerNames();
        while (e.hasMoreElements()) {
            String FileName = (String) e.nextElement();
            String verifierFileName = logPath + PREFIX + "ver." + FileName;
            SecureLogHelper helper = getSecureLogHelper(FileName);
            AMPassword logPassword = getLogPassword();
            // explicit auditor role in DSAME to initialize ver.
            if (helper != null) {
                helper.initializeVerifier(verifierFileName, logPassword, verPassword);
                try {
                    Debug.message(FileName + ":Trying to start the Verifier Thread");
                    Logger logger = (com.sun.identity.log.Logger) Logger.getLogger(FileName);
                    Handler[] handlers = logger.getHandlers();
                    ((com.sun.identity.log.handlers.SecureFileHandler) handlers[0]).startVerifierThread();
                    Debug.message(FileName + ":Started Log Verifier thread");
                } catch (Exception ex) {
                    Debug.error(FileName + ":Unable to start Verifier Thread", ex);
                // throw custom exception
                }
            }
            verificationInitialized = true;
        }
    } catch (Exception e) {
        Debug.error("Error initializing Verification", e);
    }
}
Also used : Enumeration(java.util.Enumeration) Handler(java.util.logging.Handler) Logger(com.sun.identity.log.Logger) NullLocationException(com.iplanet.log.NullLocationException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AMPassword(com.sun.identity.security.keystore.AMPassword) LogManager(java.util.logging.LogManager) SecureLogHelper(com.sun.identity.log.secure.SecureLogHelper)

Example 28 with LogManager

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();
}
Also used : LogManager(java.util.logging.LogManager)

Example 29 with LogManager

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;
}
Also used : Logger(java.util.logging.Logger) LogManager(java.util.logging.LogManager)

Example 30 with LogManager

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();
    }
}
Also used : Level(java.util.logging.Level) 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