以编程方式设置java.util.logging目标 [英] Setting java.util.logging destination programmatically

查看:73
本文介绍了以编程方式设置java.util.logging目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java.util.logging进行日志记录和跟踪.如何在Java应用程序中动态设置应将日志写入的文件.

I'm using java.util.logging for logging and tracing purposes. How can I within a Java application dynamically set the file to which the log should be written.

推荐答案

java.util.logging.FileHandler可能会为您完成其工作.以下代码段显示了一个简单的示例,该示例如何以编程方式设置日志记录目标:

The java.util.logging.FileHandler might do its job for you. The following code snippet shows a simple example how to do set the logging destination programmatically:

    Logger logger = Logger.getLogger("my.logger.name");
    try {
        FileHandler handler = new FileHandler("application.log", true);
        logger.addHandler(handler);
    } catch (IOException e) {
        throw new IllegalStateException("Could not add file handler.", e);
    }
    logger.info("Hello Logger!");

这篇关于以编程方式设置java.util.logging目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