本文整理汇总了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();
}
}
示例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");
}
示例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();
}
示例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);
}
示例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()));
}
}
示例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();
}
}
示例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));
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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));
}
示例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();
}
示例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));
}
示例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));
}
示例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));
}