當前位置: 首頁>>代碼示例>>Java>>正文


Java FileHandler.close方法代碼示例

本文整理匯總了Java中java.util.logging.FileHandler.close方法的典型用法代碼示例。如果您正苦於以下問題:Java FileHandler.close方法的具體用法?Java FileHandler.close怎麽用?Java FileHandler.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.logging.FileHandler的用法示例。


在下文中一共展示了FileHandler.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import java.util.logging.FileHandler; //導入方法依賴的package包/類
public static void main(String[] args) 
{
	try 
	{
		LogManager lm = LogManager.getLogManager();
		Logger logger = Logger.getLogger("BasicLogging");
		FileHandler fh = new FileHandler("log_test.txt");
		lm.addLogger(logger);
		logger.addHandler(fh);
		logger.log(Level.INFO, "test 1");
		logger.log(Level.INFO, "test 2");
		fh.close();
	}
	catch (Exception e) {
		System.err.println("Exception thrown: " + e);
		e.printStackTrace();
	}
}
 
開發者ID:shuqin,項目名稱:ALLIN,代碼行數:19,代碼來源:BasicLogging.java

示例2: testFileHandlerClose

import java.util.logging.FileHandler; //導入方法依賴的package包/類
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:27,代碼來源:CheckZombieLockTest.java

示例3: main

import java.util.logging.FileHandler; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {

        /* Ensure directory has been created */
        new File("logs").mkdir();

        /* Get the date to be used in the filename */
        DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
        Date now = new Date();
        String date = df.format(now);

        /* Set up the filename in the logs directory */
        String logFileName = "logs/testlog-" + date + ".txt";

        /* Set up logger */
        FileHandler myFileHandler = new FileHandler(logFileName);
        myFileHandler.setFormatter(new SimpleFormatter());
        Logger ocajLogger = Logger.getLogger("OCAJ Logger");
        ocajLogger.setLevel(Level.ALL);
        ocajLogger.addHandler(myFileHandler);

        /* Log Message */
        ocajLogger.info("\nThis is a logged information message.");

        /* Close the file */
        myFileHandler.close();
    }
 
開發者ID:mdeverdelhan,項目名稱:OCA-Java-SE-7-Programmer-I,代碼行數:27,代碼來源:TestClass.java

示例4: test

