【问题标题】:java.util.logging with Webspherejava.util.logging 与 Websphere
【发布时间】:2013-04-03 06:01:59
【问题描述】:

我使用 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方法中添加如下代码sn-p。

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 文件?

【问题讨论】:

  • 尝试绝对文件路径/名称

标签: java jakarta-ee logging websphere java.util.logging


【解决方案1】:

在 WebSphere 服务器中,您尝试执行的操作的效果是不仅要更改应用程序的日志配置,还要更改整个服务器的日志配置。由于 WebSphere 本身使用 java.util.logging,这意味着 WebSphere 内部记录的所有内容都与应用程序日志位于同一个文件中。那将毫无意义,因为那样您也可以使用标准的 WebSphere 日志文件(SystemOut.logtrace.log)。

另外,由于WebSphere安装了自己的LogHandler,很可能会禁止使用readConfiguration()方法。

【讨论】:

    【解决方案2】:

    使用readConfiguration(is) 从输入流中读取配置。您的代码设置了一个具有相对路径的属性,但 JVM 无法查看它。

    Properties prop = System.getProperties();
    prop.setProperty("java.util.logging.config.file", "logging.properties");
    

    调用不带参数的 readConfiguration() 方法只会重新加载属性,由于您的路径是相对的,因此可能不会加载。

    public void readConfiguration()
                           throws IOException,SecurityException
    Reinitialize the logging properties and reread the logging configuration.
    

    使用属性的绝对路径或传递Inputstream。这是example loading the properties from a file 并使用InputStream

    【讨论】:

    • 感谢迪帕克的回答。我尝试了您提到的两种方法。我使用 Inputstream 来加载属性。但没有运气。 FileInputStream configFile = new FileInputStream("/WEB-INF/classes/logging.properties");首选项.load(configFile); LogManager.getLogManager().readConfiguration(configFile);我需要在管理控制台中做任何额外的配置吗?
    • hmmm... 当您尝试实例化文件时,控制台/日志文件上没有异常?您是否尝试过使用调试器单步执行代码?
    • 我收到 FileNotFound 异常。 java.io.FileNotFoundException: \WEB-INF\classes\logging.properties(系统找不到指定的路径。)
    • 那是你的问题。您应该将资源作为来自类加载器的流加载。使用getResourceAsStream
    • 当我使用 getResourceAsStream 时,属性文件正在加载,但我只在控制台中获取日志消息。不在任何日志文件中。
    猜你喜欢
    • 2011-03-10
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 2014-04-05
    • 2010-09-26
    • 1970-01-01
    相关资源
    最近更新 更多