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