Log4j 禁用#timestamp 注释

Log4j disable #timestamp comments

我有这样的log4j.properties文件:

#Wed Jan 18 12:55:30 EET 2017
log4j.rootLogger=ERROR, stdout, gui, clientFile
log4j.logger.app=DEBUG
...

当我启动我的应用程序时,带有时间戳 (#Wed Jan 18 12:55:30 EET 2017) 的第一行总是在变化。它会导致 Git 提交出现一些问题(我无法将此文件添加到 .gitignore)。

找到添加时间戳的原因:此方法在app中调用linkedProperties.store(fileOutputStream, null);store()方法的实现来自java.util.Properties.

    package java.util;
    ...
    public class Properties extends Hashtable<Object,Object> {
    ...

    public void store(OutputStream out, String comments)
        throws IOException
    {
        store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
               comments,
               true);
    }

    private void store0(BufferedWriter bw, String comments, boolean escUnicode)
            throws IOException
        {
            if (comments != null) {
                writeComments(bw, comments);
             }
            bw.write("#" + new Date().toString());
            bw.newLine();
            synchronized (this) {
                for (Enumeration<?> e = keys(); e.hasMoreElements();) {
                    String key = (String)e.nextElement();
                    String val = (String)get(key);
                    key = saveConvert(key, true, escUnicode);
                    /* No need to escape embedded and trailing spaces for value, hence
                     * pass false to flag.
                     */
                    val = saveConvert(val, false, escUnicode);
                    bw.write(key + "=" + val);
                    bw.newLine();
                }
            }
            bw.flush();
        }
    ...

如何避免这种情况bw.write("#" + new Date().toString());?有没有类似java.util.Properties的东西?

编辑:考虑到 OP 的编辑,这个答案现在几乎是多余的,按照我的建议找到将时间戳添加到文件的内容。但是我会把它留在这里,因为它可能会对某人有所帮助。


首先,实际上不可能指示 Git 忽略文件中的个别行。

我的第一个建议是找到将时间戳添加到文件的内容并停止它。

唯一能在 Git 中帮助您的是从 Git 的工作树中删除文件。

git update-index --skip-worktree <file>

这将指示 Git 不应提交此文件的更改版本,因此不会将其包含在其工作树中,但仍会在存储库中保留跟踪副本。 Look here for official docs

显然,如果您要求开发人员定期 update/commit 此文件,这将不起作用。

我刚刚覆盖了 public void store(OutputStream out, String comments)(删除了 bw.write("#" + new Date().toString()))。有关此问题的更多信息,您可以使用此 link(它完全复制了我的问题):Properties.store() - suppress timestamp comment .