有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java单日志文件

我正在使用来自here的代码在Java程序中编写一个日志文件

public static void main(String[] args) {  

Logger logger = Logger.getLogger("MyLog");  
FileHandler fh;  

try {  

    // This block configure the logger with handler and formatter  
    fh = new FileHandler("C:/temp/test/MyLogFile.log");  
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();  
    fh.setFormatter(formatter);  

    // the following statement is used to log any messages  
    logger.info("My first log");  

    } catch (SecurityException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  

    logger.info("Hi How r u?");  

}

我的问题是我得到了多个日志文件


共 (1) 个答案

  1. # 1 楼答案

    更改代码如下

    fh = new FileHandler("C:/temp/test/MyLogFile.log", true);
    

    这将不会创建多个文件,并将附加到同一个文件