java - 如何生成由 websphere 9 上运行的应用程序分隔的日志文件?我使用 java.util.logging 来生成日志

标签 java ejb websphere java.util.logging

我使用 websphere 9 应用程序服务器来部署 war 和 Ear,并使用 java.util.logging 将日志生成到应用程序中。我尝试使用属性文件来配置 LogManager 的 FileHandler,但 websphere 将所有其他日志写入我的文件中。 我不使用 log4j,因为我无法在运行时设置日志级别。

是否可以使用 java.util.logging 通过 websphere 上的应用程序创建不同的文件日志?

This is my properties file Logger.properties

handlers= java.util.logging.FileHandler
#java.util.logging.ConsoleHandler.level = INFO
#java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

# Set the default formatter to be the simple formatter
java.util.logging.FileHandler.formatter =java.util.logging.SimpleFormatter
# Use UTF-8 encoding
java.util.logging.FileHandler.encoding  = UTF-8
# Write the log files to some file pattern
java.util.logging.FileHandler.pattern = C:/Users/pmendez/Documents/Log/testLogs.log
# Limit log file size to 5 Kb
java.util.logging.FileHandler.limit = 5000
# Keep 10 log files
java.util.logging.FileHandler.count = 10
#Customize the SimpleFormatter output format 
java.util.logging.SimpleFormatter.format = %d [%t] %-5p (%F:%L) - %m%n 

最佳答案

I try to use properties file to configure the FileHandler of the LogManager, but websphere write ALL other logs on my file.

根据您的logging.properties,您将FileHandler附加到根记录器。预计会看到来自 WebSphere 的日志记录,因为默认情况下 Logger::setUseParentHandlers设置为 true。

Is possible make differents file logs by application over websphere with java.util.logging ?

您必须执行以下操作之一:

  1. 将 FileHandler 附加到应用程序的根记录器,这样 FileHandler 就不会附加到 WebSphere 应用程序的父级。假设应用程序包是com.my.app.rules,您可以添加条目com.my.app.rules.handlers=java.util.logging.FileHandler并删除根记录器的附件。接下来,您从代码中请求记录器并将其固定到内存,以便该包成为应用程序代码的新根记录器。
  2. 创建java.util.logging.Filter并将其安装在 FileHandler 上。

  3. 关闭 WebSphere 的日志记录。您可以通过将根记录器设置为关闭并强制所有记录器说出信息来做到这一点。

    .level=OFF com.my.app.rules.class1.level=信息 com.my.app.rules.class2.level=信息 com.my.app.rules.class3.level=信息

It's possible set one FileHandler by app ?

默认java.util.logging.LogManager仅支持一组 FileHandler 设置。您可以将实例附加到各种记录器,但不允许通过logging.properties使用具有不同设置的实例。但是,LogManager 确实支持文档:

A property "config". This property is intended to allow arbitrary configuration code to be run. The property defines a whitespace or comma separated list of class names. A new instance will be created for each named class. The default constructor of each class may execute arbitrary code to update the logging configuration, such as setting logger levels, adding handlers, adding filters, etc.

因此,您需要做的是将一个配置类与您的应用程序捆绑在一起,该应用程序执行所需的 FileHandler 和记录器配置,并修改您的logging.properties 来加载它。这种方法的一个问题是 LogManager 只能看到来自系统类加载器的类。

关于java - 如何生成由 websphere 9 上运行的应用程序分隔的日志文件?我使用 java.util.logging 来生成日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57632635/

相关文章:

java - 从 googlemaps v2 中删除折线

java - Selenium webdriver 错误无法在(bamboo)上找到元素

jakarta-ee - Java EE NamedQuery 和 JOIN 语句 - 导致 EJB 异常

java - TomEE 中使用 EJB 3.1 的推土机

java - 如何找到 javax.transaction.RollbackException 的原因?

java - 停止监听一个端口

java - 子容器在启动期间失败 java.util.concurrent.ExecutionException :

java - 更改 JOptionPane 中的 OK Cancel 字符串

延迟后的 Java EJB 调用方法

java - 在 Eclipse 中启动 Websphere 8