我间歇性地收到以下错误消息,
试图在独立程序中附加到未启动的附加程序“ XYZ”,在该程序中我以编程方式创建记录器对象。
我正在使用
org.apache.logging.log4j.Logger 并在通过使用之前明确启动附加程序 appender.start();
任何帮助表示赞赏。
示例代码:
//记录器类
公共类LoggerDemo {
private Logger myLogger;
public LoggerDemo(String fileName){
LoggerContext context= (LoggerContext) LogManager.getContext(false);
if (!context.hasLogger(fileName)) {
Configuration config= context.getConfiguration();
Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, config,
null, null, false, false, null, null);
Appender appender = FileAppender.createAppender(fileName, "false", "false", "myLoggerFile", "true",
"false", "false", "40000", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
AppenderRef[] refs = new AppenderRef[] {ref};
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, fileName, "true", refs, null, config, null);
loggerConfig.addAppender(appender, null, null);
config.addLogger(fileName, loggerConfig);
context.updateLoggers();
}
processLogger = context.getLogger(fileName);
}
}
主类
public Class Test{
main(){
LoggerDemo logger = new LoggerDemo("abc.log");
//logger.info();
//and so on..
}
}
谢谢