gpt4 book ai didi

java - 关闭 REST API 上的 FileHandler 的最佳方法

转载 作者:行者123 更新时间:2023-12-02 09:20:44 25 4
gpt4 key购买 nike

我不知道如何在 API REST 上处理我的 FileHandler

我在 Weblogic 上安装了 REST API,我正在使用 Java 日志记录,当我的应用程序启动时,会初始化记录器并打开 FileHandler。就像我从不关闭 FileHandler 一样,创建的 .lck 文件保留在我的日志文件夹中。我真的不在乎该文件是否存在,但是当我重新部署应用程序时,就像 FileHandler 仍然打开一样,我的应用程序会启动一个新的日志文件(例如:myLog.log.0myLog.log.1)。我读到 JVM 本身应该关闭 FileHandler,但这并没有发生。我尝试使用 addShutodownHook 来关闭 FileHandler,但该代码不起作用,如果我重新部署该应用程序,它仍然保持打开状态。

@ApplicationPath("api")
public class GenericApplication extends Application {
public GenericApplication() {
initSwagger();
initLog();

Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
CtgLogger.fileJson.close();
// fileJson is my FileHandler, i made it public static to call him here.
}
});
}

我的 initLog() 方法仅调用下一个 CtgLogger.setup()...

public static void setup() throws IOException {
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
String level = PropertiesUtil.get("ctg.log.level");
logger.setLevel(LEVEL_MAP.get(level));

String filePath = PropertiesUtil.get("ctg.log.path");
String fileSize = PropertiesUtil.get("ctg.log.max.size");
String fileCount = PropertiesUtil.get("ctg.log.max.count");
if (StringUtils.isNotEmpty(fileSize) && StringUtils.isNotEmpty(fileSize) &&
NumberUtils.isNumber(fileSize) && NumberUtils.isNumber(fileCount)) {
fileJson = new FileHandler(filePath != null ? filePath : DEFAULT_LOG_NAME,
Integer.parseInt(fileSize), Integer.parseInt(fileCount), true);
} else {
fileJson = new FileHandler(filePath != null ? filePath : DEFAULT_LOG_NAME);
}
jsonFormatter = new JsonCustomFormatter();
fileJson.setFormatter(jsonFormatter);
for (Handler h: logger.getHandlers())
h.close();
logger.addHandler(fileJson);
}

就这样,然后我只需调用我的端点并使用我的记录器。

我的问题是,每次调用端点时是否应该打开和关闭 FileHandler?或者有更好的方法吗?

最佳答案

My question is, should I open and close my FileHandler each time a endpoint is called? Or is there a better way for doing this?

无需每次调用​​端点时都打开和关闭。

  1. 删除关闭 Hook 代码。 LogManager 将在关闭时关闭任何附加文件处理程序。
  2. logger 更改为静态最终引用,使其为 impossible to garbage collect 。这可确保您的设置得到维护。
  3. 由于您使用的是 JavaEE,因此请使用 javax.annotation.PostConstructjavax.annotation.PreDestroy注释来管理您的记录器设置。
  4. 如果您手动关闭 FileHandler,请确保调用 Logger::remove(Handler)以便处理程序可以被垃圾收集。您当前的代码仅关闭处理程序。
    private final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private volatile FileHandler fileJson;

public void preDestroy() {
Handler h = this.fileJson;
this.fileJson = null;
if (h != null) {
h.close();
}
logger.removeHandler(h);
}

@PostConstruct
public void setup() throws IOException {
if (fileJson != null) {
preDestroy();
}

String level = PropertiesUtil.get("ctg.log.level");
logger.setLevel(LEVEL_MAP.get(level));

String filePath = PropertiesUtil.get("ctg.log.path");
String fileSize = PropertiesUtil.get("ctg.log.max.size");
String fileCount = PropertiesUtil.get("ctg.log.max.count");
if (StringUtils.isNotEmpty(fileSize) && StringUtils.isNotEmpty(fileSize) &&
NumberUtils.isNumber(fileSize) && NumberUtils.isNumber(fileCount)) {
fileJson = new FileHandler(filePath != null ? filePath : DEFAULT_LOG_NAME,
Integer.parseInt(fileSize), Integer.parseInt(fileCount), true);
} else {
fileJson = new FileHandler(filePath != null ? filePath : DEFAULT_LOG_NAME);
}
jsonFormatter = new JsonCustomFormatter();
fileJson.setFormatter(jsonFormatter);
for (Handler h: logger.getHandlers()) {
h.close();
logger.removeHander(h);
}
logger.addHandler(fileJson);
}

关于java - 关闭 REST API 上的 FileHandler 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58711944/

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