需要关闭 JUL 的附加 FileHandler 吗?
我正在使用 java utillogging 来记录小型 Java EE 应用程序。为了添加额外的 FileHandler(例如用于错误/警告),我创建了一个 LoggerFactory,它创建实际的记录器并将文件处理程序静态添加到“主”记录器。
package de.il.myapp.logging;
public class LoggerFactory {
private static final java.util.logging.Logger MAIN_LOGGER = java.util.logging.Logger.getLogger("de.il.myapp");
static {
try {
final java.util.logging.FileHandler fh = new java.util.logging.FileHandler("error.log", 1024*1024, 5, true);
fh.setLevel(Level.WARNING);
final java.util.logging.Formatter formatterTxt = new java.util.logging.SimpleFormatter();
fh.setFormatter(formatterTxt);
MAIN_LOGGER.addHandler(fh);
} catch (final IOException e) {
//...
}
}
}
public static final Logger getLogger(final Class<?> clazz){
return java.util.logging.Logger.getLogger(clazz);
}
}
一切工作正常,除了当我停止应用程序时,lck 文件仍然存在。一开始,就会创建一个新的 lck。因此,重新启动后目录如下所示。
error.log.0
error.log.0.1
error.log.0.1.lck
error.log.0.2
error.log.0.2.lck
error.log.0.3
error.log.0.3.lck
error.log.0.lck
问题是:我怎样才能避免这种情况?最后我必须关闭文件处理程序吗?但在哪里呢?由于这是一个 Java EE 应用程序,我不认为这是一个退出点,是吗?为什么我得到的日志文件是 ..log.0.X,而不仅仅是 ..log.0?
谢谢, 因戈
I'm using java util logging for logging in a small Java EE application. To add an additional FileHandler (for example for errors/warnings), I created a LoggerFactory that creates the actual logger and that statically add a filehandler to the "main" logger.
package de.il.myapp.logging;
public class LoggerFactory {
private static final java.util.logging.Logger MAIN_LOGGER = java.util.logging.Logger.getLogger("de.il.myapp");
static {
try {
final java.util.logging.FileHandler fh = new java.util.logging.FileHandler("error.log", 1024*1024, 5, true);
fh.setLevel(Level.WARNING);
final java.util.logging.Formatter formatterTxt = new java.util.logging.SimpleFormatter();
fh.setFormatter(formatterTxt);
MAIN_LOGGER.addHandler(fh);
} catch (final IOException e) {
//...
}
}
}
public static final Logger getLogger(final Class<?> clazz){
return java.util.logging.Logger.getLogger(clazz);
}
}
Everything works fine, except, when I stop the application, the lck file are still there. On a start, a new lck is created. So after some restarts the directory looks like this.
error.log.0
error.log.0.1
error.log.0.1.lck
error.log.0.2
error.log.0.2.lck
error.log.0.3
error.log.0.3.lck
error.log.0.lck
The question is: How can I avoid this? Do I have to close the filehandler at the end? But where? Since this is a Java EE application I don't you an exit point, do I? And why do I get ..log.0.X for the logfiles, not just ..log.0?
Thanks,
Ingo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Glassfish 中,您可以使用生命周期侦听器。在 JBoss 中,您可以使用 StartupServiceMBean。您使用什么应用程序服务器?
In Glassfish, you can use Lifecycle Listeners. In JBoss, you can use
StartupServiceMBean
. What app server are you using?首先,您不允许在 EJB 中使用
java.io
类:规范的第 21.2.2 节:
这意味着您也不应该使用反过来使用
java.io
类的类,除非您通过询问容器来获取它们。但是,如果您创建一个急切加载的单例会话 bean 并在 @PreDestroy 上执行清理:
规范对此不是很清楚,但我认为单例 bean 在所有其他 bean 之后被销毁。
First of all you are not allowed to use
java.io
classes in EJBs:Section 21.2.2 of the spec:
This means that you also should not use classes that in turn use
java.io
classes unless you obtain them by asking the container.However if you create an eagerly loaded singleton session bean and perform the cleanup on
@PreDestroy
:The spec is not very clear about this but I think singleton beans are destroyed after all other beans.