简体   繁体   English

使用Log4j配置多个应用程序以同时写入同一日志文件

[英]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: 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 此配置-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: 2015年7月2日更新:
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. 显然RollingFileAppender存在一个错误,因此我将其更改为FileAppender,现在这两个实例可以访问该文件并记录其消息。

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. 我必须删除MaxFileSize和MaxBackupIndex,因为它们与FileAppender模式不兼容。

AFAIK this is not possible with log4j. AFAIK这对于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/ ) 您可以在审慎模式下使用Logback( http://logback.qos.ch/manual/appenders.html ),也可以尝试使用其他工具进行集中式日志记录(例如http://logstash.net/

I think this is mainly a Windows problem. 我认为这主要是Windows问题。 I have seen this work under Linux. 我已经在Linux下看到了这项工作。

However, I would not advise this: 但是,我不建议这样做:

  • Concurrent file access is tricky, especially under Windows 并发文件访问非常棘手,尤其是在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. 相反,您可以考虑使用log4j的SocketAppender机制,该机制允许您将日志事件从几个不同的进程发送到单个中央“服务器”进程,该进程负责将它们实际写入日志文件。

This question , and more specifically this answer , provides more details. 这个问题 ,更具体地说是这个答案 ,提供了更多细节。

Simply add all required appenders in the Log4j.xml file 只需在Log4j.xml文件中添加所有必需的附加程序

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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