简体   繁体   English

如何在这个简单的Java Logging实现中附加日志文件?

[英]How can I append to log files in this simple Java Logging implementation?

I got the following class to create and manage a Logger . 我有以下课程来创建和管理Logger Whenever across the code and program execution, calls to static getLogger() catch blocks are used to log. 每当跨代码和程序执行时,都会使用对静态getLogger() catch块的调用进行记录。

public class Log {
    private static final Logger logger = Logger.getLogger("MyLog");  

    public static void iniciarLog() throws IOException {
        FileHandler fh;  

        try { 
//          fh = new FileHandler(System.getProperty("user.home")+System.getProperty("file.separator")+"TorrentDownloader.log");  
            fh = new FileHandler("%h/TorrentDownloader.log");  
            logger.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();  
            fh.setFormatter(formatter);  

            logger.info("Se inició el log"); 
        } catch (SecurityException | IOException e) {  
            logger.severe("Error al crear el log");
        } 
    }

    public static Logger getLogger() {
        return logger;
    }
}

However, how can I append to such logging file? 但是,如何附加到此类日志文件? All examples I've seen change a lot this implementation which I like as it's clear, brief and simple. 我见过的所有例子都改变了很多我喜欢的实现,因为它清晰,简洁。

From the FileHandler constructor , you can specify a boolean to specify an append mode. FileHandler 构造函数中 ,您可以指定一个boolean来指定追加模式。

Do as following: 请执行以下操作:

fh = new FileHandler("%h/TorrentDownloader.log", true);  

使用不同的构造函数

fh = new FileHandler("%h/TorrentDownloader.log", true);  

You can use this constructor: 您可以使用此构造函数:

FileHandler handler = new FileHandler(String pattern, boolean append);

and in your case, it's: 在你的情况下,它是:

fh = new FileHandler("%h/TorrentDownloader.log", true);

This constructor creates a FileHandler with a file name pattern, and a boolean telling whether the FileHandler should append to any existing files or not. 此构造函数创建一个带有文件名模式的FileHandler,以及一个告诉FileHandler是否应附加到任何现有文件的布尔值。

And this article has a full explanation. 本文有一个完整的解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中为LinkedList实现实现append和deleteNode方法? - How can I implement append and deleteNode methods for a LinkedList implementation in Java? 如何使用java.util.logging将不同的日志条目写入两个不同的日志文件? - How do I write different log entries to two different log files using java.util.logging? Java日志记录-将字符串附加到上一个日志消息 - Java logging - append a string to previous log message 可以将java.util.logging配置为使用压缩日志文件吗? - Can java.util.logging be configured to use compressed log files? 如何在Java日志记录中将不同的日志级别发送到不同的文件/输出? - How to send different log levels to different files/outputs in Java logging? java util logging.properties:如何记录到两个不同的文件 - java util logging.properties: How to log to two different files 如何输出当前 java.util.logging.Logger 日志文件名的名称? - How can I output the name of the current java.util.logging.Logger log file name? 如何在java.util.logging中获取记录器,以便我可以在运行时更改其日志级别? - How to get loggers in java.util.logging so i can change their log level at runtime? Java Logging API生成空日志文件 - Java Logging API generating empty log files 如何生成由在Websphere 9上运行的应用分隔的日志文件? 我使用java.util.logging生成日志 - How generate log files separated by apps running over websphere 9 ? I use java.util.logging to generate logs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM