简体   繁体   English

Java Logger与Log4j

[英]Java Logger with Log4j

文件记录器 Am processing files using java , there are 2 or 3 Java components to process files.My requirement is for file must have a log file and its processing details will be logged in relevant log file. 我使用Java处理文件,有2个或3个Java组件来处理文件。我的要求是文件必须有一个日志文件,其处理详细信息将记录在相关的日志文件中。

Problem is for single file it works well but with multiple file am getting problem. 问题是单个文件运行良好,但多个文件却出现问题。 When multiple files are getting processed logger is logging log detail of file1.txt logs to file2.log instead of file1.log... 处理多个文件时,记录器正在将file1.txt日志的日志详细信息记录到file2.log而不是file1.log中。

public Class FileProcessComponent1
{
public void process(String fileName)
{
  Logger Log =  Logg.getLogger1(fileName,this.getClass());
  log.info("file1 logging");
}
}

public Class FileProcessComponent2
{
public void process(String fileName)
{
  Logger Log =  Logg.getLogger1(fileName,this.getClass());
  log.info("file1 logging");
}
}


public Class Logg
{
      public static Logger getLogger1(String fileName,Class clazz) throws Exception
      {
          if(fileName == null || "".equals(fileName.trim()))
              throw new Exception("File Name or Map for File Name is Null");        

            fileName = "/home/logs/"+fileName+".log";
            Logger logger = Logger.getLogger(clazz.getCanonicalName()+":"+System.nanoTime());
            logger.setAdditivity(false);
            FileAppender appender = new DailyRollingFileAppender(new PatternLayout("%d{ISO8601}\t%p\t%c\t%m%n"),    fileName, "'.'yyyy-MM-dd");
            logger.addAppender(appender);
            logger.setLevel(Level.DEBUG);
            return logger; 
      }
}

I think you want to create a logger pointing to a unqiue file for each files comes for processing. 我认为您想为每个要处理的文件创建一个指向unqiue文件的记录器。

Try this, 尝试这个,

a) At the point where you start processing a new file, Create a new Logger with file name as Loggername. a)在开始处理新文件时,请创建一个新的Logger,其文件名为Loggername。 b) Create the Appender with filename.log and assign the appender to the logger. b)使用filename.log创建Appender并将追加器分配给记录器。

Now, when you try to get the logger, always use the filename in getLogger(). 现在,当您尝试获取记录器时,请始终使用getLogger()中的文件名。

Once you are done with processing a file, remove the logger/appender. 处理完文件后,请删除记录器/附加器。 (this is very important) (这个非常重要)

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

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