简体   繁体   English

Log4j禁用#时间戳注释

[英]Log4j disable #timestamp comments

I have such log4j.properties file: 我有这样的log4j.properties文件:

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

And when I start my application first line with timestamp ( #Wed Jan 18 12:55:30 EET 2017 ) is always changing. 当我启动应用程序时,带有时间戳的第一行( #Wed Jan 18 12:55:30 EET 2017 )总是在变化。 It causes some problems with Git commits (I can not add this file to .gitignore). 这会导致Git提交出现一些问题(我无法将此文件添加到.gitignore)。

Found what is adding the timestamp: this method is calling in app linkedProperties.store(fileOutputStream, null); 找到添加时间戳的内容:此方法正在应用程序中调用linkedProperties.store(fileOutputStream, null); The implementation of store() method is from java.util.Properties. 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();
        }
    ...

How can I avoid this bw.write("#" + new Date().toString()); 我如何避免这种bw.write("#" + new Date().toString()); ? Is there something similar to java.util.Properties? 是否有类似于java.util.Properties的内容?

Edit : This answer is now laregely redundant given the OP's edits, following my suggestion to find what was adding the timestamp to the file. 编辑 :根据我的建议来查找在文件中添加时间戳的内容,鉴于OP的编辑,此答案现在几乎是多余的。 However I'll keep it here as it may help someone, perhaps. 但是,我将其保留在这里,因为它可能会对某人有所帮助。


Firstly, it's not really possible to instruct Git to ignore individual lines in a file. 首先,实际上不可能指示Git忽略文件中的每一行。

My first recommendation would be to find what is adding the timestamp to the file and stop it. 我的第一个建议是找到将时间戳添加到文件中并将其停止的原因。

The only thing that comes to mind that could help you in Git specifically is removing the file from Gits working tree. 想到的唯一可以帮助您在Git中进行帮助的事情就是从Gits工作树中删除文件。

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

This will instruct Git that a changed version of this file shouldn't be committed and so will not include it in its working tree, but will still keep the tracked copy in the repository. 这将指示Git不应提交此文件的更改版本,因此不会将其包含在其工作树中,但仍会将跟踪的副本保留在存储库中。 Look here for official docs 在这里查看官方文档

Obivously, this won't work if you require developers to regularly update/commit this file. 显然,如果您要求开发人员定期更新/提交此文件,则此方法将无效。

I have just overrided public void store(OutputStream out, String comments) (removed bw.write("#" + new Date().toString()) ). 我刚刚重写了公共无效存储(OutputStream输出,字符串注释)(删除了bw.write("#" + new Date().toString()) )。 For more information about this problem you can use this link (it fully dublicates my issue): Properties.store() - suppress timestamp comment . 有关此问题的更多信息,您可以使用此链接(它完全复制了我的问题): Properties.store()-抑制时间戳记注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM