我使用 IBM Rational Application Developer (RAD) 创建了一个动态 Web 项目。我使用 java.util.logging 作为日志记录框架。我把logging.properties直接放在WEB-INF/classes里。
我面临的问题是,即使我将它放在 WEB-INF/classes 中,应用程序也无法加载 logging.properties。我在 WebSphere Application Server 管理员控制台中添加了以下通用 JVM 参数
-Djava.util.logging.config.file="logging.properties"
我在 servlet init 方法中添加了以下代码片段。
Properties prop = System.getProperties();
prop.setProperty("java.util.logging.config.file", "logging.properties");
System.out.println("Is file exists " + file.exists());
try {
LogManager.getLogManager().readConfiguration();
} catch (IOException ex) {
ex.printStackTrace();
}
我在 logging.properties 中关闭了控制台级调试,所以我不应该在控制台中获取日志记录。但目前我在控制台中获取日志,而不是在我在 logging.properits 中提到的日志文件中。
logging.properties
#------------------------------------------
# Handlers
#-----------------------------------------
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
# Default global logging level
.level=ALL
# ConsoleHandler
java.util.logging.ConsoleHandler.level=OFF
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# FileHandler
java.util.logging.FileHandler.level=FINE
# Naming style for the output file:
java.util.logging.FileHandler.pattern=${SERVER_LOG_ROOT}/nyllogs/loadData.log
# Name of the character set encoding to use
java.util.logging.FileHandler.encoding=UTF8
# Limiting size of output file in bytes:
java.util.logging.FileHandler.limit=25000000
# Number of output files to cycle through
java.util.logging.FileHandler.count=2
# Style of output (Simple or XML):
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
请告诉我为什么应用程序无法获取 logging.properties 文件?
最佳答案
在 WebSphere 服务器中,您尝试执行的操作的效果是不仅更改应用程序的日志记录配置,而且更改整个服务器的日志记录配置。由于 WebSphere 本身使用 java.util.logging,这意味着 WebSphere 内部记录的所有内容都将进入与应用程序日志相同的文件。那将毫无意义,因为那样您还不如使用标准的 WebSphere 日志文件(SystemOut.log
和 trace.log
)。
此外,由于 WebSphere 安装了它自己的 LogHandler
,它很可能会禁止使用 readConfiguration()
方法。
关于使用 Websphere 进行 java.util.logging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15942061/