实例
比如在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配置就是
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" ignoreExceptions="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] (%F:%L) - %m%n" />
</Console>
<MyLog4j2Appender name="MyLog4j2Appender" >
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] (%F:%L) - %m%n" />
</MyLog4j2Appender>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="MyLog4j2Appender" level="error" />
</Root>
</Loggers>
</Configuration>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。