简体   繁体   English

以编程方式创建 Log4j 2.x FileAppender

[英]Create a Log4j 2.x FileAppender programmatically

I want to migrate from Log4j 1.2 to 2.4.我想从 Log4j 1.2 迁移到 2.4。 Since I'm running multiple instances of my program on the same machines I want to include an ID (called clientId in the following code) in the logfile.因为我在同一台机器上运行我的程序的多个实例,所以我想在日志文件中包含一个 ID(在下面的代码中称为 clientId)。 Therefore I used Log4j 1.2's way to setup a FileAppender programmatically:因此,我使用 Log4j 1.2 的方式以编程方式设置 FileAppender:

int clientId = ?// gets set before

FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile("logs/client_" + clientId + ".log");
fa.setLayout(new PatternLayout("%d %-5p %c{1} %m%n"));
fa.setThreshold(Level.INFO);
fa.setAppend(true);
fa.activateOptions();
Logger.getRootLogger().addAppender(fa);

I fail to achieve something similar with Log4j 2.0, since they removed the ability to directly modify these properties.我无法实现与 Log4j 2.0 类似的东西,因为它们删除了直接修改这些属性的能力。 Instead I tried to use a CustomConfigurationFactory like described in https://logging.apache.org/log4j/2.x/manual/customconfig.html#Example But I fail to understand how I make use of it?相反,我尝试使用https://logging.apache.org/log4j/2.x/manual/customconfig.html#Example 中描述的 CustomConfigurationFactory 但我不明白我如何使用它? The documentation states该文件指出

This will cause the Configuration to automatically be hooked into Log4j when the LoggerContext is created.这将导致配置在创建 LoggerContext 时自动挂钩到 Log4j。

I tried something like:我试过类似的东西:

LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.getConfiguration(CustomConfigurationFactory.getInstance());

but this doesn't work.但这不起作用。

I found it out myself.我自己发现了。 I modified the Factory to use a FileAppender:我修改了工厂以使用 FileAppender:

    builder.setConfigurationName(name);
    builder.setStatusLevel(Level.INFO);
    builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).
            addAttribute("level", Level.INFO));
    AppenderComponentBuilder appenderBuilder = builder.newAppender("file", "FILE").
            addAttribute("fileName", "log/client_"+Config.CLIENTID+".log");
    appenderBuilder.add(builder.newLayout("PatternLayout").
            addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"));
    appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY,
            Filter.Result.NEUTRAL).addAttribute("marker", "FLOW"));
    builder.add(appenderBuilder);
    builder.add(builder.newLogger("org.apache.logging.log4j", Level.INFO).
            add(builder.newAppenderRef("file")).
            addAttribute("additivity", false));
    builder.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef("file")));
    return builder.build();

And then use:然后使用:

ConfigurationFactory.setConfigurationFactory(new CustomConfigurationFactory());
Logger log = LogManager.getLogger(Main.class.getName());

to get the new logger获取新的记录器

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

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