'log4j2.17.1 creates the file but does not write to it
I use log4j2.17.1 but it does not write to the file it created. I created the follwing configurations programatically. Is there anything wrong with it? Thanks in adnvance.
<?xml version="1.0"?>
<Configuration>
<Appenders>
<RollingFile name="rolling" fileName="logsInfo" filePattern="rolling-%d{MM-dd-yy}.log.gz">
<Policies>
<SizeBasedTriggeringPolicy size="5kb"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="Info">
<AppenderRef ref="rolling"/>
</Root>
</Loggers>
</Configuration>
Here is the code that generated the above configurations:
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
AppenderComponentBuilder rolling = builder.newAppender("rolling", "RollingFile");
rolling.addAttribute("fileName", "logInfo");
rolling.addAttribute("filePattern", "rolling-%d{MM-dd-yy}.log.gz");
ComponentBuilder<?> triggeringPolicies = builder.newComponent("Policies")
.addComponent(builder.newComponent("SizeBasedTriggeringPolicy")
.addAttribute("size", "5kb"));
rolling.addComponent(triggeringPolicies);
ComponentBuilder<?> rolloverStrategy = builder.newComponent("DefaultRolloverStrategy")
.addAttribute("max", 5);
rolling.addComponent(rolloverStrategy);
String log4jPattern = "%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n";
LayoutComponentBuilder standard = builder.newLayout("PatternLayout");
standard.addAttribute("pattern", log4jPattern);
rolling.add(standard);
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(level);
rootLogger.add(builder.newAppenderRef("rolling"));
builder.add(rolling);
builder.add(rootLogger);
//wrties the the configurations in xml to stdout
builder.writeXmlConfiguration(System.out);
System.out.println();
BuiltConfiguration config = builder.build();
Configurator.initialize(config);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
