log4j配置文件错误检测

发布于 2024-09-27 11:41:01 字数 134 浏览 1 评论 0原文

我目前正在使用 log4j 编写一个记录器。一旦我加载了 log4j.properties 或 log4j.xml 文件,我想知道是否有一种方法可以检测记录器配置文件是否有效。如果它无效,我希望改为加载默认设置(位于另一个文件中)。

谢谢

I'm currently writing a logger utilizing log4j. Once I load in a log4j.properties or a log4j.xml file, I was wondering if there was a way to detect if the logger configuration file is valid. If it isn't valid, I was hoping to load a default setting (that's located in another file) instead.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-10-04 11:41:01

我们通过在加载配置之前重定向 System.err 并检查错误是否记录到流中来解决这个问题:

class ConfigurationLoader {
    class Log4jConfigStderrStream extends ByteArrayOutputStream {
        private int lineCount;

        private StringBuilder output;

        private PrintStream underlying;

        public Log4jConfigStderrStream(PrintStream underlying) {
            this.lineCount = 0;
            this.output = new StringBuilder("");
            this.underlying = underlying;
        }

        @Override
        public void flush() throws IOException {
            String[] buffer;
            synchronized (this) {
                buffer = this.toString().split("\n");
                super.flush();
                super.reset();
                for (int i = 0; i < buffer.length; i++) {
                    String line = buffer[i].replace("\n", "").replace("\r",
                            "");
                    if (line.length() > 0) {
                        this.lineCount++;
                        this.output.append(line);
                        this.output.append("\n");
                    }
                }
            }
        }

        public String getOutput() {
            return this.output.toString();
        }

        public PrintStream getUnderlying() {
            return this.underlying;
        }

        public boolean hasErrors() {
            return this.lineCount == 0 ? false : true;
        }
    }

    private String output;

    public void flushOutput() {
        if (!"".equals(this.output))
            System.err.print(this.output);
    }

    public boolean loadConfiguration(String filename) {
        Log4jConfigStderrStream confErr;
        confErr = new Log4jConfigStderrStream(System.err);
        System.setErr(new PrintStream(confErr, true));
        LogManager.resetConfiguration();
        DOMConfigurator.configure(filename);
        System.setErr(confErr.getUnderlying());
        this.output = confErr.getOutput();
        return !confErr.hasErrors();
    }
}

We solved this problem by redirecting System.err before loading the configuration and checking if errors were logged to the stream:

class ConfigurationLoader {
    class Log4jConfigStderrStream extends ByteArrayOutputStream {
        private int lineCount;

        private StringBuilder output;

        private PrintStream underlying;

        public Log4jConfigStderrStream(PrintStream underlying) {
            this.lineCount = 0;
            this.output = new StringBuilder("");
            this.underlying = underlying;
        }

        @Override
        public void flush() throws IOException {
            String[] buffer;
            synchronized (this) {
                buffer = this.toString().split("\n");
                super.flush();
                super.reset();
                for (int i = 0; i < buffer.length; i++) {
                    String line = buffer[i].replace("\n", "").replace("\r",
                            "");
                    if (line.length() > 0) {
                        this.lineCount++;
                        this.output.append(line);
                        this.output.append("\n");
                    }
                }
            }
        }

        public String getOutput() {
            return this.output.toString();
        }

        public PrintStream getUnderlying() {
            return this.underlying;
        }

        public boolean hasErrors() {
            return this.lineCount == 0 ? false : true;
        }
    }

    private String output;

    public void flushOutput() {
        if (!"".equals(this.output))
            System.err.print(this.output);
    }

    public boolean loadConfiguration(String filename) {
        Log4jConfigStderrStream confErr;
        confErr = new Log4jConfigStderrStream(System.err);
        System.setErr(new PrintStream(confErr, true));
        LogManager.resetConfiguration();
        DOMConfigurator.configure(filename);
        System.setErr(confErr.getUnderlying());
        this.output = confErr.getOutput();
        return !confErr.hasErrors();
    }
}
七月上 2024-10-04 11:41:01

您只能尝试手动检查记录器是否存在。

http ://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/LogManager.java?view=markup

就像:LogManager.exists(“你的记录器名称”);

此外,您还可以通过根据 DTD 验证您的 xml 是否有效。

但是“有效”日志文件的定义是什么?如果仅基于语法,请使用 DTD 验证并检查属性文件的格式是否正确。

如果文件有效并且具有特定星座,请使用上面的手动方法。

希望有帮助,
基督教

You can only try to check manually for a logger being existent or not.

http://svn.apache.org/viewvc/logging/log4j/trunk/src/main/java/org/apache/log4j/LogManager.java?view=markup

Like: LogManager.exists("your logger name");

Additionally you can check if your xml is valid or not with validating it against the DTD.

But what is the definition for a "valid" log file? If its only syntax based use DTD validation and check if the property file is in a good format.

If a file is valid if it has a specific constellation use the manual approach above.

Hope that helps,
Christian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文