本文整理了Java中org.apache.logging.log4j.core.config.Configuration
类的一些代码示例,展示了Configuration
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration
类的具体详情如下:
包路径:org.apache.logging.log4j.core.config.Configuration
类名称:Configuration
[英]Interface that must be implemented to create a configuration.
Custom implementations are recommended to extend AbstractConfiguration.
[中]创建配置时必须实现的接口。
建议使用自定义实现来扩展AbstractConfiguration。
代码示例来源:origin: Graylog2/graylog2-server
private void initializeLogging(final Level logLevel) {
final LoggerContext context = (LoggerContext) LogManager.getContext(false);
final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(logLevel);
config.getLoggerConfig(Main.class.getPackage().getName()).setLevel(logLevel);
context.updateLoggers(config);
}
代码示例来源:origin: igniterealtime/Openfire
String addAppender(final Writer writer) {
final String name = "openfire-s2s-test-appender-" + StringUtils.randomString( 10 );
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
final PatternLayout layout = PatternLayout.createDefaultLayout(config);
final Appender appender = WriterAppender.createAppender(layout, null, writer, name, false, true);
appender.start();
config.addAppender(appender);
final Level level = null;
final Filter filter = null;
for (final LoggerConfig loggerConfig : config.getLoggers().values()) {
loggerConfig.addAppender(appender, level, filter);
}
config.getRootLogger().addAppender(appender, level, filter);
return name;
}
代码示例来源:origin: Graylog2/graylog2-server
@VisibleForTesting
protected void setLoggerLevel(final String loggerName, final Level level) {
final LoggerContext context = (LoggerContext) LogManager.getContext(false);
final Configuration config = context.getConfiguration();
final LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
if(loggerName.equals(loggerConfig.getName())) {
loggerConfig.setLevel(level);
} else {
final LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, level, loggerConfig.isAdditive());
newLoggerConfig.setLevel(level);
config.addLogger(loggerName, newLoggerConfig);
}
context.updateLoggers();
}
代码示例来源:origin: apache/storm
public void setLoggerLevel(LoggerContext logContext, String loggerName, String newLevelStr) {
Level newLevel = Level.getLevel(newLevelStr);
Configuration configuration = logContext.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
if (loggerConfig.getName().equalsIgnoreCase(loggerName)) {
LOG.info("Setting {} log level to: {}", loggerConfig, newLevel);
loggerConfig.setLevel(newLevel);
} else {
// create a new config. Make it additive (true) s.t. inherit parents appenders
LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, newLevel, true);
LOG.info("Adding config for: {} with level: {}", newLoggerConfig, newLevel);
configuration.addLogger(loggerName, newLoggerConfig);
}
}
}
代码示例来源:origin: igniterealtime/Openfire
void removeAppender(final String name) {
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
config.getAppenders().remove( name ).stop();
for (final LoggerConfig loggerConfig : config.getLoggers().values()) {
loggerConfig.removeAppender( name );
}
config.getRootLogger().removeAppender( name );
}
代码示例来源:origin: dadoonet/fscrawler
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(FsCrawlerCli.class.getPackage().getName());
logger.warn("--debug or --trace can't be used when --silent is set. Only silent mode will be activated.");
logger.warn("--silent is set but no job has been defined. Add a job name or remove --silent option. Exiting.");
jCommander.usage();
return;
LoggerConfig rootLogger = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.setLevel(Level.OFF);
rootLogger.setLevel(Level.OFF);
} else {
loggerConfig.setLevel(commands.debug ? Level.DEBUG : Level.TRACE);
ctx.updateLoggers();
logger.info("No job specified. Here is the list of existing jobs:");
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testPropertiesConfiguration() {
final Configuration config = context.getConfiguration();
assertNotNull("No configuration created", config);
assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
final Map<String, Appender> appenders = config.getAppenders();
assertNotNull(appenders);
assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1);
final Map<String, LoggerConfig> loggers = config.getLoggers();
assertNotNull(loggers);
assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 1);
final Filter filter = config.getFilter();
assertNotNull("No Filter", filter);
assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
final Logger logger = LogManager.getLogger(getClass());
logger.info("Welcome to Log4j!");
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
private void validateAppender(final LoggerContext loggerContext, final String expectedFilePattern) {
final RollingFileAppender appender = loggerContext.getConfiguration().getAppender("fooAppender");
Assert.assertNotNull(appender);
Assert.assertEquals(expectedFilePattern, appender.getFilePattern());
LogManager.getLogger("root").info("just to show it works.");
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
final File file = new File(FILE);
assertTrue("Log file does not exist", file.exists());
logger.debug("This is test message number 1");
Thread.sleep(2500);
final File dir = new File(DIR);
break;
logger.debug("Sleeping #" + i);
Thread.sleep(100); // Allow time for rollover to complete
final File[] files = dir.listFiles();
for (final File dirFile : files) {
logger.error("Found file: " + dirFile.getPath());
final RollingFileAppender app = (RollingFileAppender) loggerContextRule.getLoggerContext().getConfiguration().getAppender("RollingFile");
final TriggeringPolicy policy = app.getManager().getTriggeringPolicy();
assertNotNull("No triggering policy", policy);
代码示例来源:origin: org.apache.logging.log4j/log4j-core
/**
* Validates that the code pattern we use to add an appender on the fly
* works with a basic appender that is not the new OutputStream appender or
* new Writer appender.
*/
@Test
public void testUpdatePatternWithFileAppender() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
// @formatter:off
final Appender appender = FileAppender.newBuilder()
.withFileName("target/" + getClass().getName() + ".log")
.withAppend(false)
.withName("File")
.withIgnoreExceptions(false)
.withBufferedIo(false)
.withBufferSize(4000)
.setConfiguration(config)
.build();
// @formatter:on
appender.start();
config.addAppender(appender);
ConfigurationTestUtils.updateLoggers(appender, config);
LogManager.getLogger().error("FOO MSG");
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
ctx.reconfigure();
final Configuration config = ctx.getConfiguration();
assertThat(config, instanceOf(XmlConfiguration.class));
for (final StatusListener listener : StatusLogger.getLogger().getListeners()) {
config.addAppender(appender);
final AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
final AppenderRef[] refs = new AppenderRef[] {ref};
final LoggerConfig loggerConfig = LoggerConfig.createLogger(false, Level.INFO, "org.apache.logging.log4j",
"true", refs, null, config, null );
loggerConfig.addAppender(appender, null, null);
config.addLogger("org.apache.logging.log4j", loggerConfig);
ctx.updateLoggers();
final Logger logger = ctx.getLogger(CustomConfigurationTest.class.getName());
logger.info("This is a test");
final File file = new File(LOG_FILE);
assertThat(file, exists());
代码示例来源:origin: org.apache.logging.log4j/log4j-core
private void validate(final Configuration config) {
assertNotNull(config);
assertNotNull(config.getName());
assertFalse(config.getName().isEmpty());
assertNotNull("No configuration created", config);
assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
final Map<String, Appender> appenders = config.getAppenders();
assertNotNull(appenders);
assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 2);
final KafkaAppender kafkaAppender = (KafkaAppender)appenders.get("Kafka");
final GelfLayout gelfLayout = (GelfLayout)kafkaAppender.getLayout();
final Map<String, LoggerConfig> loggers = config.getLoggers();
assertNotNull(loggers);
assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
final LoggerConfig rootLoggerConfig = loggers.get("");
assertEquals(Level.ERROR, rootLoggerConfig.getLevel());
assertFalse(rootLoggerConfig.isIncludeLocation());
final LoggerConfig loggerConfig = loggers.get("org.apache.logging.log4j");
assertEquals(Level.DEBUG, loggerConfig.getLevel());
assertTrue(loggerConfig.isIncludeLocation());
final Filter filter = config.getFilter();
assertNotNull("No Filter", filter);
assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
final List<CustomLevelConfig> customLevels = config.getCustomLevels();
assertNotNull("No CustomLevels", filter);
assertEquals(1, customLevels.size());
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testBadFileName() throws Exception {
final StringBuilder dir = new StringBuilder("/VeryLongDirectoryName");
for (final String element : CHARS) {
dir.append(element);
dir.append(element.toUpperCase());
}
final String value = FILESEP.equals("/") ? dir.toString() + "/test.log" : "1:/target/bad:file.log";
System.setProperty("testfile", value);
ctx = Configurator.initialize("Test1", "bad/log4j-badfilename.xml");
LogManager.getLogger("org.apache.test.TestConfigurator");
final Configuration config = ctx.getConfiguration();
assertNotNull("No configuration", config);
assertEquals("Unexpected Configuration", "XMLConfigTest", config.getName());
assertThat(config.getAppenders(), hasSize(equalTo(2)));
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
final File file = new File("target/test-classes/log4j2-config.xml");
assertTrue("setLastModified should have succeeded.", file.setLastModified(System.currentTimeMillis() - 120000));
ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml");
final Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator");
Configuration config = ctx.getConfiguration();
assertNotNull("No configuration", config);
assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
final Map<String, Appender> map = config.getAppenders();
assertNotNull("Appenders map should not be null.", map);
assertThat(map, hasSize(greaterThan(0)));
TimeUnit.SECONDS.sleep(config.getWatchManager().getIntervalSeconds()+1);
for (int i = 0; i < 17; ++i) {
logger.debug("Test message " + i);
if (is(theInstance(config)).matches(ctx.getConfiguration())) {
Thread.sleep(500);
final Configuration newConfig = ctx.getConfiguration();
assertThat("Configuration not reset", newConfig, is(not(theInstance(config))));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
代码示例来源:origin: org.elasticsearch/elasticsearch
public static void addAppender(final Logger logger, final Appender appender) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.addAppender(appender);
LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
if (!logger.getName().equals(loggerConfig.getName())) {
loggerConfig = new LoggerConfig(logger.getName(), logger.getLevel(), true);
config.addLogger(logger.getName(), loggerConfig);
}
loggerConfig.addAppender(appender, null, null);
ctx.updateLoggers();
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
@Test
public void testPropertiesConfiguration() {
final Configuration config = context.getConfiguration();
assertNotNull("No configuration created", config);
assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED);
final Map<String, Appender> appenders = config.getAppenders();
assertNotNull(appenders);
assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1);
final Map<String, LoggerConfig> loggers = config.getLoggers();
assertNotNull(loggers);
assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2);
final Filter filter = config.getFilter();
assertNotNull("No Filter", filter);
assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter);
final Logger logger = LogManager.getLogger(getClass());
assertEquals("Incorrect level " + logger.getLevel(), Level.DEBUG, logger.getLevel());
logger.debug("Welcome to Log4j!");
}
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
final File file = new File(FILE);
assertTrue("Log file does not exist", file.exists());
logger.debug("This is test message number 1, waiting for rolling");
final RollingFileAppender app = (RollingFileAppender) loggerContextRule.getLoggerContext().getConfiguration().getAppender("RollingFile");
final TriggeringPolicy policy = app.getManager().getTriggeringPolicy();
assertNotNull("No triggering policy", policy);
final CronExpression expression = ((CronTriggeringPolicy) policy).getCronExpression();
assertEquals("Incorrect cron expresion", cronExpression, expression.getCronExpression());
logger.debug("Cron expression will be {}", expression.getCronExpression());
logger.debug("Adding first event {}", i);
代码示例来源:origin: spring-projects/spring-integration
@Override
public void evaluate() throws Throwable {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
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);
LoggerConfig loggerConfig = config.getLoggerConfig(category);
LoggerConfig specificConfig = loggerConfig;
specificConfig = new LoggerConfig(category, Log4j2LevelAdjuster.this.level, true);
specificConfig.setParent(loggerConfig);
config.addLogger(category, specificConfig);
ctx.updateLoggers();
LoggerConfig loggerConfig = config.getLoggerConfig(cls.getName());
loggerConfig.setLevel(classLevels.get(cls));
LoggerConfig loggerConfig = config.getLoggerConfig(category);
loggerConfig.setLevel(categoryLevels.get(category));
ctx.updateLoggers();
代码示例来源:origin: apache/hive
Logger logger = LogManager.getLogger("hive.ql.metadata.Hive");
Level oldLevel = logger.getLevel();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
loggerConfig.setLevel(Level.DEBUG);
ctx.updateLoggers();
appender.addToLogger(logger.getName(), Level.DEBUG);
appender.start();
logStr.contains(expectedString));
} finally {
loggerConfig.setLevel(oldLevel);
ctx.updateLoggers();
appender.removeFromLogger(logger.getName());
代码示例来源:origin: Graylog2/graylog2-server
private void addInstrumentedAppender(final MetricRegistry metrics, final Level level) {
final InstrumentedAppender appender = new InstrumentedAppender(metrics, null, null, false);
appender.start();
final LoggerContext context = (LoggerContext) LogManager.getContext(false);
final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration();
config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).addAppender(appender, level, null);
context.updateLoggers(config);
}
内容来源于网络,如有侵权,请联系作者删除!