如何让 root 和命名记录器的文件处理程序记录到同一文件?
我的目标是使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试将 log4j 样式配置应用于 java.util.logging。如果要使用不同的格式化程序创建多个 FileHandler,则必须使用 config 选项来安装每个 FileHandler。创建一个配置类:
然后将以下内容添加到您的logging.properties中:
否则,您可以子类化FileHandler只是为了创建一个可以在您的logging.properties中使用的新类名。
您必须使用 %g选项来区分轮换日志和 %u 来处理冲突。来自 FileHandler 文档:
默认情况下,所有指定的记录器都会写入根记录器的处理程序。因此,根据您正在执行的操作,您可能不需要将文件处理程序附加到指定的记录器。
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:
Then add the following to your logging.properties:
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:
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.