问题
i am using spring batch and i need to create different log files one log for job, due to i acces to external properties file, i need to create configuration logs in java code. My canfiguration is
ConsoleAppender console = new ConsoleAppender();
console.setLayout(new PatternLayout(stdoutConversionPattern));
console.setThreshold(Level.INFO);
console.activateOptions();
Logger.getRootLogger().addAppender(console);
// FICHERO: FileAppender1.LOG CONTENDRA LA INFROMACION DE LA EJECUCION DEL FileAppender1
RollingFileAppender FileAppender1Appender = new RollingFileAppender();
FileAppender1Appender.setName("FileAppender1Logger");
FileAppender1Appender.setFile(FileAppender1File);
FileAppender1Appender.setLayout(new PatternLayout(FileAppender1ConversionPattern));
FileAppender1Appender.setAppend(true);
FileAppender1Appender.setMaxBackupIndex(FileAppender1MaxBackup);
FileAppender1Appender.setMaxFileSize(FileAppender1MaxFileSize);
FileAppender1Appender.activateOptions();
Logger.getRootLogger().addAppender(FileAppender1Appender);
// FICHERO: FileAppender2.LOG CONTENDRA LA INFROMACION DE LA EJECUCION DEL FileAppender2
RollingFileAppender FileAppender2Appender = new RollingFileAppender();
FileAppender2Appender.setName("FileAppender2Logger");
FileAppender2Appender.setFile(FileAppender2File);
FileAppender2Appender.setLayout(new PatternLayout(FileAppender2ConversionPattern));
FileAppender2Appender.setAppend(true);
FileAppender2Appender.setMaxBackupIndex(FileAppender2MaxBackup);
FileAppender2Appender.setMaxFileSize(FileAppender2MaxFileSize);
FileAppender2Appender.activateOptions();
Logger.getRootLogger().addAppender(FileAppender2Appender);
Logger.getRootLogger().setAdditivity(false);
properties.put("logging.file", FileAppender2File);
And i write :
static final Logger FileAppender1Log = LogManager.getLogger("FileAppender1Logger");
static final Logger FileAppender1Log = LogManager.getLogger("FileAppender2Logger");
But Always the logger write en all logs files when i want have differents logger one log for job Sorry for my english, not is my native languaje and thanks
回答1:
You are adding both appender to Root configuration, thats why it is not able to differentiate different logger. You should add appender to Logger which you are interested like below
Logger.getLogger("FileAppender1Logger").addAppender(FileAppender1Appender);
Logger.getLogger("FileAppender2Logger").addAppender(FileAppender2Appender);
来源:https://stackoverflow.com/questions/62174758/how-create-diferents-log-file-with-different-content-using-log4-in-java-configur