如何在log4j2中以编程方式配置SmtpAppender

2022-04-12 00:00:00 java log4j2

以下是我正在编写的代码:

目的是以编程方式配置SmtpAppender。除了SmtpAppender,我还需要以编程方式添加RollingFileAppender和控制台附加器。

package vish;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.appender.SmtpAppender;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;

public class SmtpAppenderBuilder {

  public static void main(String[] args) {

    String pattern = "%d{MM-dd@HH\:mm\:ss}%-4r %-5p [%t] %37c %3x - %m%n";
    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
    LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern", pattern);

    RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG);
    builder.setStatusLevel(Level.DEBUG);
    org.apache.logging.log4j.core.appender.SmtpAppender.Builder smtpBuilder = SmtpAppender.newBuilder();
    smtpBuilder.setName("emailAppender");
    smtpBuilder.setSmtpUsername("test1@gmail.com");
    smtpBuilder.setSmtpPassword("###YpSv1925");
    smtpBuilder.setSmtpProtocol("https");
    smtpBuilder.setSmtpHost("smtp.gmail.com");
    smtpBuilder.setBufferSize(512);
    smtpBuilder.setTo("test2@gmail.com");
    smtpBuilder.setSubject("testing");
  }
}

如何将smtpAppender添加到配置或rootLogger?


解决方案

您混淆了两个API:

  • ConfigurationBuilder API,这是与配置文件最接近的代码等价物。它只创建实际日志组件的定义,真正的定义是在配置对象上调用Configuration#initialize()时创建的。您可以创建SMTPAppender的定义:

    private Configuration createConfig() {
       ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder()//
                                                                                     .setStatusLevel(Level.DEBUG);
       LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")//
                                                     .addAttribute("pattern", "%d{MM-dd@HH\:mm\:ss}%-4r %-5p [%t] %37c %3x - %m%n");
       AppenderComponentBuilder appenderBuilder = builder.newAppender("emailAppender", "SMTP")//
                                                         .addAttribute("smtpUsername", "test1@gmail.com")
                                                         .addAttribute("smtpPassword", "###YpSv1925")
                                                         .addAttribute("smtpProtocol", "smtps")
                                                         .addAttribute("smtpHost", "smtp.gmail.com")
                                                         .addAttribute("to", "test2@gmail.com")
                                                         .addAttribute("subject", "testing")
                                                         .add(layoutBuilder);
       AppenderRefComponentBuilder appenderRefBuilder = builder.newAppenderRef("emailAppender");
       RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(Level.DEBUG)//
                                                             .add(appenderRefBuilder);
       return builder.add(appenderBuilder)//
                     .add(rootLoggerBuilder)
                     .build();
    }
    
  • Log4j 2.x组件的实际构建器,由Configuration#initialize使用上面的定义通过反射调用。您也可以直接使用它们:

    private static Configuration createConfig2() {
       return new AbstractConfiguration(null, ConfigurationSource.NULL_SOURCE) {
    
          @Override
          protected void doConfigure() {
             Layout<String> layout = PatternLayout.newBuilder()//
                                                  .withPattern("%d{MM-dd@HH\:mm\:ss}%-4r %-5p [%t] %37c %3x - %m%n")
                                                  .withConfiguration(this)
                                                  .build();
             Appender appender = SmtpAppender.newBuilder()//
                                             .setName("emailAppender")
                                             .setSmtpUsername("test1@gmail.com")
                                             .setSmtpPassword("###YpSv1925")
                                             .setSmtpProtocol("smtps")
                                             .setTo("test2@gmail.com")
                                             .setSubject("testing")
                                             .setLayout(layout)
                                             .setConfiguration(this)
                                             .build();
             LoggerConfig rootLogger = getRootLogger();
             rootLogger.setLevel(Level.DEBUG);
             rootLogger.addAppender(appender, null, null);
          }
       };
    }
    

这两个Configuration是等效的,您可以使用:

将它们应用于当前上下文
Configurator.reconfigure(config);

但是,它们将在Configurator.reconfigure()丢失,除非您定义your own ConfigurationFactory

相关文章