Saturday, 28 September 2013

Java Logger with 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.

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...
public Class FileProcessComponent1
{
public void process()
{
Logger Log = Logg.getLogger1("file1",this.getClass());
log.info("file1 logging");
}
}
public Class FileProcessComponent2
{
public void process()
{
Logger Log = Logg.getLogger1("file1",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;
}
}

No comments:

Post a Comment