如何在log4j 2中使用AppenderComponentBuilder正确创建RollingFileAppender [英] How to properly create a RollingFileAppender with AppenderComponentBuilder in log4j 2
问题描述
我希望用户选择将通过可配置选项创建的appender的类型。
I would like the user to choose the type of the appender(s) that will be created via a configurable option.
例如,假设我有一个命令行参数来控制将为该进程创建的appender的类型。用户可以选择以下值之一:file,syslog或两者。
For example, let's say that I have a command line argument that controls the type of the appenders that will be created for that process. The user can choose one of these values: file,syslog, or both.
我想以编程方式执行此操作,而不是使用外部xml配置文件。
I would like to do this in a programmatic way, rather than using an external xml configuration file.
这是我尝试以编程方式创建带有syslog appender的记录器的简化版本。
This is a simplified version of my attempt to create a logger with a syslog appender in a programmatic way.
// simplified version of a method that creates and returns a logger
// using AppenderComponentBuilder
static Logger createLogger()
{
ConfigurationBuilder< BuiltConfiguration > builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel( Level.OFF );
// create the syslog appender
AppenderComponentBuilder appenderBuilder =
builder.newAppender( "syslogAppender", "Syslog" )
.addAttribute( "protocol", "TCP" )
.addAttribute( "host", "localhost" )
.addAttribute( "port", 514 )
.addAttribute( "facility", "LOCAL2" )
.addAttribute( "immediateFlush", true )
.addAttribute( "newLine", true );
builder.add( appenderBuilder );
// create the new logger
builder.add( builder.newLogger( "TestLogger", Level.DEBUG )
.add( builder.newAppenderRef( "syslogAppender" ) )
.addAttribute( "additivity", false ) );
builder.add( builder.newRootLogger( Level.DEBUG )
.add( builder.newAppenderRef( "syslogAppender" ) ) );
// Initialize the new configuration
LoggerContext ctx = Configurator.initialize( builder.build() );
Logger logger = ctx.getLogger( "TestLogger" );
return logger;
}
我的问题:
1)您是否认为这是使用log4j 2 API以编程方式创建记录器和syslog appender的最佳方式?
1) Do you believe that this is the best way to use the log4j 2 API in order to programmatically create a logger and a syslog appender?
2)我会喜欢扩展上面的createLogger()方法,以便根据用户选择创建合适的appender:file,syslog,两者。 file选项应创建一个Rolling文件appender,它具有特定的模式布局,触发策略和翻转策略。我尝试使用相同的log4j 2 API并使用AppenderComponentBuilder来执行此操作,但我找不到正确的方法。我设法以编程方式执行此操作的唯一方法是使用RollingFileAppender.createAppender()方法,这是一个真正的痛苦,并且很难让我以编程方式动态创建具有滚动文件追加器的单个记录器,一个syslog appender或两者(取决于用户输入)。
2) I would like to extend the above createLogger() method so that it will create the proper appenders depending on the user choice: file,syslog,both. The "file" option should create a Rolling file appender with a specific pattern layout, triggering policy, and rollover strategy. I tried to use the same log4j 2 API and use the AppenderComponentBuilder to do this, but I couldn't find a correct way of doing this. The only way I managed to programmatically do this was with the RollingFileAppender.createAppender() method, which is a real pain and makes it very difficult to allow me to create, on the fly, programmatically, a single logger with either a rolling file appender, a syslog appender or both (depending on the user input).
所以,实质上,我正在寻找的是以编程方式创建RollingFileAppender的正确方法使用AppenderComponentBuilder的特定模式布局,触发策略和翻转策略。
So, in essence, what I am looking for is the proper way to programmatically create a RollingFileAppender with a specific pattern layout, triggering policy, and rollover strategy using AppenderComponentBuilder.
提前致谢
推荐答案
-
是的,这是以编程方式创建新配置的预期方式。
Yes, this is the intended way to programmatically create a new configuration.
这是一个如何创建滚动文件追加器的示例。由于Appender可以配置任意组件,因此您可以使用通用ComponentBuilder来指定这些插件。
Here is an example of how to create a rolling file appender. Since Appenders can be configured with arbitrary components you use the generic ComponentBuilder to specify those plugins.
ConfigurationBuilder< BuiltConfiguration > builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
builder.setStatusLevel( Level.ERROR);
builder.setConfigurationName("RollingBuilder");
// create the console appender
AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target",
ConsoleAppender.Target.SYSTEM_OUT);
appenderBuilder.add(builder.newLayout("PatternLayout").
addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
builder.add( appenderBuilder );
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
.addAttribute("pattern", "%d [%t] %-5level: %msg%n");
ComponentBuilder triggeringPolicy = builder.newComponent("Policies")
.addComponent(builder.newComponent("CronTriggeringPolicy").addAttribute("schedule", "0 0 0 * * ?"))
.addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", "100M"));
appenderBuilder = builder.newAppender("rolling", "RollingFile")
.addAttribute("fileName", "target/rolling.log")
.addAttribute("filePattern", "target/archive/rolling-%d{MM-dd-yy}.log.gz")
.add(layoutBuilder)
.addComponent(triggeringPolicy);
builder.add(appenderBuilder);
// create the new logger
builder.add( builder.newLogger( "TestLogger", Level.DEBUG )
.add( builder.newAppenderRef( "rolling" ) )
.addAttribute( "additivity", false ) );
builder.add( builder.newRootLogger( Level.DEBUG )
.add( builder.newAppenderRef( "rolling" ) ) );
Configurator.initialize(builder.build());
这篇关于如何在log4j 2中使用AppenderComponentBuilder正确创建RollingFileAppender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!