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