Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly configure log4j2 programmatically?

I am trying to setup log4j2 to write logs using the RollingFileAppender. I want to configure the logging system programmatically instead of using XML files.

Here is what I tried (mostly the same as the docs at https://logging.apache.org/log4j/2.x/manual/customconfig.html#Configurator):

public static void configure(String rootLevel, String packageLevel) {
    ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory
        .newConfigurationBuilder();

    builder.setConfigurationName("RollingBuilder");
    builder.setStatusLevel(Level.TRACE);

    // Create a rolling file appender

    LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")
        .addAttribute("pattern", "%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1}:%L - %m%n");

    ComponentBuilder triggeringPolicy =
        builder
            .newComponent("Policies")
            .addComponent(
                builder
                    .newComponent("SizeBasedTriggeringPolicy")
                    .addAttribute("size", "200M")
            );

    AppenderComponentBuilder appenderBuilder =
        builder
            .newAppender("rolling", "RollingFile")
            .addAttribute("fileName", "log")
            .addAttribute("filePattern", "log.%d.gz")
            .add(layoutBuilder)
            .addComponent(triggeringPolicy);

    builder.add(appenderBuilder);

    // Create new logger

    LoggerComponentBuilder myPackageLoggerBuilder =
        builder.newLogger("com.mypackage", packageLevel)
            .add(builder.newAppenderRef("rolling"))
            .addAttribute("additivity", false);
    builder.add(myPackageLoggerBuilder);

    RootLoggerComponentBuilder rootLoggerBuilder =
        builder
            .newRootLogger(rootLevel)
            .add(builder.newAppenderRef("rolling"));
    builder.add(rootLoggerBuilder);

    // Initialize logging
    Configurator.initialize(builder.build());
}

I call the configure() method at the start of the main method. A file named log is created when I run my program, but all log output goes to standard out and the log file remains empty.

Can someone help figure out what is wrong with my config?

I am not using any log4j configuration file, if that makes a difference. Also using the slf4j API in my code. Dependencies -

org.apache.logging.log4j:log4j-api:2.11.1
org.apache.logging.log4j:log4j-core:2.11.1
org.apache.logging.log4j:log4j-slf4j-impl:2.11.1
org.slf4j:slf4j-api:1.7.25
like image 490
Rohan Avatar asked Dec 07 '25 18:12

Rohan


1 Answers

Please find the sample Root log and finally needs to call reconfigure function.

RootLoggerComponentBuilder rootLogger = builder
   .newRootLogger(Level.ALL)
   .add(builder.newAppenderRef("LogToRollingFile"));

LoggerComponentBuilder logger = builder
   .newLogger("MyClass",Level.ALL)
   .addAttribute("additivity", false);

Configurator.reconfigure(builder.build());
like image 118
Mohsun Avatar answered Dec 09 '25 06:12

Mohsun