7

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"));
Akhilesh
  • 1,400
  • 3
  • 15
  • 20
  • 1
    You need to reset and add your appenders to the rootLogger. See [this post](http://stackoverflow.com/questions/8965946/configuring-log4j-loggers-programmatically) – Stefan Jun 30 '14 at 11:29
  • This post talks about log4j1, so this is not helpful. – Akhilesh Jun 30 '14 at 11:41
  • 1
    This was possible with the help of ConfigurationFactory of log4j2 [link][1] [1]: http://stackoverflow.com/questions/20886717/configure-log4j2-programmatically-using-configurationfactory – Akhilesh Jul 01 '14 at 07:56

2 Answers2

7

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();
    }
}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
4

Try the following sample code

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.AppenderRef;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.spi.LoggerContext;

public class TestLog4j2 {

    public static void main(String[] args) {
        try {

            ConsoleAppender console = ConsoleAppender.createAppender(PatternLayout.createDefaultLayout(), null, "SYSTEM_OUT", "console", null, null);
             final LoggerContext ctx = (LoggerContext) new org.apache.logging.log4j.core.LoggerContext("console" );
                final Configuration config = ((org.apache.logging.log4j.core.LoggerContext) ctx).getConfiguration();
                console.start();
                config.addAppender(console);
                AppenderRef ref = AppenderRef.createAppenderRef("console", null, null);
                AppenderRef[] refs = new AppenderRef[] {ref};
                LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.ALL, "org.apache.logging.log4j",
                    "true", refs, null, config, null );
                loggerConfig.addAppender(console, null, null);
                config.addLogger("org.apache.logging.log4j", loggerConfig);
                ((org.apache.logging.log4j.core.LoggerContext) ctx).updateLoggers();
                ExtendedLogger logger = (ExtendedLogger) ctx.getLogger("console");
                logger.error("abc");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Bruce
  • 793
  • 8
  • 17