简体   繁体   中英

Configuring multiple applications using Log4j to write simultaneously in same log file

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.

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/ )

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.

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.

Simply add all required appenders in the Log4j.xml file

eg

 <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM