如何输出当前java.util.logging.Logger日志文件名的名称? [英] How can I output the name of the current java.util.logging.Logger log file name?

查看:143
本文介绍了如何输出当前java.util.logging.Logger日志文件名的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在多个JVM中运行但可以登录到同一%t(临时)目录的工具.我正在logging.properties文件中使用%u(唯一)模式变量,以便每个实例都记录到不同的日志文件中.

I have a tool that is ran in multiple JVMs but logs to the same %t (temp) directory. I am using the %u (unique) pattern variable in the logging.properties file so that each instance logs to a different log file.

如果该进程识别出故障(这是一个监视工具),它将发送电子邮件,并且我希望附加遇到该故障的特定实例的日志文件.但是,如何获取日志文件路径?如果这不是一个好方法,也很高兴接受这是一个更好的方法.

If the process identifies a failure (this is a monitoring tool) it sends an email and I want to attach the log file of the particular instance that encountered the failure. But how do I get the log file path? Also gladly accepted would be a better approach if this is not a good one.

推荐答案

Oracle已在涵盖此内容的java.util.logging.FileHandler 所需的JDK-4798814 getFiles().

There is an RFE filed with Oracle under JDK-4798814 getFiles() needed for java.util.logging.FileHandler that covers this.

如果不使用安全管理器,可以求助于反思.

You can resort to reflection if you are not using a security manager.

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源代码,它也可能会中断.

Keep in mind that this solution might race against file rotation. It also might break if the FileHandler source code is changed.

如果不需要轮换,则可以始终扩展 StreamHandler ,并提供一个已知的文件位置.

If you don't need rotation then you can always extend the StreamHandler and provide a known file location.

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连接,则

If the monitoring tool supports incoming TCP connections then the java.util.logging.SocketHandler would be a way to send all of the log information to the monitoring tool and then you could have the monitoring tool decide where to store or send the log data.

这篇关于如何输出当前java.util.logging.Logger日志文件名的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