log4j must be properly configured for logging to files.
try this :
Logger logger = Logger.getLogger(yourclassname.class);
BasicConfigurator.configure(); // basic log4j configuration
Logger.getRootLogger().setLevel(Level.INFO);
FileAppender fileAppender = null;
try {
fileAppender =
new RollingFileAppender(new PatternLayout("%d{dd-MM-yyyy HH:mm:ss} %C %L %-5p:%m%n"),"file.log");
logger.addAppender(fileAppender);
} catch (IOException e) {
e.printStackTrace();
}
logger.info("TEST LOG ENTRY");
This should create a log file named file.log in the local folder.
Use your java program and logic to removeAppender and addAppender as necessary to switch files.
Or you can create multiple logger instances each with one fileAppender if switching is required dynamically throughout the program.
This way of using log4j avoids the need for external configuration file log4j.properties.