gpt4 book ai didi

java - 如何配置logging.properties将内容放入Java类中(没有包装模式)?

转载 作者:行者123 更新时间:2023-12-02 12:34:13 24 4
gpt4 key购买 nike

我的 tomcat/conf 目录中有一个通用的logging.properties..

如何修改它才能处理(使用 java 类)我的应用程序生成的所有日志?

有没有办法在不使用包装器(来自 Log API)类的情况下做到这一点?

最佳答案

您可以使用 Handler处理正在记录的数据的子类。

然后,您可以通过配置文件或以编程方式通过 Logger 类的 addHandler 方法将其添加到根记录器。

例如,您可以创建一个 Handler 子类 FoobarHandler,它在控制台上打印所有记录的数据,并以“Foobar”文本为前缀:

public class MyHandler extends Handler {

@Override
public void close() throws SecurityException {
// housekeeping before handler close
}

@Override
public void flush() {
// not really needed as data is processed without any sort of buffering
}

@Override
public void publish(LogRecord record) {
System.out.println("Foobar " + record.getMessage().toUpperCase());
}

}

在配置文件中,将处理程序的类名(即 myapp.FoobarHandler)添加到全局处理程序配置中:

# Global logging properties.
# ------------------------------------------
# The set of handlers to be loaded upon startup.
# Comma-separated list of class names.
# (? LogManager docs say no comma here, but JDK example has comma.)
handlers=java.util.logging.FileHandler, myapp.FoobarHandler

# Default global logging level.
# Loggers and Handlers may override this level
.level=INFO

# Loggers
# ------------------------------------------
# Loggers are usually attached to packages.
# Here, the level for each package is specified.
# The global level is used by default, so levels
# specified here simply act as an override.
myapp.ui.level=ALL
myapp.business.level=CONFIG
myapp.data.level=SEVERE

关于java - 如何配置logging.properties将内容放入Java类中(没有包装模式)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14964817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com