我正在编写一个以编程方式创建log4j2配置的程序。我的log4j2.xml文件中有一个默认配置。根据列表中的某些数据类型,我必须为每种类型创建/复制log4j配置。该配置用于每日滚动为这些类型复制的appender。创建记录器配置,根据数据类型将日志消息写入不同的文件。但每天的滚动都没有发生。
有人可以帮助解决为什么类型
没有发生滚动这是我的log4j2 xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG" packages="com.ericsson.tests">
<Properties>
<Property name="log-path">C:\Users\ekhaavi\Documents\ClaprProject\logtest\
</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/ipAddr.log"
filePattern="${log-path}/ipAddr-%d{MM-dd-yyyy}.log">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<!-- <SizeBasedTriggeringPolicy size="1 KB" /> -->
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
<RollingFile name="DailyRolling"
fileName="${log-path}/ipaddress/myexample.type.log" filePattern="${log-path}/ipaddress/myexample.type-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="1 KB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug" />
</Logger>
<logger name="com.log.ipaddr.type" additivity="false">
<appender-ref ref="DailyRolling" level="info" />
</logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
以下是基于数据类型生成log4j配置的代码。如果我需要更改为基于尺寸的滚动
,也可以帮助我 package com.ericsson.tests;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
import org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy;
import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
public class Log4j2Running {
static Logger LOGGER;
public static void main(String[] args) {
System.setProperty("log4j.configurationFactory","com.ericsson.tests.MyXMLConfigurationFactory");
LOGGER = LogManager.getLogger(Log4j2Running.class);
LOGGER.debug("Debug Message Logged !!!");
LOGGER.info("Info Message Logged !!!");
LOGGER.error("Error Message Logged !!!", new NullPointerException("NullError"));
/**CHANGES TO LOGGER TO DYNAMICALLY GENERATE THE FILES --**/
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
Appender app = config.getAppender("DailyRolling");
LoggerConfig lgcf = config.getLoggerConfig("com.log.ipaddr.type");
app.stop();
config.removeLogger("com.log.ipaddr.type");
List<String> typeList = new ArrayList<String>();
typeList.add("Data1");
typeList.add("Data2");
typeList.add("Data3");
typeList.add("Data4");
if (app instanceof RollingFileAppender) {
for(String str : typeList){
RollingFileAppender rp = ((RollingFileAppender) app);
String fileName = rp.getFileName().replace("type", str);
fileName = fileName.replace("ipaddress", "10.5.256.12");
String filePattern = rp.getFilePattern().replace("type", str);
filePattern = filePattern.replace("ipaddress", "10.5.256.12");
String name = "10.5.256.12"+"."+str;
/*RollingFileAppender appender = RollingFileAppender.createAppender(rp.getFileName().replace("type", str),
"false", "false", str, "true", "false", "false", "4000", rp.getLayout(), null, "false", null, config);*/
TimeBasedTriggeringPolicy policy = TimeBasedTriggeringPolicy.createPolicy("1", "true");
DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy("10", "0", null,null,null, true, config);
RollingFileAppender appender =RollingFileAppender.createAppender(fileName, filePattern, "true", name, "false", null, "true",rp.getTriggeringPolicy(),
strategy, rp.getLayout(), null, null, null, null, config);
appender.start();
config.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef(appender.getName(), null, null);
AppenderRef[] refs = new AppenderRef[] { ref };
System.out.println(" the level is "+ lgcf.getLevel());
String loggerName = lgcf.getName().replace("type", str);
loggerName = loggerName.replace("ipaddr", "10.24.286.19");
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", lgcf.getLevel(), loggerName,
"true", refs, null, config, null);
loggerConfig.addAppender(appender, null, null);
config.addLogger(loggerName, loggerConfig);
ctx.updateLoggers();
}
}
/**END OF THE CHANGES **/
Logger log = LogManager.getLogger("com.log."+"10.24.286.19"+"."+"Data1");
log.info("Demo1--SHDGSGDHGSHGDSG");
Logger log2 = LogManager.getLogger("com.log."+"10.24.286.19"+"."+"Data2");
log2.info("Demo1--SHDGSGDHGSHGDSG");
Logger log3 = LogManager.getLogger("com.log."+"10.24.286.19"+"."+"Data3");
log3.info("Demo1--SHDGSGDHGSHGDSG");
Logger log4 = LogManager.getLogger("com.log."+"10.24.286.19"+"."+"Data4");
log4.info("Demo1--SHDGSGDHGSHGDSG");
}
}
答案 0 :(得分:0)
问题是我没有为不同类型使用单独的Rolling Manager。通过使用单独的滚动管理器解决了问题
RollingFileManager rollFileManager = RollingFileManager.getFileManager(fileName, filePattern, true,false,policy,rstrategy,null, rp.getLayout(),128,false,false,config); policyupdated.initialize(rollFileManager);