log4j 和 java util logging 可以共存吗

Can log4j and java util logging coexist

我的应用程序使用 log4j 但 OkHttpClient 使用 java util 日志记录。因此,除了 log4j.properties,我还创建了一个 logging.properties 文件,其中包含以下内容:

handlers=java.util.logging.FileHandler

.level=FINE

okhttp3.internal.http2.level=FINE

java.util.logging.FileHandler.pattern = logs/%hjava%u.log

java.util.logging.FileHandler.limit = 50000

java.util.logging.FileHandler.count = 1

java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

java.util.logging.ConsoleHandler.level = FINE

java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

然后我将其添加到用于启动应用程序的 jvm 参数中 -Djava.util.logging.config.file="file://${BASE_DIR}/logging.properties"

但我没有看到文件处理程序指示的任何正在创建的新文件夹。有人知道为什么吗?

But I don't see any new folders being created as indicated by the Filehandler. Any one know why?

FileHandler 不会创建任何 new folders。必须在 FileHandler 创建文件之前创建一个目录。

系统属性 需要位于文件系统上的文件路径它不会使用美元符号语法扩展系统属性或环境变量。

您可以使用基于工作目录的相对路径,或者您必须使用 logging.properties 的绝对路径。无法将日志记录属性打包到存档中。

如果您想解决此限制,则需要创建 a custom config class and use the java.util.logging.config.class property in conjunction with the java.util.logging.config.file property. You then write a class that reads the file://${BASE_DIR}/logging.properties and performs the needed transformation into a path to a file. Then update the configuration if you are using JDK9 or newer. On older versions you need to use readConfiguration and add code to work work around limitations of the LogManager