Search in sources :

Example 6 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 7 with FileHandler

use of java.util.logging.FileHandler in project azure-tools-for-java by Microsoft.

the class AzureActionsComponent method initLoggerFileHandler.

private void initLoggerFileHandler() {
    try {
        String loggerFilePath = Paths.get(CommonSettings.settingsBaseDir, "corelibs.log").toString();
        System.out.println("Logger path:" + loggerFilePath);
        logFileHandler = new FileHandler(loggerFilePath, false);
        java.util.logging.Logger l = java.util.logging.Logger.getLogger("");
        logFileHandler.setFormatter(new SimpleFormatter());
        l.addHandler(logFileHandler);
        // FIXME: use environment variable to set level
        l.setLevel(Level.INFO);
        l.info("=== Log session started ===");
    } catch (IOException e) {
        e.printStackTrace();
        LOG.error("initLoggerFileHandler()", e);
    }
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

Example 8 with FileHandler

use of java.util.logging.FileHandler in project azure-tools-for-java by Microsoft.

the class Activator method initAzureToolsCoreLibsLoggerFileHandler.

private void initAzureToolsCoreLibsLoggerFileHandler() {
    try {
        String loggerFilePath = Paths.get(CommonSettings.settingsBaseDir, "corelibs.log").toString();
        System.out.println("Logger path:" + loggerFilePath);
        logFileHandler = new FileHandler(loggerFilePath, false);
        java.util.logging.Logger l = java.util.logging.Logger.getLogger("");
        logFileHandler.setFormatter(new SimpleFormatter());
        l.addHandler(logFileHandler);
        // FIXME: use environment variable to set the level
        l.setLevel(Level.INFO);
        l.info("=== Log session started ===");
    } catch (IOException e) {
        e.printStackTrace();
        log("initAzureToolsCoreLibsLoggerFileHandler@Activator", e);
    }
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

Example 9 with FileHandler

use of java.util.logging.FileHandler in project enclojure by EricThorsen.

the class launcher method main.

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < args.length; ++i) {
        sb.append(args[i]);
        sb.append(" ");
    }
    try {
        FileHandler fh = new FileHandler("%t" + java.io.File.separator + "repl%glog");
        l.addHandler(fh);
        requireFn.invoke(Symbol.create("org.enclojure.repl.main"));
        if (args.length > 1) {
            String[] _args = new String[args.length - 1];
            System.arraycopy(args, 1, _args, 0, _args.length);
            applyFn.invoke(setupTrackingFn, args);
        }
    } catch (Exception ex) {
        Logger.getLogger(launcher.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : FileHandler(java.util.logging.FileHandler)

Example 10 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)

Aggregations

FileHandler (java.util.logging.FileHandler)43 IOException (java.io.IOException)19 File (java.io.File)12 SimpleFormatter (java.util.logging.SimpleFormatter)9 Handler (java.util.logging.Handler)7 LogRecord (java.util.logging.LogRecord)5 ConsoleHandler (java.util.logging.ConsoleHandler)4 Formatter (java.util.logging.Formatter)3 Logger (java.util.logging.Logger)3 Config (edu.neu.ccs.pyramid.configuration.Config)2 Setup (org.openjdk.jmh.annotations.Setup)2 FileLogFormatter (org.opensolaris.opengrok.logger.formatter.FileLogFormatter)2 LogFormatter (com.facebook.buck.log.LogFormatter)1 HumanReadableException (com.facebook.buck.util.HumanReadableException)1 NaviLogFormatter (com.google.security.zynamics.binnavi.Log.NaviLogFormatter)1 FileReadException (com.google.security.zynamics.binnavi.config.FileReadException)1 RcvrExceptionHandler (edu.cmu.cs.hcii.cogtool.ui.RcvrExceptionHandler)1 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)1 NewlineLogFormatter (edu.stanford.nlp.util.logging.NewlineLogFormatter)1 FileInputStream (java.io.FileInputStream)1