如何让 root 和命名记录器的文件处理程序记录到同一文件?

发布于 2024-12-18 12:13:48 字数 852 浏览 2 评论 0原文

我的目标是使用 java.util.logging(jdk1.6)

  • 将所有日志写入同一个日志文件,并
  • 进行日志轮换
  • ,可以在文件模式中使用 %u 以避免与

我配置的根记录器和多个 其他程序发生潜在的文件冲突命名记录器,其中 根记录器的处理程序设置为 java.util.logging.FileHandler,其设置

java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern = xxx.%u.log
java.util.logging.FileHandler.limit = 200000
java.util.logging.FileHandler.count = 20
java.util.logging.FileHandler.append = true

和命名记录器的处理程序设置为 customFileH 使用自定义设置

customFileH.class = java.util.logging.FileHandler
customFileH.level = ALL
customFileH.formatter = xxxFormat

当我运行程序时,根记录器会将日志写入xxx.0.log,

而命名记录器会将日志写入xxx.0.log.1,作为不同的日志文件。

即使我在文件模式中取出了“%u”;备注文件轮换配置, 他们是否写入不同的日志文件。 :(

感谢您提前发表评论。:)

My goal is to use java.util.logging(jdk1.6)

  • write all logs to the same log file and
  • do log rotation
  • can use %u in file pattern to avoid potential file conflict with other program

I had configured a root logger and several named logger, where
root logger's handler set to java.util.logging.FileHandler with settings

java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern = xxx.%u.log
java.util.logging.FileHandler.limit = 200000
java.util.logging.FileHandler.count = 20
java.util.logging.FileHandler.append = true

and named loggers' handler set to customFileH
with customized settings

customFileH.class = java.util.logging.FileHandler
customFileH.level = ALL
customFileH.formatter = xxxFormat

When I run the program, the root logger will write log to xxx.0.log,

while named logger will write log to xxx.0.log.1, as different log files.

Even I took out "%u" in file pattern; remark file rotation configs,
did them write to different log files. :(

Thanks for any comment in advance. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2024-12-25 12:13:48

看起来您正在尝试将 log4j 样式配置应用于 java.util.logging。如果要使用不同的格式化程序创建多个 FileHandler,则必须使用 config 选项来安装每个 FileHandler。创建一个配置类:

package so;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;

public final class LoggingConfig {

    /**
     * Pin the loggers.
     */
    private static final Logger root = Logger.getLogger("");
    private static final Logger named = Logger.getLogger("some.name");

    /**
     * Configure the loggers.
     * @throws Exception if there is a problem.
     */
    public LoggingConfig() throws Exception {
        root.addHandler(newRootHandler());
        named.addHandler(newNamedHandler());
    }

    private FileHandler newRootHandler() throws IOException {
        FileHandler f = new FileHandler("simple%g.%u.log", 200000, 20, true);
        f.setFormatter(new SimpleFormatter());
        return f;
    }

    private FileHandler newNamedHandler() throws IOException {
        FileHandler f = new FileHandler("xml%g.%u.log", 200000, 20, true);
        f.setFormatter(new XMLFormatter());
        return f;
    }
}

然后将以下内容添加到您的logging.properties中:

config=so.LoggingConfig

否则,您可以子类化FileHandler只是为了创建一个可以在您的logging.properties中使用的新类名。

您必须使用 %g选项来区分轮换日志和 %u 来处理冲突。来自 FileHandler 文档:

因此,如果三个进程都尝试登录到fred%u.%g.txt,那么它们最终可能会使用fred0.0.txt、fred1.0.txt、fred2.0.txt作为它们的第一个文件旋转序列。

默认情况下,所有指定的记录器都会写入根记录器的处理程序。因此,根据您正在执行的操作,您可能不需要将文件处理程序附加到指定的记录器。

Looks like you are trying to apply log4j style configuration to java.util.logging. If you want to create multiple FileHandlers with different formatters then you have to use the config option to install each FileHandler. Create a config class:

package so;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;

public final class LoggingConfig {

    /**
     * Pin the loggers.
     */
    private static final Logger root = Logger.getLogger("");
    private static final Logger named = Logger.getLogger("some.name");

    /**
     * Configure the loggers.
     * @throws Exception if there is a problem.
     */
    public LoggingConfig() throws Exception {
        root.addHandler(newRootHandler());
        named.addHandler(newNamedHandler());
    }

    private FileHandler newRootHandler() throws IOException {
        FileHandler f = new FileHandler("simple%g.%u.log", 200000, 20, true);
        f.setFormatter(new SimpleFormatter());
        return f;
    }

    private FileHandler newNamedHandler() throws IOException {
        FileHandler f = new FileHandler("xml%g.%u.log", 200000, 20, true);
        f.setFormatter(new XMLFormatter());
        return f;
    }
}

Then add the following to your logging.properties:

config=so.LoggingConfig

Otherwise, you can subclass FileHandler just to create a new class name that can be used in your logging.properties.

You have to use the %g option to distinguish rotated logs and %u to deal with conflicts. From the FileHandler documentation:

Thus if three processes were all trying to log to fred%u.%g.txt then they might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as the first file in their rotating sequences.

By default all of the named loggers will write to the handlers of the root logger. So depending on what you are doing you may not need to attach file handlers to the named loggers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文