Search in sources :

Example 1 with FileHandler

use of java.util.logging.FileHandler in project jdk8u_jdk by JetBrains.

the class CheckZombieLockTest method createFileHandler.

private static FileHandler createFileHandler(File writableDir) throws SecurityException, RuntimeException, IOException {
    // Test 1: make sure we can create FileHandler in writable directory
    try {
        FileHandler handler = new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
        handler.publish(new LogRecord(Level.INFO, handler.toString()));
        handler.flush();
        return handler;
    } catch (IOException ex) {
        throw new RuntimeException("Test failed: should have been able" + " to create FileHandler for " + "%t/" + WRITABLE_DIR + "/log.log in writable directory.", ex);
    }
}
Also used : LogRecord(java.util.logging.LogRecord) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

Example 2 with FileHandler

use of java.util.logging.FileHandler in project jdk8u_jdk by JetBrains.

the class CheckZombieLockTest method testFileHandlerReuse.

private static void testFileHandlerReuse(File writableDir) throws IOException {
    List<File> before = listLocks(writableDir, true);
    System.out.println("before: " + before.size() + " locks found");
    try {
        if (!before.isEmpty()) {
            throw new RuntimeException("Expected no lock file! Found: " + before);
        }
    } finally {
        before.stream().forEach(CheckZombieLockTest::delete);
    }
    FileHandler handler1 = createFileHandler(writableDir);
    System.out.println("handler created: " + handler1);
    List<File> after = listLocks(writableDir, true);
    System.out.println("after creating handler: " + after.size() + " locks found");
    if (after.size() != 1) {
        throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after);
    }
    final File lock = after.get(0);
    after.clear();
    handler1.close();
    after = listLocks(writableDir, true);
    System.out.println("after closing handler: " + after.size() + " locks found");
    if (!after.isEmpty()) {
        throw new RuntimeException("Unexpected number of lock files found for " + handler1 + ": " + after);
    }
    if (!createFile(lock, false)) {
        throw new IOException("Can't create fake lock file: " + lock);
    }
    try {
        before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        if (before.size() != 1) {
            throw new RuntimeException("Unexpected number of lock files found: " + before + " expected [" + lock + "].");
        }
        FileHandler handler2 = createFileHandler(writableDir);
        System.out.println("handler created: " + handler2);
        after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        after.removeAll(before);
        if (!after.isEmpty()) {
            throw new RuntimeException("Unexpected lock file found: " + after + "\n\t" + lock + " should have been reused");
        }
        handler2.close();
        System.out.println("handler closed: " + handler2);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
        if (supportsLocking) {
            FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()), StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND, StandardOpenOption.WRITE);
            try {
                if (fc.tryLock() != null) {
                    System.out.println("locked: " + lock);
                    handler2 = createFileHandler(writableDir);
                    System.out.println("handler created: " + handler2);
                    after = listLocks(writableDir, true);
                    System.out.println("after creating handler: " + after.size() + " locks found");
                    after.removeAll(before);
                    if (after.size() != 1) {
                        throw new RuntimeException("Unexpected lock files found: " + after + "\n\t" + lock + " should not have been reused");
                    }
                } else {
                    throw new RuntimeException("Failed to lock: " + lock);
                }
            } finally {
                delete(lock);
            }
        }
    } finally {
        List<File> finalLocks = listLocks(writableDir, false);
        System.out.println("end: " + finalLocks.size() + " locks found");
        delete(writableDir);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) File(java.io.File) FileHandler(java.util.logging.FileHandler)

Example 3 with FileHandler

use of java.util.logging.FileHandler in project adempiere by adempiere.

the class MigrateLogger method open.

/**
	 * create logger's handlers and files
	 */
