简体   繁体   中英

Log4j disable #timestamp comments

I have such log4j.properties file:

#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. It causes some problems with Git commits (I can not add this file to .gitignore).

Found what is adding the timestamp: this method is calling in app linkedProperties.store(fileOutputStream, null); The implementation of store() method is from 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()); ? Is there something similar to 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. 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.

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 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. 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()) ). For more information about this problem you can use this link (it fully dublicates my issue): Properties.store() - suppress timestamp comment .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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