我有一个处理 JAVA 代码的文件,在处理文件时,我想记录所有文件处理详细信息,例如文件有这个行数,并且它在每个正在处理的文件的单独日志文件中都有一些“Hello”字符串。
我在这里尝试的代码工作正常,但如果我有 2 个文件,如 file1.txt 和 file2.txt。记录器在 log_file1.log 中记录 file1.txt 详细信息,那么在 log_file2.log 中记录 file2.txt 时,它会保留 log_file1。日志内容也在 log_file2.log 中。
文件1.txt
Hello How are you
Hello How are you
log_file1.log
it has 2 lines and 2 Hello Strings
文件2.txt
Hello How are you
Hello How are you
Hello How are you
log_file2.log
it has 2 lines and 2 Hello Strings
it has 3 lines and 3 Hello Strings
我试过的代码:
public class DummyLog
{
private static PatternLayout patternLayout = new PatternLayout("%d{ISO8601}\t%p\t%c\t%m%n");
public static Logger getLogger(String fileName, Class clazz) throws Exception
{
fileName = "D:/New folder/log-properties/" + fileName + ".log";
Logger logger = Logger.getLogger(clazz);
FileAppender appender = new DailyRollingFileAppender(patternLayout,
fileName, "'.'yyyy-MM-dd");
logger.addAppender(appender);
logger.setLevel(Level.DEBUG);
return logger;
}
}
主要课程:
public class TestDummyLog
{
public static void main(String args[]) throws Exception
{
for(int i = 2;i > 0;i--)
{
String loggerName = "Log1_"+new SimpleDateFormat("yyyyddMMHHmmssSSS").format(new Date());
Logger logger = DummyLog.getLogger(loggerName, TestDummyLog.class);
logger.info("Joseph Micheal TestLogger:" +new SimpleDateFormat("yyyyddMMHHmmssSSS").format(new Date()));
new TestLoggerChild().getChileStuff(loggerName);
Thread.sleep(6000);
}
}
}
儿童班:
public class TestLoggerChild
{
public static Logger logger = null;
public void getChileStuff(String fileName) throws Exception
{
logger = DummyLog.getLogger(fileName,TestLoggerChild.class);
logger.info("Child Sample info message"+new SimpleDateFormat("yyyyddMMHHmmssSSS").format(new Date()));
logger.info("Class :::::: TestLoggerChild.class");
logger.debug("fileName @@@@@@@@@@@@@:" + fileName);
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.info("Sample info message ");
logger.info("Class :::::: TestLoggerChild.class");
}
}