public void open() {
    // create custom formatters
    // (switch display of log level, source class, and source method ON or OFF)
    MigrateLogger_Formatter traceFormatter = new MigrateLogger_Formatter(true);
    MigrateLogger_Formatter warningFormatter = new MigrateLogger_Formatter(false);
    MigrateLogger_Formatter errorFormatter = new MigrateLogger_Formatter(false);
    // create handlers for output files
    // (and activate buttons to view log files if GUI is available)
    FileHandler traceLog = null;
    FileHandler warningLog = null;
    FileHandler errorLog = null;
    String time = null;
    time = getDateTime();
    StringBuffer filename = new StringBuffer(getLogDirectory()).append("migration_").append(time);
    try {
        traceLog = new FileHandler(new StringBuffer(filename).append(".trace.log").toString());
        warningLog = new FileHandler(new StringBuffer(filename).append(".warning.log").toString());
        errorLog = new FileHandler(new StringBuffer(filename).append(".error.log").toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // create a custom filter to log messages of one and only one level
    MigrateLogger_Filter levelFilter = new MigrateLogger_Filter(Level.WARNING);
    // limit log levels in each handler
    traceLog.setLevel(getMaxLogLevel());
    warningLog.setLevel(Level.WARNING);
    warningLog.setFilter(levelFilter);
    errorLog.setLevel(Level.SEVERE);
    // customize format of handlers
    traceLog.setFormatter(traceFormatter);
    warningLog.setFormatter(warningFormatter);
    errorLog.setFormatter(errorFormatter);
    // add handlers to logger
    m_logger.addHandler(traceLog);
    m_logger.addHandler(warningLog);
    m_logger.addHandler(errorLog);
}
Also used : FileHandler(java.util.logging.FileHandler)

Example 4 with FileHandler

use of java.util.logging.FileHandler in project geode by apache.

the class AbstractLauncher method initLogger.

void initLogger() {
    try {
        this.logger.addHandler(new FileHandler(SystemUtils.CURRENT_DIRECTORY.concat("debug.log")));
        this.logger.setLevel(Level.ALL);
        this.logger.setUseParentHandlers(true);
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.err.flush();
        throw new RuntimeException(e);
    }
}
Also used : IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

Example 5 with FileHandler

use of java.util.logging.FileHandler in project robovm by robovm.

the class OldFileHandlerTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    manager.reset();
    //initProp
    props.clear();
    props.put("java.util.logging.FileHandler.level", "FINE");
    props.put("java.util.logging.FileHandler.filter", className + "$MockFilter");
    props.put("java.util.logging.FileHandler.formatter", className + "$MockFormatter");
    props.put("java.util.logging.FileHandler.encoding", "iso-8859-1");
    // limit to only two message
    props.put("java.util.logging.FileHandler.limit", "1000");
    // rotation count is 2
    props.put("java.util.logging.FileHandler.count", "2");
    // using append mode
    props.put("java.util.logging.FileHandler.append", "true");
    props.put("java.util.logging.FileHandler.pattern", "%t/log/java%u.test");
    HOMEPATH = System.getProperty("user.home");
    TEMPPATH = System.getProperty("java.io.tmpdir");
    File file = new File(TEMPPATH + SEP + "log");
    file.mkdir();
    manager.readConfiguration(propertiesToInputStream(props));
    handler = new FileHandler();
    r = new LogRecord(Level.CONFIG, "msg");
}
Also used : LogRecord(java.util.logging.LogRecord) File(java.io.File) FileHandler(java.util.logging.FileHandler)

Aggregations

FileHandler (java.util.logging.FileHandler)134 SimpleFormatter (java.util.logging.SimpleFormatter)58 IOException (java.io.IOException)49 File (java.io.File)48 Logger (java.util.logging.Logger)36 Handler (java.util.logging.Handler)34 Properties (java.util.Properties)14 ConsoleHandler (java.util.logging.ConsoleHandler)14 BalancerRunner (org.mobicents.tools.sip.balancer.BalancerRunner)13 Formatter (java.util.logging.Formatter)12 Config (edu.neu.ccs.pyramid.configuration.Config)11 Date (java.util.Date)11 LogRecord (java.util.logging.LogRecord)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Level (java.util.logging.Level)4 Pair (edu.neu.ccs.pyramid.util.Pair)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 LogFormat (de.Keyle.MyPet.api.util.LogFormat)2 CTFT (edu.neu.ccs.pyramid.calibration.CTFT)2