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?");  

}

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

最佳答案

修改代码如下

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

这不会创建多个文件,而是会附加到同一个文件。

关于Java单个日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26944302/

相关文章:

java - 在 Android 中根据 GroupID 获取联系人

java - 我遇到错误并且陷入此错误 : E/RecyclerView: No adapter attached; skipping layout

java - 禁用 Spring Boot Web 客户端日志

java - 是否有一个 Java 库可以从数字列表中创建数字范围?

java - 片段页面findViewByID无效

java - 在倒数计时器内播放声音不会停止

java - Spring Web 服务客户端使用智能卡签名

java.io.Windows 文件系统?

java - 数组长度不同,Junit 中预期长度 = 1 实际长度 = 0

java - Android Studio代码问题