实例
比如在root logger下新增一个自己的appender ref
public static void load(String appId,String serverAddress,int port){ final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration(); PatternLayout layout= PatternLayout.createLayout("%d{yyyy-MM-dd HH:mm:ss} [%t] (%F:%L) - %m%n", null, config,null, Charset.forName("UTF-8"),true,false,null,null); Appender appender= MyLog4j2Appender.createAppender("MyLog4j2Appender",layout, null, null); appender.start(); config.addAppender(appender); config.getLoggerConfig("ROOT").addAppender(appender, Level.ERROR,null); ctx.updateLoggers(config); }
其实对应的xml配置就是