如何以编程方式配置appender或初始化log4j2中的日志记录?

时间:2014-06-30 11:23:25

标签: java logging log4j2

我一直在尝试以编程方式配置appender,但到目前为止还没有获得成功。 我只想配置没有文件的appender,以便它可以将日志发送到控制台或文件。

    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"));

2 个答案:

答案 0 :(得分:6)

这是最简单的方法:

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();
    }
}

答案 1 :(得分:4)

尝试以下示例代码

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();
        }
    }
}