Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically configure appenders or initialize logging in log4j2?

I have been trying to configure appender programatically, but not getting success so far. i just want to configure appender without file so that it can send logs to console or file.

    String PATTERN = "%d [%p|%c|%C{1}] %m%n";
    PatternLayout layout = PatternLayout.createLayout(PATTERN, null, null,
            null, null, null);
    ConsoleAppender console = ConsoleAppender.createAppender(layout, null,
            null, "console", "true", "true"); // create appender

    AppenderRef appender = AppenderRef.createAppenderRef("console",
            "DEBUG", null);

    logger = (Logger) LogManager.getLogger(InitLogger.class);
    LoggerContext context = logger.getContext();
    BaseConfiguration configuration =(BaseConfiguration) context.getConfiguration();
    configuration.addAppender(console);
    logger.addAppender(configuration.getAppender("console"));
like image 854
Akhilesh Avatar asked Jun 30 '14 11:06

Akhilesh


People also ask

How do you get all Appenders in log4j2?

Enumeration appenders = logger. getAllAppenders(); . . . fileBackupIndex = rollingFileAppender. getMaxBackupIndex();

How do I use console Appenders in log4j2?

Log4j2 ConsoleAppender ConfigurationThe target poperty specifies the target of the logging messages i.e. SYSTEM_OUT or SYSTEM_ERR . The follow attribute tells whether the appender should honor the reassignments of System. out or System. err made after the logging configuration has been initialized.


1 Answers

Here is the simplest way to do this:

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.AbstractConfiguration;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;

public class Main {

    public static void main(String[] args) {
        configure();
        Logger logger = LogManager.getLogger("com.company");
        logger.trace("Hello Word!");

    }

    public static void configure() {
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
        ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout());
        appender.start();
        config.addAppender(appender);
        AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) };
        LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null);
        loggerConfig.addAppender(appender, null, null);
        config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
        ctx.updateLoggers();
    }
}
like image 120
Paul Vargas Avatar answered Nov 04 '22 06:11

Paul Vargas