How to configure SmtpAppender programmatically in log4j2(如何在log4j2中以编程方式配置SmtpAppender)
问题描述
以下是我正在编写的代码:
目的是以编程方式配置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
。
这篇关于如何在log4j2中以编程方式配置SmtpAppender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在log4j2中以编程方式配置SmtpAppender
基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01