Is there any way to get two instances of same application logging to the same logfile? Currently I have this code:
JAVA:
log = Logger.getLogger("APP");
Properties properties = new Properties();
properties.load(ClassLoader.getSystemClassLoader()
.getResourceAsStream("config/logger.properties"));
String logpath = properties.getProperty("log4j.appender.APP.File");
Properties log4jProperties = new Properties();
InputStream configStream = ClassLoader.getSystemClassLoader()
.getResourceAsStream("config/logger.properties");
log4jProperties.load(configStream);
configStream.close();
PropertyConfigurator.configure(log4jProperties);
log.error("error");
This configurations - logger.properties
log4j.rootLogger = DEBUG, R
log4j.category.APP=DEBUG, APP
log4j.appender.APP= org.apache.log4j.RollingFileAppender
log4j.appender.APP.File = C:\\Users\\log4j\\Desktop\\b.txt
log4j.appender.APP.MaxFileSize = 2KB
log4j.appender.APP.MaxBackupIndex = 3
log4j.appender.APP.Append = true
log4j.appender.APP.layout = org.apache.log4j.PatternLayout
log4j.appender.APP.layout.ConversionPattern=%-2d{dd/MM/yy HH:mm:ss} %p %t %c - %m%n
And when I launch the second instance of my application I'm getting this following error:
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: C:\Users\log4j\Desktop\c.txt (The process cannot access the file because it is being used by another process)
UPDATE 07/02/2015:
I found my problem. Apparently there is an bug with the RollingFileAppender, so I changed it to FileAppender and now the two instances can access to the file and log their messages.
This is the final configuration:
log4j.rootLogger = DEBUG, R
log4j.category.APP=DEBUG, APP
log4j.appender.APP= org.apache.log4j.RollingFileAppender
log4j.appender.APP.File = C:\\Users\\log4j\\Desktop\\b.txt
log4j.appender.APP.Append = true
log4j.appender.APP.layout = org.apache.log4j.PatternLayout
log4j.appender.APP.layout.ConversionPattern=%-2d{dd/MM/yy HH:mm:ss} %p %t %c - %m%n
I had to remove the MaxFileSize and MaxBackupIndex because they aren't compatible with the FileAppender mode.