To create multiple log files with log4j based on folder name

3.3k views Asked by At

I am having multiple folders with each folder having few xml's.

While unmarshalling XMls in for loop at the starting of the loop i am setting folder variable based on the folder xml's i am unmarshalling . But i am not able to change the value of folder varialbe in the end.

Log4j file :

log4j.rootLogger=DEBUG, theFileAppender log4j.category.org.exolab.castor.xml=ERROR, theFileAppender log4j.category.org.castor.core.util=ERROR, theFileAppender

log4j.appender.theFileAppender=org.apache.log4j.FileAppender log4j.appender.theFileAppender.File = ${folder}/error.log

log4j.appender.theFileAppender.Append=false

log4j.appender.file.MaxFileSize=10MB

log4j.appender.file.MaxBackupIndex=1 log4j.appender.theFileAppender.layout=org.apache.log4j.PatternLayout log4j.appender.theFileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c %x - %m%n

Starting of the loop i am setting the folder :

System.setProperty("folder", "E:/Logging/folderName");

At the end of loop : i am setting the folder by :

Properties properties = new Properties(System.getProperties()); properties.setProperty("folder","E:/Logging/folderName");

folderName is my folder name.

Its creating the required folder but its appending to the error log file created in the folder which is created at the starting of loop.

Please help me how to set the variable .

2

There are 2 answers

2
Oscar Jara On BEST ANSWER

You can change the folder by invoking log4j API like this:

Getting your root logger and then modifying your appender by name (in this case, as your question says: "theFileAppender").

String logFolderName = "newfoldername";
String logFilePath = "E:/Logging/" + logFolderName + "/error.log";

Logger logger = Logger.getRootLogger();
FileAppender appender = (FileAppender)logger.getAppender("theFileAppender");
appender.setFile(logFilePath);
appender.activateOptions();

As you can see, you just need to modify your logFolderName variable according to your needs for the new log file to be created.

0
Paul Vargas On

You can do that in two ways. The easiest is to add the variable they need in the VM arguments. e.g. adding the name of your directory:

-Dfolder=E:/Logging/folderName

The other way, if you can not add the argument, is to add programmatically. e.g.:

public class ChangeFileTest {

    private static Logger LOG = Logger.getLogger(ChangeFileTest.class);

    static {
        // Get the appender
        Appender appender = LogManager.getRootLogger()
                .getAppender("theFileAppender");
        if (appender instanceof FileAppender) {

            // The new filename
            String filename = "C:\\tmp\\newstout.log";

            // Change the file
            ((FileAppender) appender).setFile(filename);
            ((FileAppender) appender).setAppend(true);

            // Use the new file
            ((FileAppender) appender).activateOptions();
        }
    }

    public static void main(String[] args) {
        LOG.info("Everything is OK!");
    }

}

You can see the whole process if you add to the arguments of the virtual machine:

-Dlog4j.debug