Spring Boot Log4j2编程配置不起作用

时间:2019-04-27 13:18:10

标签: spring-boot log4j2 programmatic-config

即使使用org.apache.logging.log4j.core.config.Configuration,也不会更新slf4j之外的log4j2的程序配置。

运行具有以下配置的Spring引导应用程序。

package org.mevenk.webservices;

.
import org.mevenk.webservices.logger.Logger;
import org.mevenk.webservices.logger.LoggerFactory;

@EnableAutoConfiguration
@SpringBootApplication
public class MeVenkWebServicesApplication {

private static final Logger LOG = 
LoggerFactory.getlogger(MeVenkWebServicesApplication.class);

public static void main(String[] args) {
    SpringApplication.run(MeVenkWebServicesApplication.class, args);

    }

}

public interface Logger {

    public void debug(final String message);

}


package org.mevenk.webservices.logger;

import org.apache.logging.log4j.core.config.LoggerConfig;

class LoggerDefinition {

    private LoggerConfig loggerConfig;

    LoggerDefinition(final String name, final LogLevel logLevel, final boolean additive) {
        this.loggerConfig = new LoggerConfig(name, logLevel.getLevel(), additive);
    }

}


package org.mevenk.webservices.logger;

import org.apache.logging.log4j.Level;

enum LogLevel {

    DEBUG {
        @Override
        Level getLevel() {
            return Level.DEBUG;
        }
    }

    abstract Level getLevel();

}


package org.mevenk.webservices.logger;

import org.apache.logging.log4j.LogManager;

final class MeVenkWebServicesLogger implements Logger {

    private final org.apache.logging.log4j.Logger logger;

    MeVenkWebServicesLogger(final Class<?> clazz) {
        logger = LogManager.getLogger(clazz);
        System.out.println(logger);
    }

    @Override
    public void debug(String message) {
        logger.debug(message);

    }

}

通过工厂类处理的配置

package org.mevenk.webservices.logger;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;


public final class LoggerFactory {

    private static final LinkedHashMap<Class<?>, Logger> LOGGER_OBJECTS = new LinkedHashMap<Class<?>, Logger>();
    private static final LinkedHashSet<LoggerDefinition> LOGGER_DEFINITIONS = new LinkedHashSet<LoggerDefinition>();

    static {
        setLoggerConfiguration();
    }

    private static void setLoggerConfiguration() {

        updateLoggerlevelMap();
        addLogLvels();

    }

    private static void updateLoggerlevelMap() {

        LOGGER_DEFINITIONS.add(new LoggerDefinition("org.mevenk.webservices.controller", LogLevel.DEBUG, false));
        LOGGER_DEFINITIONS.add(new LoggerDefinition("org.mevenk.webservices.service", LogLevel.INFO, false));

    }

    private static void addLogLvels() {

        final LoggerContext loggerContext = (LoggerContext) LoggerContext.getContext(false);
        final Configuration config = loggerContext.getConfiguration();

        for (LoggerDefinition definition : LOGGER_DEFINITIONS) {
            LoggerConfig loggerConfig = definition.getLoggerConfig();
            config.addLogger(loggerConfig.getName(), loggerConfig);
            Level level = loggerConfig.getLevel();
            loggerConfig.setLevel(level);
        }

        loggerContext.updateLoggers();

    }

    public static Logger getlogger(Class<?> clazz) {

        return addOrReturnLogger(clazz);
    }

    private static Logger addOrReturnLogger(Class<?> clazz) {
        if (!LOGGER_OBJECTS.containsKey(clazz)) {
            LOGGER_OBJECTS.put(clazz, new MeVenkWebServicesLogger(clazz));
        }
        return LOGGER_OBJECTS.get(clazz);
    }

}

此配置将*.controller软件包设置为DEBUG,但最终将其设置为INFO

启动此应用程序将导致以下Loggers的控制台输出。

org.mevenk.webservices.MeVenkWebServicesApplication:ERROR in 18b4aac2
org.mevenk.webservices.controller.RootController:INFO in 18b4aac2
org.mevenk.webservices.controller.samplerest.SampleRestCallsController:INFO in 18b4aac2
org.mevenk.webservices.service.impl.samplerest.SampleRestCallsServiceImpl:INFO in 18b4aac2

0 个答案:

没有答案