我有一种情况,我想将我创建的所有日志写入文本文件.
我们使用java.util.logging.Logger API来生成日志.
我试过了:
private static Logger logger = Logger.getLogger(className.class.getName());
FileHandler fh;
fh = new FileHandler("C:/className.log");
logger.addHandler(fh);
Run Code Online (Sandbox Code Playgroud)
但仍然只在控制台上登录....
我有一种情况,我想使用记录器打印catch块中捕获的所有异常.
try {
File file = new File("C:\\className").mkdir();
fh = new FileHandler("C:\\className\\className.log");
logger.addHandler(fh);
logger.setUseParentHandlers(false);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
} catch (Exception e) {
logger.info(e);
}
Run Code Online (Sandbox Code Playgroud)
我得到错误记录器无法应用 java.io.Exception...
我担心的是,如果我在try块中执行了很多操作,并且只保留一个catch块作为catch(Exception e),那么有没有办法使用logger来打印catch块中捕获的任何异常?注意:我们使用的是java.util.logging.Logger API