import java.util.logging.FileHandler; //導入方法依賴的package包/類
public static void test(String name, Properties props) throws Exception {
    System.out.println("Testing: " + name);
    String file = props.getProperty("test.file.name");
    // create the lock files first - in order to take the path that
    // used to trigger the NPE
    Files.createFile(Paths.get(file + ".lck"));
    Files.createFile(Paths.get(file + ".1.lck"));
    final FileHandler f1 = new FileHandler();
    final FileHandler f2 = new FileHandler();
    f1.close();
    f2.close();
    System.out.println("Success for " + name);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:14,代碼來源:FileHandlerPath.java

示例5: main

import java.util.logging.FileHandler; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    File loggerDir = createLoggerDir();
    String configFilePath = loggerDir.getPath() + File.separator + CONFIG_FILE_NAME;
    File configFile = new File(configFilePath);
    createFile(configFile, false);
    System.setProperty("java.util.logging.config.file", configFilePath);
    List<FileHandler> fileHandlers = new ArrayList<>();
    try (FileWriter writer = new FileWriter(configFile)) {
        writer.write(MAX_LOCK_PROPERTY);
        writer.flush();
        // 200 raises the default limit of 100, we try 102 times
        for (int i = 0; i < 102; i++) {
            fileHandlers.add(new FileHandler(loggerDir.getPath() + File.separator + "test_%u.log"));
        }
    } catch (IOException ie) {
        throw new RuntimeException("Test Failed: " + ie.getMessage());
    } finally {
        for (FileHandler fh : fileHandlers) {
            fh.close();
        }
        FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:FileHandlerMaxLocksTest.java

示例6: main

import java.util.logging.FileHandler; //導入方法依賴的package包/類
public static void main(String[] args) 
{
	try 
	{
		LogManager lm = LogManager.getLogManager();
		Logger parentLogger, childLogger;
		FileHandler xmlHandler = new FileHandler("log_output.xml");
		FileHandler htmlHandler = new FileHandler("log_output.html");
		parentLogger = Logger.getLogger("ParentLogger");
		childLogger = Logger.getLogger("ParentLogger.ChildLogger");
		
		lm.addLogger(parentLogger);
		lm.addLogger(childLogger);
		parentLogger.setLevel(Level.WARNING);
		childLogger.setLevel(Level.ALL);
		xmlHandler.setFormatter(new XMLFormatter());
		htmlHandler.setFormatter(new HTMLFormatter());
		
		parentLogger.addHandler(xmlHandler);
		childLogger.addHandler(htmlHandler);
		
		childLogger.log(Level.FINE, "This is a fine log message");
		childLogger.log(Level.SEVERE, "This is a severe log message");
		xmlHandler.close();
		htmlHandler.close();
	}
	catch (Exception e) {
		System.err.println("Exception thrown: " + e);
		e.printStackTrace();
	}
}
 
開發者ID:shuqin,項目名稱:ALLIN,代碼行數:32,代碼來源:LoggingExample2.java

示例7: doTestDontLog

import java.util.logging.FileHandler; //導入方法依賴的package包/類
private void doTestDontLog(File log) throws IOException {
  // Set things up so that if Velocity is successfully logging then we will see its log output
  // in the temporary file we have created. This depends on Velocity falling back on JDK logging,
  // so this test won't do anything useful if its classpath contains Log4J or Commons Logging or
  // any of the other exotic logging systems that Velocity will pounce on if it sees them.
  FileHandler fileHandler = new FileHandler(log.getPath());
  fileHandler.setFormatter(new SimpleFormatter());
  Logger logger = Logger.getLogger(JdkLogChute.DEFAULT_LOG_NAME);
  logger.addHandler(fileHandler);
  logger.setLevel(Level.ALL);
  LogManager logManager = LogManager.getLogManager();
  logManager.addLogger(logger);

  // Now do a random compilation that implies using RetroFacebookProcessor.
  JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
      "foo.bar.Baz",
      "package foo.bar;",
      "",
      "import retrofacebook.RetroFacebook;",
      "",
      "@RetroFacebook",
      "public abstract class Baz {",
      "  public abstract int buh();",
      "",
      "  public static Baz create(int buh) {",
      "    return new RetroFacebook_Baz(buh);",
      "  }",
      "}");
  assert_().about(javaSource())
      .that(javaFileObject)
      .processedWith(new RetroFacebookProcessor())
      .compilesWithoutError();

  // The log file should be empty.
  fileHandler.close();
  assertEquals("", Files.toString(log, StandardCharsets.UTF_8));
}
 
開發者ID:yongjhih,項目名稱:RetroFacebook,代碼行數:38,代碼來源:NoVelocityLoggingTest.java

示例8: testRolloverLogfile

import java.util.logging.FileHandler; //導入方法依賴的package包/類
@Test
public void testRolloverLogfile() throws SecurityException, IOException {
    LoggerFactory.rolloverLogfile();

       for(File file : findLogFiles()) {
           assertTrue("Could not remove logfile: " + file, file.delete());
       }

       assertEquals(0, findLogFiles().length);

       Logger log = Logger.getLogger("");    // NOSONAR - local logger used on purpose here
       FileHandler handler = new FileHandler("build/LoggerFactoryTest.%g.%u.log", 100*1024*1024, 10);
       try {
           log.addHandler(handler);

           Logger logger = LoggerFactory.make();
           logger.info("testlog1");

           LoggerFactory.rolloverLogfile();

           logger.info("testlog2");

           LoggerFactory.rolloverLogfile();

           logger.info("testlog2");

           File[] logFiles = findLogFiles();
           assertEquals(3, logFiles.length);
       } finally {
       	handler.close();
           log.removeHandler(handler);
       }
}
 
開發者ID:centic9,項目名稱:commons-dost,代碼行數:34,代碼來源:LoggerFactoryTest.java

示例9: testRolloverLogfileException

import java.util.logging.FileHandler; //導入方法依賴的package包/類
@Test
public void testRolloverLogfileException() throws SecurityException, IOException {
    LoggerFactory.rolloverLogfile();

    for(File file : findLogFiles()) {
        assertTrue("Could not remove logfile: " + file, file.delete());
    }

    assertEquals(0, findLogFiles().length);

    final AtomicBoolean shouldFail = new AtomicBoolean(false);

    Logger log = Logger.getLogger("");    // NOSONAR - local logger used on purpose here
    FileHandler handler = new FileHandler("build/LoggerFactoryTest.%g.%u.log", 100*1024*1024, 10) {
        @Override
        public synchronized void setLevel(Level newLevel) throws SecurityException {
            if(shouldFail.get()) {
                throw new IllegalArgumentException("testexception");
            }

            super.setLevel(newLevel);
        }
    };

    try {
        log.addHandler(handler);

        Logger logger = LoggerFactory.make();
        logger.info("testlog1");

        LoggerFactory.rolloverLogfile();

        logger.info("testlog2");

        shouldFail.set(true);

        try {
            LoggerFactory.rolloverLogfile();
            fail("Should catch exception");
        } catch (IllegalStateException e) {
            TestHelpers.assertContains(ExceptionUtils.getStackTrace(e), "testexception");
        }
    } finally {
    	handler.close();
        log.removeHandler(handler);
    }
}
 
開發者ID:centic9,項目名稱:commons-dost,代碼行數:48,代碼來源:LoggerFactoryTest.java

示例10: testRolloverLogFileClosed

import java.util.logging.FileHandler; //導入方法依賴的package包/類
@Test
public void testRolloverLogFileClosed() throws SecurityException, IOException {
    LoggerFactory.rolloverLogfile();

    for(File file : findLogFiles()) {
        assertTrue("Could not remove logfile: " + file, file.delete());
    }

    assertEquals(0, findLogFiles().length);

    Logger log = Logger.getLogger("");    // NOSONAR - local logger used on purpose here
    FileHandler handler = new FileHandler("build/LoggerFactoryTest.%g.%u.log", 100*1024*1024, 10);
    try {
        log.addHandler(handler);

        handler.setLevel(Level.OFF);

        Logger logger = LoggerFactory.make();
        logger.info("testlog1");

        LoggerFactory.rolloverLogfile();

        logger.info("testlog2");

        LoggerFactory.rolloverLogfile();

        logger.info("testlog2");

        File[] logFiles = findLogFiles();
        assertEquals(1, logFiles.length);
    } finally {
    	handler.close();
        log.removeHandler(handler);
    }
}
 
開發者ID:centic9,項目名稱:commons-dost,代碼行數:36,代碼來源:LoggerFactoryTest.java

示例11: doTestDontLog

import java.util.logging.FileHandler; //導入方法依賴的package包/類
private void doTestDontLog(File log) throws IOException {
  // Set things up so that if Velocity is successfully logging then we will see its log output
  // in the temporary file we have created. This depends on Velocity falling back on JDK logging,
  // so this test won't do anything useful if its classpath contains Log4J or Commons Logging or
  // any of the other exotic logging systems that Velocity will pounce on if it sees them.
  FileHandler fileHandler = new FileHandler(log.getPath());
  fileHandler.setFormatter(new SimpleFormatter());
  Logger logger = Logger.getLogger(JdkLogChute.DEFAULT_LOG_NAME);
  logger.addHandler(fileHandler);
  logger.setLevel(Level.ALL);
  LogManager logManager = LogManager.getLogManager();
  logManager.addLogger(logger);

  // Now do a random compilation that implies using AutoCursorProcessor.
  JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
      "foo.bar.Baz",
      "package foo.bar;",
      "",
      "import auto.cursor.AutoCursor;",
      "",
      "@AutoCursor",
      "public abstract class Baz {",
      "  public abstract int buh();",
      "",
      "  public static Baz create(int buh) {",
      "    return new AutoCursor_Baz(buh);",
      "  }",
      "}");
  assert_().about(javaSource())
      .that(javaFileObject)
      .processedWith(new AutoCursorProcessor())
      .compilesWithoutError();

  // The log file should be empty.
  fileHandler.close();
  assertEquals("", Files.toString(log, StandardCharsets.UTF_8));
}
 
