Search in sources :

Example 36 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project tutorials by eugenp.

the class MyServiceTest method testProgrammaticConfig.

@Test
public void testProgrammaticConfig() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    PatternLayout layout = PatternLayout.newBuilder().withConfiguration(config).withPattern("%d{HH:mm:ss.SSS} %level %msg%n").build();
    Appender appender = FileAppender.newBuilder().setConfiguration(config).withName("programmaticFileAppender").withLayout(layout).withFileName("java.log").build();
    appender.start();
    config.addAppender(appender);
    AppenderRef ref = AppenderRef.createAppenderRef("programmaticFileAppender", null, null);
    AppenderRef[] refs = new AppenderRef[] { ref };
    LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "programmaticLogger", "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("programmaticLogger", loggerConfig);
    ctx.updateLoggers();
    Logger pLogger = LogManager.getLogger("programmaticLogger");
    pLogger.info("Programmatic Logger Message");
}
Also used : Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) AppenderRef(org.apache.logging.log4j.core.config.AppenderRef) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) Test(org.junit.Test)

Example 37 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project incubator-pulsar by apache.

the class ConfigurationAssemblerTest method testBuildConfiguration.

@Test
public void testBuildConfiguration() throws Exception {
    try {
        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
        final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
        CustomConfigurationFactory.addTestFixtures("config name", builder);
        final Configuration configuration = builder.build();
        try (LoggerContext ctx = Configurator.initialize(configuration)) {
            validate(configuration);
        }
    } finally {
        System.getProperties().remove(Constants.LOG4J_CONTEXT_SELECTOR);
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.testng.annotations.Test)

Example 38 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project incubator-pulsar by apache.

the class JavaInstanceRunnable method removeLogTopicHandler.

private void removeLogTopicHandler() {
    if (logAppender == null)
        return;
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    for (final LoggerConfig loggerConfig : config.getLoggers().values()) {
        loggerConfig.removeAppender(logAppender.getName());
    }
    config.getRootLogger().removeAppender(logAppender.getName());
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) NamespaceConfiguration(org.apache.bookkeeper.stream.proto.NamespaceConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 39 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project spring-integration by spring-projects.

the class Log4j2LevelAdjuster method apply.

@Override
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
            Configuration config = ctx.getConfiguration();
            Map<Class<?>, Level> classLevels = new HashMap<>();
            for (Class<?> cls : Log4j2LevelAdjuster.this.classes) {
                String className = cls.getName();
                LoggerConfig loggerConfig = config.getLoggerConfig(className);
                LoggerConfig specificConfig = loggerConfig;
                if (!loggerConfig.getName().equals(className)) {
                    specificConfig = new LoggerConfig(className, Log4j2LevelAdjuster.this.level, true);
                    specificConfig.setParent(loggerConfig);
                    config.addLogger(className, specificConfig);
                }
                classLevels.put(cls, specificConfig.getLevel());
                specificConfig.setLevel(Log4j2LevelAdjuster.this.level);
            }
            Map<String, Level> categoryLevels = new HashMap<>();
            for (String category : Log4j2LevelAdjuster.this.categories) {
                LoggerConfig loggerConfig = config.getLoggerConfig(category);
                LoggerConfig specificConfig = loggerConfig;
                if (!loggerConfig.getName().equals(category)) {
                    specificConfig = new LoggerConfig(category, Log4j2LevelAdjuster.this.level, true);
                    specificConfig.setParent(loggerConfig);
                    config.addLogger(category, specificConfig);
                }
                categoryLevels.put(category, specificConfig.getLevel());
                specificConfig.setLevel(Log4j2LevelAdjuster.this.level);
            }
            ctx.updateLoggers();
            logger.debug("++++++++++++++++++++++++++++ " + "Overridden log level setting for: " + Arrays.toString(Log4j2LevelAdjuster.this.classes) + " and " + Arrays.toString(Log4j2LevelAdjuster.this.categories) + " for test " + method.getName());
            try {
                base.evaluate();
            } finally {
                logger.debug("++++++++++++++++++++++++++++ " + "Restoring log level setting for: " + Arrays.toString(Log4j2LevelAdjuster.this.classes) + " and " + Arrays.toString(Log4j2LevelAdjuster.this.categories) + " for test " + method.getName());
                for (Class<?> cls : Log4j2LevelAdjuster.this.classes) {
                    LoggerConfig loggerConfig = config.getLoggerConfig(cls.getName());
                    loggerConfig.setLevel(classLevels.get(cls));
                }
                for (String category : Log4j2LevelAdjuster.this.categories) {
                    LoggerConfig loggerConfig = config.getLoggerConfig(category);
                    loggerConfig.setLevel(categoryLevels.get(category));
                }
                ctx.updateLoggers();
            }
        }
    };
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) HashMap(java.util.HashMap) Statement(org.junit.runners.model.Statement) Level(org.apache.logging.log4j.Level) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 40 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project core-ng-project by neowu.

the class ESLoggerConfigFactory method bindLogger.

public static void bindLogger() {
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration config = context.getConfiguration();
    Map<String, ESLogger> loggers = Maps.newConcurrentHashMap();
    Appender appender = new AbstractAppender("", null, null) {

        @Override
        public void append(LogEvent event) {
            String name = event.getLoggerName();
            ESLogger logger = loggers.computeIfAbsent(name, key -> new ESLogger(key, null, (LoggerImpl) LoggerFactory.getLogger(key)));
            logger.log(event.getLevel(), event.getMarker(), event.getMessage(), event.getThrown());
        }
    };
    appender.start();
    config.addAppender(appender);
    LoggerConfig loggerConfig = new LoggerConfig("", Level.ALL, false);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("", loggerConfig);
    context.updateLoggers();
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) LoggerImpl(core.framework.impl.log.LoggerImpl) LoggerContext(org.apache.logging.log4j.core.LoggerContext) ESLogger(core.framework.impl.search.log.ESLogger) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)210 LoggerContext (org.apache.logging.log4j.core.LoggerContext)120 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)71 Test (org.junit.Test)54 Appender (org.apache.logging.log4j.core.Appender)50 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)37 Test (org.junit.jupiter.api.Test)29 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)22 File (java.io.File)21 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)20 Path (java.nio.file.Path)14 Map (java.util.Map)14 ArrayList (java.util.ArrayList)13 Level (org.apache.logging.log4j.Level)13 ListAppender (org.apache.log4j.ListAppender)12 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)12 ConfigurationSource (org.apache.logging.log4j.core.config.ConfigurationSource)12 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)11 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)11 InputStream (java.io.InputStream)10