Afer many days of struggling and trying to figure out this abomination I'm at my begginer limits.
Not working code based on documentation of log4j2:
final LoggerContext cx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final Layout<String> layout = PatternLayout.createDefaultLayout(config);
FileAppender.Builder<?> ab = (FileAppender.Builder<?>) FileAppender.newBuilder();
ab.withFileName("test.txt");
ab.withAppend(true);
ab.setName("test");
ab.setLayout(layout)
ab.build();
FileAppender appender = ab.build();
appender.start();
config.addAppener(appender);
AppenderRef ref = AppenderRef.createAppenderRef("test", null, null);
AppenderRef[] refs = new AppenderRef[] {ref};
LoggerConfig lc = LoggerConfig.createLogger(false, Level.DEBUG, "test", "true", refs, null, config, null);
lc.addAppender(appender, null, null);
config.addLogger("test", lc);
ctx.updateLoggers();
This is how I see in my head what this code is doing. Correct me if I'm wrong:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<!-- Here is the output from code -->
<FileAppender name="test" fileName="test.txt" />
</Appenders>
<Loggers>
<!-- Here is the output from code -->
<Logger name="test" level="debug">
<AppenderRef ref="traceLog" />
</Logger>
<!-- Below is some already existing root logger -->
<Root level="info" additivity="false">
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
But as mentioned this does not work. When debugging in intellij I can see configs and loggers being created. Debugging compiled code from command line with log4j2.debug also shows that appender and logger "test" are started and stopped and yet this code just does not work and I have no idea why.
Now after tinkering a bit, now I have code like below. It works but not as I want.
final LoggerContext cx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final Layout<String> layout = PatternLayout.createDefaultLayout(config);
FileAppender.Builder<?> ab = (FileAppender.Builder<?>) FileAppender.newBuilder();
ab.withFileName("test.txt");
ab.withAppend(true);
ab.setName("test");
ab.setLayout(layout)
ab.build();
FileAppender appender = ab.build();
appender.start();
ctx.getConfiguration().addAppender(appender);
ctx.getRootLogger().addAppender(ctx.getConfiguration().getAppender(appender.getName());
ctx.updateLoggers();
Again this is how I see in my head what this code is doing. Correct me if I'm wrong:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<!-- Here is the output from code -->
<FileAppender name="test" fileName="test.txt" />
</Appenders>
<Loggers>
<!-- Below is some already existing root logger modified by above java code -->
<Root level="info" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="test" />
</Root>
</Loggers>
</Configuration>
Now this works and logs are created but I've found a way to change logging level for entire Root logger and not for separate refs which I need instead.
That said does someone know how to make the first part of code to work or how to set logging level in references added to root logger but programmaticaly?
I will be grateful for all help regarding this dreadful library that is just broken and overcomplicated. But it is my personal opinion.