Log4j2: programatically adding additional logger with different log level

34 views Asked by At

I am using log4j2 to programmatically create some loggers. I should have a root logger with a console appender and a file appender. Additionally, there should be an SMTP appender that only logs on Level. Error.

I am able to add the SMTP appender to the root logger and then the SMTP appender is properly working (sending e-mails at the level of the root logger). I am however not able to create a separate logger, with a separate level (Level.Error) containing the SMTP appender. When I look at the log4j2 debug logs, the logger and the appender are created, but no e-mails are sent.

So this works:

        System.setProperty("log4j2.debug", "true");

        ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();

        builder.setStatusLevel(getLevel());

        builder.setConfigurationName("LOG4J2-CONFIGURATION-BUILDER");

        builder.add(consoleAppender.createAppender(builder));

        builder.add(fileAppender.createAppender(builder));

        builder.add(builder.newAppender(smtpAppender.getName(), "SMTP")
                .addAttribute("subject", getSmtpAppenderEmailSubject())
                .addAttribute("to", sendSmtpMessagesTo)
                .addAttribute("from", smtpEmail)
                .addAttribute("smtpUsername", smtpEmail)
                .addAttribute("smtpPassword", smtpPassword)
                .addAttribute("smtpHost", "smtp.gmail.com")
                .addAttribute("smtpPort", 587)
                .addAttribute("bufferSize", 10)
                .addAttribute("smtpProtocol", "smtp")
                .addAttribute("smtpDebug", true)
        );

        builder.add( builder.newRootLogger(getLevel())
                .add(builder.newAppenderRef(consoleAppender.getName()))
                .add(builder.newAppenderRef(fileAppender.getName()))
                .add(builder.newAppenderRef(smtpAppender.getName()))
        );

        LoggerContext ctx = Configurator.initialize(builder.build());

But this doesn't:

        System.setProperty("log4j2.debug", "true");

        ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();

        builder.setStatusLevel(getLevel());

        builder.setConfigurationName("LOG4J2-CONFIGURATION-BUILDER");

        builder.add(consoleAppender.createAppender(builder));

        builder.add(fileAppender.createAppender(builder));

        builder.add(builder.newAppender(smtpAppender.getName(), "SMTP")
                .addAttribute("subject", getSmtpAppenderEmailSubject())
                .addAttribute("to", sendSmtpMessagesTo)
                .addAttribute("from", smtpEmail)
                .addAttribute("smtpUsername", smtpEmail)
                .addAttribute("smtpPassword", smtpPassword)
                .addAttribute("smtpHost", "smtp.gmail.com")
                .addAttribute("smtpPort", 587)
                .addAttribute("bufferSize", 10)
                .addAttribute("smtpProtocol", "smtp")
                .addAttribute("smtpDebug", true)
        );

        builder.add( builder.newRootLogger(getLevel())
                .add(builder.newAppenderRef(consoleAppender.getName()))
                .add(builder.newAppenderRef(fileAppender.getName()))
                //.add(builder.newAppenderRef(smtpAppender.getName()))
        );



        builder.add(builder.newLogger("SMTP_LOGGER", Level.ERROR)
                .add(builder.newAppenderRef(smtpAppender.getName()))
                .addAttribute("additivity", false)
        );

        LoggerContext ctx = Configurator.initialize(builder.build());

I have gotten my inspiration mainly from here: https://www.baeldung.com/log4j2-programmatic-config

This question seems similar: How to create second, separate log4j2 logger?, however, the solution suggested there does not work for me. No e-mail are sent.

0

There are 0 answers