如何输出当前java.util.logging.Logger日志文件名?
我有一个在多个JVM中运行的工具,但它登录到同一个如何输出当前java.util.logging.Logger日志文件名?,java,logging,java.util.logging,Java,Logging,Java.util.logging,我有一个在多个JVM中运行的工具,但它登录到同一个%t(temp)目录。我在logging.properties文件中使用%u(唯一)模式变量,以便每个实例都记录到不同的日志文件中 如果流程识别出一个故障(这是一个监控工具),它会发送一封电子邮件,我想附加遇到故障的特定实例的日志文件。但是如何获取日志文件路径?如果这不是一个好的方法,我们也很乐意接受这一方法。有一份向Oracle提交的RFE,其中涵盖了这一点 如果不使用安全管理器,则可以求助于反射 public class GetFileHan
%t
(temp)目录。我在logging.properties
文件中使用%u
(唯一)模式变量,以便每个实例都记录到不同的日志文件中
如果流程识别出一个故障(这是一个监控工具),它会发送一封电子邮件,我想附加遇到故障的特定实例的日志文件。但是如何获取日志文件路径?如果这不是一个好的方法,我们也很乐意接受这一方法。有一份向Oracle提交的RFE,其中涵盖了这一点 如果不使用安全管理器,则可以求助于反射
public class GetFileHandler extends FileHandler {
public GetFileHandler() throws IOException {
super();
}
/**
* Gets the files used by this handler. Index zero is the file that is
* in use.
*
* @return a array of files.
* @throws IOException if there is an error.
* @throws SecurityException if not allowed.
*/
public File[] getFiles() throws IOException {
return GetFileHandler.getFiles(this);
}
/**
* Gets the files used by this handler. Index zero is the file that is
* in use.
*
* @param h any non null FileHandler.
* @return a array of files.
* @throws NullPointerException if the given FileHandler is null.
* @throws IOException if there is an error.
* @throws SecurityException if not allowed.
*/
public static File[] getFiles(FileHandler h) throws IOException {
try {
Field f = FileHandler.class.getDeclaredField("files");
f.setAccessible(true);
synchronized (h) {
return ((File[]) f.get(h)).clone();
}
} catch (ReflectiveOperationException roe) {
throw new IOException(roe);
}
}
}
请记住,此解决方案可能会与文件旋转竞争。如果FileHandler源代码发生更改,它也可能会中断
如果不需要旋转,则始终可以扩展并提供已知的文件位置
public class KnownFileHandler extends StreamHandler {
private final File file;
public KnownFileHandler() throws IOException {
String v = LogManager.getLogManager().getProperty(getClass().getName() +".name");
if(v == null) {
v = "knownfilehandler.log";
}
file = new File(v);
this.setOutputStream(new FileOutputStream(file));
}
public File getFile() {
return this.file;
}
}
如果监视工具支持传入的TCP连接,那么将是一种将所有日志信息发送到监视工具的方法,然后您可以让监视工具决定在何处存储或发送日志数据。如果每个程序都记录到不同的日志文件夹或使用不同的日志文件,不是会更好地组织吗姓名/前缀?谢谢你的评论。这对我来说并不重要,因为一旦通过电子邮件发送日志文件,它们就是“一次性的”。没有人会浏览日志文件。在任何情况下,看起来我都需要通过编程而不是简单地通过日志配置来完成这项工作,因此最终可能会对它们进行组织。非常有用,感谢您提供的众多建议!