我有一个处理JAVA代码的文件,在处理文件时,我想记录所有文件处理细节,比如文件中没有这些行,它有一些" Hello"正在处理每个文件的单独日志文件中的字符串。
我在这里尝试的代码工作正常,但如果我有2个文件,如file1.txt和file2.txt。记录器在log_file1.log中记录file1.txt详细信息然后记录log_file2.log中的file2.txt它保留log_file1.log内容也在log_file2.log中。
FILE1.TXT
Hello How are you
Hello How are you
log_file1.log
it has 2 lines and 2 Hello Strings
FILE2.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");
}
}
答案 0 :(得分:1)
是否需要使用相同的类别(TestLoggerChild.class)?如果你传递自定义参数化字符串,它必须工作:DummyLog.getLogger(fileName,“Category”+ i);