2

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.

Gigabeat
  • 21
  • 1
  • 4
  • Windows doesn't support multiple programs writing to the same file this way. Linux allows it but doesn't always work the way you would like. – Peter Lawrey Feb 06 '15 at 13:04

4 Answers4

0

AFAIK this is not possible with log4j. You can either use Logback (http://logback.qos.ch/manual/appenders.html) with prudent mode or try to use another tool for centralized logging (for example http://logstash.net/)

Piotr
  • 116
  • 2
0

I think this is mainly a Windows problem. I have seen this work under Linux.

However, I would not advise this:

  • Concurrent file access is tricky, especially under Windows
  • A log file written from multiple origins is harder to read.
  • There are no guarantees about the order of logged lines
  • Log file rotation will give you headaches (only the process which rotates the log file will write to the new file, the other processes will keep on writing to the old file)

If you want to aggregate data for some sort of report consider logging to a database which offers transactionality and more control over the data.

If you want to monitor for errors from multiple sources you could write a script which greps multiple log files for certain keywords.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • The source is always the same but from different instances. The order of logged lines isn't important, i'm only looking for get an eye on error messages. I can prevent these headaches since that the app is going to create a new folder every day. I know that a database would be the best option, but i can't use one in this scenario. – Gigabeat Feb 06 '15 at 13:15
0

Trying to write to the same file concurrently from different processes is asking for trouble. Instead you could consider using log4j's SocketAppender mechanism, which allows you to send log events from several different processes to a single central "server" process that is responsible for actually writing them to the log file.

This question, and more specifically this answer, provides more details.

Community
  • 1
  • 1
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
0

Simply add all required appenders in the Log4j.xml file

e.g.

 <category name="com.vasx.edm.common.parser.ParserBase" additivity="true">
   <priority value="DEBUG"/>
   <appender-ref ref="ROLLING_APPENDER"/>
   <appender-ref ref="ROLLING_FILE_APPENDER"/>
 </category>

 <category name="com.vasx.edm.dsp.DSParser" additivity="true">
   <priority value="DEBUG"/>
   <appender-ref ref="ROLLING_APPENDER"/>
   <appender-ref ref="ROLLING_FILE_APPENDER"/>
 </category>
Noman K
  • 277
  • 1
  • 5
  • 15