【发布时间】:2019-08-28 15:09:51
【问题描述】:
我需要为 websphere application server 9 上安装和运行的每个应用程序生成一个日志文件。 我使用 JUL 生成日志文件。我的解决方案是创建一个从 FileHandler 继承的特定类,并通过配置文件设置日志属性。
这是我的代码:
//Read config file
LogManager.getLogManager().readConfiguration(LoggerJUL.class.getResourceAsStream("/Logger.properties"));
//Add handler to logger
Logger.getLogger(clazz)).addHandler(new PersonalFileHandler());
PersonalFileHandler 扩展了 FileHandler,属性由运行时 FileHandler 类的 configure 方法设置。
通过这种方式,我实现了通过在 Websphere 上运行的应用程序创建一个日志文件,而不会覆盖服务器日志的目标。
虽然我实现了部分目标,但如果原始日志文件被锁定,则会生成额外的文件,就像这样:testLogs.log.0、testLogs.log.1、testLogs.log.0.1 等。 我阅读了许多建议和解决方案,但我无法阻止这个问题。 有什么建议吗?
handlers = com.mucam.xxxx.PersonalFileHandler
# Set the default formatter to be the simple formatter
com.mucam.xxxx.PersonalFileHandler.formatter = java.util.logging.SimpleFormatter
# Write the log files to some file pattern
com.mucam.xxxx.PersonalFileHandler.pattern = C:/Users/pmendez/Documents/Log/testLogs.log
# Limit log file size to 5 Kb
com.mucam.xxxx.PersonalFileHandler.limit = 5000
# Keep 10 log files
com.mucam.xxxx.PersonalFileHandler.count = 10
#Customize the SimpleFormatter output format
java.util.logging.SimpleFormatter.format = %d{ISO8601} [%t] %-5p %c %x - %m%n
#Append to existing file
com.mucam.xxxx.PersonalFileHandler.append = true
【问题讨论】:
标签: java java.util.logging filehandler