记录器无法正常运行(Logger not functioning properly)

编程入门 行业动态 更新时间:2024-10-28 08:17:21
记录器无法正常运行(Logger not functioning properly)

我正在使用java.util.logging.Logger类来登录我的应用程序。 我添加了FileHandler,以便应用程序日志直接存储在log.txt文件中。

但由于某种原因,在应用程序终止后,日志远未完成。 在cmd上,我可以看到所有语句,但它们永远不会附加到文件中。

我已将FileHandler设置为Logger:

private void setLogger() { try { FileHandler hand = new FileHandler("log/log.txt", true); hand.setFormatter(new SimpleFormatter()); Logger log = Logger.getLogger(ImageRename.MAIN_LOG); //log.setUseParentHandlers(false); log.addHandler(hand); log.setLevel(Level.ALL); } catch (IOException e) { System.out.println("Could Not set logger"); } }

有冲洗的问题吗? 怎么解决? 谢谢。

PS:在调试时,我注意到它们之间

Logger.getLogger(ImageRename.MAIN_LOG).getHandlers()。长度

返回0.它应返回的位置1.最初它是打印1,但在某处,它变为零。

I am using java.util.logging.Logger Class for logging in my application. I have added FileHandler so that the application log is stored directly in log.txt file.

But for some reason, after the application is terminated the log is far from complete. On cmd, I can see all the statements but they are never appended to the file.

I have set FileHandler to the Logger by:

private void setLogger() { try { FileHandler hand = new FileHandler("log/log.txt", true); hand.setFormatter(new SimpleFormatter()); Logger log = Logger.getLogger(ImageRename.MAIN_LOG); //log.setUseParentHandlers(false); log.addHandler(hand); log.setLevel(Level.ALL); } catch (IOException e) { System.out.println("Could Not set logger"); } }

Any problem with flushing? How to solve it? Thanks.

PS: On debugging, I have noticed that in between

Logger.getLogger(ImageRename.MAIN_LOG).getHandlers().length

returns 0. Where as it should return 1. Initially it was printing 1, but somewhere down the line it becomes zero.

最满意答案

问题是...... 垃圾收集

发生的事情可能如下:

你调用Logger.getLogger(ImageRename.MAIN_LOG); 您设置了记录器。 Java注意到它是未引用的,并将其丢弃。 你调用Logger.getLogger(ImageRename.MAIN_LOG); 并期望获得相同的记录器。 使用默认配置设置记录器。

你可以通过两个措施避免这种情况:

使用配置文件logging.properties进行配置。 创建记录器时,Java日志记录API将查询配置,从而适当地重新创建它。

使用静态引用。 无论如何,这是最好的做法。 为每个班级配备一个记录器:

private final static Logger LOG = Logger.getLogger(ExampleClass.class.getName());

当类加载时,它不应该是垃圾收集AFAICT。

The problem is ... garbage collection.

What is happening is likely the following:

You call Logger.getLogger(ImageRename.MAIN_LOG); You setup the logger. Java notices it is unreferenced, and discards it. You call Logger.getLogger(ImageRename.MAIN_LOG); and expect to get the same logger. A fresh logger is set up with default configuration.

You can avoid this by two measures:

Use a configuration file logging.properties for configuration. When creating the logger, the Java logging API will consult the configuration, and thus recreate it appropriately.

Use static references. This is a best practise anyway. Equip each class with a logger:

private final static Logger LOG = Logger.getLogger(ExampleClass.class.getName());

While the class is loaded it then should not be garbage collected AFAICT.

更多推荐

本文发布于:2023-07-27 05:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1285447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:记录器   正常运行   Logger   functioning   properly

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!