简体   繁体   English

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

[英]How to configure SmtpAppender programmatically in log4j2

Following is the code, I am working on:以下是我正在处理的代码:

Purpose is to configure SmtpAppender programmatically.目的是以编程方式配置 SmtpAppender。 Along with the SmtpAppender, I also need to add RollingFileAppender as well as Console appender programmatically.除了 SmtpAppender,我还需要以编程方式添加 RollingFileAppender 和 Console appender。

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");
  }
}

How should I add the smtpAppender to the configutation or the rootLogger?我应该如何将 smtpAppender 添加到配置或 rootLogger?

You are mixing up two APIs:您正在混合两个 API:

  • the ConfigurationBuilder API , which is the closest code equivalent of the configuration files. ConfigurationBuilder API ,这是与配置文件最接近的等效代码。 It only creates definitions of the actual logging components, the real ones are created when Configuration#initialize() is called on the configuration object.它只创建实际日志组件的定义,真正的组件是在配置 object 上调用Configuration#initialize()时创建的。 You can create the definition of an SMTPAppender like this:您可以像这样创建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(); }
  • the actual builders of Log4j 2.x components, which are called by reflection by Configuration#initialize using the definitions above. Log4j 2.x 组件的实际构建者,使用上述定义由Configuration#initialize通过反射调用。 You can also use them directly:您也可以直接使用它们:

     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); } }; }

Both Configuration s are equivalent and you can apply them on the current context with:两个Configuration是等效的,您可以将它们应用于当前上下文:

Configurator.reconfigure(config);

However they will be lost upon Configurator.reconfigure() , unless you define your own ConfigurationFactory .但是,除非您定义自己的ConfigurationFactory ,否则它们将在Configurator.reconfigure()上丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM