Log4j2 动态生成Appender或者动态修改Appender的各种配置

日志实战 小海豚博客管理员 2020-04-20 23:01:41.0 343 2条

有时要在代码执行的过程中在进行对log4j2日志使用哪个Appender输出或者需要动态修改一些状态,比如输出等级,添加过滤器等,那我们可以用动态生成/添加Appender

项目的Log4j2依赖

  1. <dependency>
  2. <groupId>org.apache.logging.log4j</groupId>
  3. <artifactId>log4j-core</artifactId>
  4. <version>2.11.1</version>
  5. </dependency>

动态生成Appender

  1. public static void createDomainAppender(final String domainId){
  2. final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  3. final org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
  4. if (config.getAppender(domainId + "DomainCntCoreLog") != null) {
  5. return;
  6. }
  7. final PatternLayout layout = PatternLayout.newBuilder()
  8. .withCharset(Charset.forName("UTF-8"))
  9. .withConfiguration(config)
  10. .withPattern("%d %t %p %X{TracingMsg} %c - %m%n")
  11. .build();
  12. final TriggeringPolicy policy = TimeBasedTriggeringPolicy.newBuilder()
  13. .withModulate(true)
  14. .withInterval(1)
  15. .build();
  16. final Appender appender = RollingFileAppender.newBuilder()
  17. .withName(domainId + "DomainCntCoreLog")
  18. .withImmediateFlush(true)
  19. .withFileName("logs/" + domainId + "/CNTCore.log")
  20. .withFilePattern("logs/" + domainId + "/CNTCore.log.%d{yyyy-MM-dd-a}.gz")
  21. .withLayout(layout)
  22. .withPolicy(policy)
  23. .build();
  24. appender.start();
  25. config.addAppender(appender);
  26. // final KeyValuePair[] pairs = {KeyValuePair.newBuilder().setKey("domainId").setValue(domainId).build()};
  27. // final Filter filter = ThreadContextMapFilter.createFilter(pairs, null, Result.ACCEPT, Result.DENY);
  28. //config.getLoggerConfig("com.nonelonely").addAppender(appender, Level.DEBUG, filter);
  29. config.getLoggerConfig("com.nonelonely").addAppender(appender, Level.DEBUG, null);
  30. ctx.updateLoggers(config);
  31. }

这段代码动态生成一个名为omainCntCoreLog的RollingFileAppender,该appender交由com.nonelonely这个logger来使用,并将日志信息输入到logs/{domainId}/CNTCore.log,当然你可以添加一些过滤器或者选择不加null。

注意该com.nonelonely是配置文件中Logger的名字 如下配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration monitorInterval="30">
  3. <Appenders>
  4. <Console name="stdout" target="SYSTEM_OUT">
  5. <PatternLayout pattern="%-5p %m%n" />
  6. <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
  7. </Console>
  8. <RollingFile name="cntCorelog" immediateFlush="true" fileName="logs/CNTCore.log" filePattern="logs/CNTCore.log.%d{yyyy-MM-dd-a}.gz"
  9. append="true">
  10. <PatternLayout>
  11. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS}:%p %t %X{TracingMsg} %c - %m%n</pattern>
  12. </PatternLayout>
  13. <Policies>
  14. <TimeBasedTriggeringPolicy modulate="true" interval="1" />
  15. </Policies>
  16. </RollingFile>
  17. </Appenders>
  18. <Loggers>
  19. <Logger name="com.nonelonely" level="debug" additivity="true">
  20. <AppenderRef ref="cntCorelog" />
  21. </Logger>
  22. <Root level="error">
  23. <AppenderRef ref="stdout" />
  24. </Root>
  25. </Loggers>
  26. </configuration>
暗锚,解决锚点偏移

文章评论

嘿,来试试登录吧!