java - Renaming a Log4J log file during the program run -


We have recently switched from July (java.util.Logging) to Log4J because I added extra log files Wanted Different Logging Levels

At the end of the execution of the program (optionally for all purposes and purposes) we have option in the program to attach a value and date / time stamp to the log file name. / P>

Because JUL wanted to open and close the file needed to write the file, it was not locked and we can just use .renameTo () to change the file name.

Now, using Log4J, that file is opened and locked, preventing us from rename the file (names).

Before configuring logging, I can not determine the name of the file because the option for naming the property file is after some time of logging required (that is why we named it at the end of the program).

Do you have any suggestions on how to get it?

What logback and / or SFL 4J help or interrupt?


I have worked around the problem by using the software parameter in the log4j property file, setting the property and then reloading the property file.

This allows me to rename the log file to something else at the end of the run, and then rename the old files.

It is unmanageable, and is very lonely, so I would like to avoid it because it also temporarily leaves files around after run.

An accurate viewpoint must implement your own log4jappender, probably filepadder ()

I have not requested to do this yet, but what I am doing, I have to use the built-in API setFile (...):

For example: < Pre> public class RenamingFileAppender extends FileAppender {... / ** Fix concurrency problem in implementing stock ** / Public Synchronized Zero Setfa Target (string file) {super.setFile (file); } Public synchronized bidding renaming file (String Numan) {// The whole method is synchronized to avoid losing the log message / / The implementation can be clever in the short-term queue / short-term queue / any incoming incoming file File is being renamed to FileFile = new File (this.fileName); New file file = new file (new name); // Check to make sure existing files, etc can be renamed ... // change the name of existing log file to tfph file = file.crackcat.txt file ("rename-appenders", ".log") Create a temporary file to use; TempFile.deleteOnExit (); // To use the temporary file to tell the built-in underlying, the current file is flushed and the super .setfile (tempFile.getAbsolutePath (), false, this.bufferedIO, this.bufferSize) stops; // Rename File Recently CurrentFile.renameTo (newFile); // Now go back to the original log content under the new name Note Epand = True Super .setfile (newfile.getAbsolutePath (), true, this.bufferreiro, this.bufferSize); }

Comments