0
log4j2 (log4j2.xml을 사용하여 매개 변수를 초기화)를 사용하는 응용 프로그램에서 작업하고 로거를로드합니다. 응용 프로그램이 실행될 때 로그 수준 변경 및 파일 첨부 관리자 속성과 같은 몇 가지 매개 변수를 프로그래밍 방식으로 재정의해야합니다.log4j2 변경 파일 애플릿 최대 파일을 프로그래밍 방식으로 프로그래밍
/**
* @param logger Logger instance
* @param level New log level to be applied on the logger instance
*/
private void setLoggerLevel(Logger logger, Level level) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
LoggerConfig loggerConfig = getLoggerConfig(logger.getName());
Logger rootLogger = LogManager.getRootLogger();
if(logger != rootLogger && loggerConfig != config.getLoggerConfig(rootLogger.getName()))
{
loggerConfig.setLevel(level);
context.updateLoggers();
}
}
이제는 롤링 파일 어 펜더의 속성을 사용하여 동일한 문제를 해결하려고했습니다. 그러나, 나는 기존 RollingFile의 펜더의 재산
Logger applicationLogger = LogManager.getLogger(logName);
if (applicationAppender instanceof RollingFileAppender) {
// Configure max properties of rolling file appender here
if (maxLogFiles != null) {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
// Copied code from lib
DefaultRolloverStrategy newStrategy = DefaultRolloverStrategy.createStrategy(String.valueOf(maxLogFiles), null, null,
String.valueOf(Deflater.DEFAULT_COMPRESSION), null, true, config);
// How to modify the existing appender?
}
}
프로그래밍 롤링 파일 어 펜더 수정을 진행 할 수있는 방법은 무엇입니까 수정하는 방법에 붙어 있어요?
은 아마의 중복 : [Log4j2 RollingFile이 appender - 각 로그 파일의 시작 부분에서 사용자 정보를 추가] (http://stackoverflow.com/questions/20819376/log4j2-rollingfile-appender-add-custom-info-at-the-start-of-each-logfile) –
링크 된 질문은 기본적으로 log4j2에 대한 후크 추가를 제안합니다. appender를 수정하기 위해 전체 기능을 재정의 할 필요는 없습니다. appender를 제거하고 기존 appender를 다시 추가 할 수 있습니까? – dmachop