開發者ID:yongjhih,項目名稱:AutoCursor,代碼行數:38,代碼來源:NoVelocityLoggingTest.java

示例12: close

import java.util.logging.FileHandler; //導入方法依賴的package包/類
/** {@inheritDoc} */
@SuppressWarnings("NonSynchronizedMethodOverridesSynchronizedMethod")
@Override public void close() throws SecurityException {
    FileHandler delegate0 = delegate;

    if (delegate0 != null)
        delegate0.close();
}
 
開發者ID:apache,項目名稱:ignite,代碼行數:9,代碼來源:JavaLoggerFileHandler.java

示例13: doTestDontLog

import java.util.logging.FileHandler; //導入方法依賴的package包/類
private void doTestDontLog(File log) throws IOException {
  // Set things up so that if Velocity is successfully logging then we will see its log output
  // in the temporary file we have created. This depends on Velocity falling back on JDK logging,
  // so this test won't do anything useful if its classpath contains Log4J or Commons Logging or
  // any of the other exotic logging systems that Velocity will pounce on if it sees them.
  FileHandler fileHandler = new FileHandler(log.getPath());
  fileHandler.setFormatter(new SimpleFormatter());
  Logger logger = Logger.getLogger(JdkLogChute.DEFAULT_LOG_NAME);
  logger.addHandler(fileHandler);
  logger.setLevel(Level.ALL);
  LogManager logManager = LogManager.getLogManager();
  logManager.addLogger(logger);

  // Now do a random compilation that implies using RetrofitProcessor.
  JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
      "foo.bar.Baz",
      "package foo.bar;",
      "",
      "import retrofit.http.Retrofit;",
      "import retrofit.http.Retrofit.GET;",
      "import rx.Observable;",
      "",
      "@Retrofit",
      "public abstract class Baz {",
      "  @GET(\"/\")",
      "  public abstract Observable<Integer> buh();",
      "",
      "  public static Baz create() {",
      "    return new Retrofit_Baz();",
      "  }",
      "}");
  assert_().about(javaSource())
      .that(javaFileObject)
      .processedWith(new RetrofitProcessor())
      .compilesWithoutError();

  // The log file should be empty.
  fileHandler.close();
  assertEquals("", Files.toString(log, StandardCharsets.UTF_8));
}
 
開發者ID:yongjhih,項目名稱:retrofit2.bak,代碼行數:41,代碼來源:NoVelocityLoggingTest.java

示例14: doTestDontLog

import java.util.logging.FileHandler; //導入方法依賴的package包/類
private void doTestDontLog(File log) throws IOException {
  // Set things up so that if Velocity is successfully logging then we will see its log output
  // in the temporary file we have created. This depends on Velocity falling back on JDK logging,
  // so this test won't do anything useful if its classpath contains Log4J or Commons Logging or
  // any of the other exotic logging systems that Velocity will pounce on if it sees them.
  FileHandler fileHandler = new FileHandler(log.getPath());
  fileHandler.setFormatter(new SimpleFormatter());
  Logger logger = Logger.getLogger(JdkLogChute.DEFAULT_LOG_NAME);
  logger.addHandler(fileHandler);
  logger.setLevel(Level.ALL);
  LogManager logManager = LogManager.getLogManager();
  logManager.addLogger(logger);

  // Now do a random compilation that implies using RetroWeiboProcessor.
  JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
      "foo.bar.Baz",
      "package foo.bar;",
      "",
      "import retroweibo.RetroWeibo;",
      "",
      "@RetroWeibo",
      "public abstract class Baz {",
      "  public abstract int buh();",
      "",
      "  public static Baz create(int buh) {",
      "    return new RetroWeibo_Baz(buh);",
      "  }",
      "}");
  assert_().about(javaSource())
      .that(javaFileObject)
      .processedWith(new RetroWeiboProcessor())
      .compilesWithoutError();

  // The log file should be empty.
  fileHandler.close();
  assertEquals("", Files.toString(log, StandardCharsets.UTF_8));
}
 
開發者ID:8tory,項目名稱:SimpleWeibo,代碼行數:38,代碼來源:NoVelocityLoggingTest.java

示例15: doTestDontLog

import java.util.logging.FileHandler; //導入方法依賴的package包/類
private void doTestDontLog(File log) throws IOException {
  // Set things up so that if Velocity is successfully logging then we will see its log output
  // in the temporary file we have created. This depends on Velocity falling back on JDK logging,
  // so this test won't do anything useful if its classpath contains Log4J or Commons Logging or
  // any of the other exotic logging systems that Velocity will pounce on if it sees them.
  FileHandler fileHandler = new FileHandler(log.getPath());
  fileHandler.setFormatter(new SimpleFormatter());
  Logger logger = Logger.getLogger(JdkLogChute.DEFAULT_LOG_NAME);
  logger.addHandler(fileHandler);
  logger.setLevel(Level.ALL);
  LogManager logManager = LogManager.getLogManager();
  logManager.addLogger(logger);

  // Now do a random compilation that implies using AutoJsonProcessor.
  JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
      "foo.bar.Baz",
      "package foo.bar;",
      "",
      "import auto.json.AutoJson;",
      "",
      "@AutoJson",
      "public abstract class Baz {",
      "  public abstract int buh();",
      "",
      "  public static Baz create(int buh) {",
      "    return new AutoJson_Baz(buh);",
      "  }",
      "}");
  assert_().about(javaSource())
      .that(javaFileObject)
      .processedWith(new AutoJsonProcessor())
      .compilesWithoutError();

  // The log file should be empty.
  fileHandler.close();
  assertEquals("", Files.toString(log, StandardCharsets.UTF_8));
}
 
開發者ID:yongjhih,項目名稱:AutoJson,代碼行數:38,代碼來源:NoVelocityLoggingTest.java


注:本文中的java.util.logging.FileHandler.close方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。