本文整理了Java中org.apache.logging.log4j.core.config.Configuration.addAppender()
方法的一些代码示例,展示了Configuration.addAppender()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.addAppender()
方法的具体详情如下:
包路径:org.apache.logging.log4j.core.config.Configuration
类名称:Configuration
方法名:addAppender
暂无
代码示例来源:origin: alibaba/nacos
@Override
public void activateAsync(List<Object[]> args) {
Map<String, Appender> appenders = logger.getAppenders();
if (appenders == null) {
throw new IllegalStateException("Activate async appender failed, no appender exist.");
}
AppenderRef[] refs = new AppenderRef[appenders.size()];
int i = 0;
for (Appender appender : appenders.values()) {
configuration.addAppender(appender);
refs[i++] = AppenderRef.createAppenderRef(appender.getName(), null, null);
}
AsyncAppender.Builder builder = AsyncAppender.newBuilder()
.setName(productName + "." + logger.getName() + ".AsyncAppender")
.setConfiguration(configuration)
.setAppenderRefs(refs)
.setBlockingQueueFactory(ArrayBlockingQueueFactory.<LogEvent>createFactory());
invokeMethod(builder, args);
AsyncAppender asyncAppender = builder.build();
asyncAppender.start();
removeAllAppenders(logger);
logger.addAppender(asyncAppender);
setProductName(productName);
}
代码示例来源: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: org.apache.logging.log4j/log4j-core
/**
* Tests that you can add an output stream appender dynamically.
*/
private void addAppender(final OutputStream outputStream, final String outputStreamName) {
final LoggerContext context = LoggerContext.getContext(false);
final Configuration config = context.getConfiguration();
final PatternLayout layout = PatternLayout.createDefaultLayout(config);
final Appender appender = OutputStreamAppender.createAppender(layout, null, outputStream, outputStreamName, false, true);
appender.start();
config.addAppender(appender);
ConfigurationTestUtils.updateLoggers(appender, config);
}
代码示例来源:origin: org.apache.logging.log4j/log4j-core
private void addAppender(final Writer writer, final String writerName) {
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, writerName, false, true);
appender.start();
config.addAppender(appender);
ConfigurationTestUtils.updateLoggers(appender, config);
}
代码示例来源:origin: apache/ignite
/**
* Creates console appender with some reasonable default logging settings.
*
* @return Logger with auto configured console appender.
*/
public Logger createConsoleLogger() {
// from http://logging.apache.org/log4j/2.x/manual/customconfig.html
final LoggerContext ctx = impl.getContext();
final Configuration cfg = ctx.getConfiguration();
PatternLayout.Builder builder = PatternLayout.newBuilder()
.withPattern("%d{ISO8601}][%-5p][%t][%c{1}] %m%n")
.withCharset(Charset.defaultCharset())
.withAlwaysWriteExceptions(false)
.withNoConsoleNoAnsi(false);
PatternLayout layout = builder.build();
ConsoleAppender.Builder consoleAppenderBuilder = ConsoleAppender.newBuilder()
.withName(CONSOLE_APPENDER)
.withLayout(layout);
ConsoleAppender consoleApp = consoleAppenderBuilder.build();
consoleApp.start();
cfg.addAppender(consoleApp);
cfg.getRootLogger().addAppender(consoleApp, Level.TRACE, null);
ctx.updateLoggers(cfg);
return ctx.getRootLogger();
}
代码示例来源: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
config.addAppender(appender);
final AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
final AppenderRef[] refs = new AppenderRef[] {ref};
代码示例来源:origin: mulesoft/mule
private void doAddAppender(LoggerContext context, Appender appender) {
appender.start();
context.getConfiguration().addAppender(appender);
getRootLogger(context).addAppender(appender, Level.ALL, null);
}
代码示例来源:origin: mulesoft/mule
@Test
public void noAppendersForMutedApplication() throws Exception {
when(context.isArtifactClassloader()).thenReturn(true);
ArtifactDescriptor descriptor = mock(ArtifactDescriptor.class);
Properties properties = new Properties();
properties.setProperty(MULE_MUTE_APP_LOGS_DEPLOYMENT_PROPERTY, "true");
when(descriptor.getDeploymentProperties()).thenReturn(Optional.of(properties));
when(context.getArtifactDescriptor()).thenReturn(descriptor);
contextConfigurer.update(context);
verify(context.getConfiguration(), never()).addAppender(any(Appender.class));
}
代码示例来源:origin: mulesoft/mule
@Override
public void run() {
LoggerConfig rootLogger = ((AbstractConfiguration) context.getConfiguration()).getRootLogger();
Collection<Appender> appenders = new ArrayList<>();
appenders.add(ConsoleAppender.createAppender(mock(Layout.class), null, null, "Console", null, null));
when(rootLogger.getAppenders().values()).thenReturn(appenders);
contextConfigurer.configure(context);
verify(context.getConfiguration(), never()).addAppender(any(ConsoleAppender.class));
verify(rootLogger, never()).addAppender(any(ConsoleAppender.class), same(Level.INFO), any(Filter.class));
}
});
代码示例来源:origin: mulesoft/mule
@Override
public void run() {
contextConfigurer.update(context);
ArgumentCaptor<ConsoleAppender> appenderCaptor = ArgumentCaptor.forClass(ConsoleAppender.class);
verify(context.getConfiguration()).addAppender(appenderCaptor.capture());
Appender forcedConsoleAppender = appenderCaptor.getValue();
assertThat(forcedConsoleAppender, notNullValue());
assertThat(forcedConsoleAppender.getName(), equalTo(FORCED_CONSOLE_APPENDER_NAME));
assertThat(forcedConsoleAppender.isStarted(), is(true));
LoggerConfig rootLogger = ((AbstractConfiguration) context.getConfiguration()).getRootLogger();
verify(rootLogger).addAppender(forcedConsoleAppender, Level.ALL, null);
}
});
代码示例来源:origin: mulesoft/mule
@Test
public void perAppDefaultAppender() throws Exception {
when(context.isArtifactClassloader()).thenReturn(true);
when(context.getArtifactDescriptor().getDeploymentProperties()).thenReturn(empty());
contextConfigurer.update(context);
ArgumentCaptor<RollingFileAppender> appenderCaptor = ArgumentCaptor.forClass(RollingFileAppender.class);
verify(context.getConfiguration()).addAppender(appenderCaptor.capture());
Appender perAppAppender = appenderCaptor.getValue();
assertThat(perAppAppender, notNullValue());
assertThat(perAppAppender.getName(), equalTo(PER_APP_FILE_APPENDER_NAME));
assertThat(perAppAppender.isStarted(), is(true));
String filePattern = ClassUtils.getFieldValue(perAppAppender, FILE_PATTERN_PROPERTY, true);
String filePatternTemplate = filePattern.substring(filePattern.lastIndexOf('/') + 1);
String filePatternTemplateDateSuffix = filePatternTemplate.substring(filePatternTemplate.lastIndexOf('.') + 1);
assertThat(filePatternTemplateDateSuffix, equalTo(FILE_PATTERN_TEMPLATE_DATE_SECTION));
LoggerConfig rootLogger = context.getConfiguration().getRootLogger();
verify(rootLogger).addAppender(perAppAppender, Level.ALL, null);
}
代码示例来源: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: Meituan-Dianping/Zebra
ZebraRolloverStrategy.createStrategy("30", "1", null, Deflater.DEFAULT_COMPRESSION + "", config), layout,
fileInfoFilter, "false", null, null, config);
config.addAppender(fileInfoAppender);
fileInfoAppender.start();
AppenderRef fileInfoRef = AppenderRef.createAppenderRef("FileInfo", null, fileInfoFilter);
config.addAppender(consoleErrorAppender);
consoleErrorAppender.start();
Appender consoleWarnAppender = ConsoleAppender.createAppender(layout, consoleWarnFilter, "SYSTEM_OUT",
"ConsoleWarn", "false", "false");
config.addAppender(consoleWarnAppender);
consoleWarnAppender.start();
AppenderRef consoleWarnAppenderRef = AppenderRef.createAppenderRef("ConsoleWarn", Level.WARN, consoleWarnFilter);
代码示例来源:origin: mulesoft/mule
@Before
public void before() {
context = getDefaultContext();
testAppender = new TestAppender(TEST_APPENDER, null, null);
context.getConfiguration().addAppender(testAppender);
LoggerConfig loggerConfig =
AsyncLoggerConfig.createLogger("false", LEVEL.name(), CATEGORY, "true",
new AppenderRef[] {AppenderRef.createAppenderRef(TEST_APPENDER, null, null)}, null,
context.getConfiguration(), null);
loggerConfig.addAppender(testAppender, null, null);
context.getConfiguration().addLogger(CATEGORY, loggerConfig);
context.getConfiguration().start();
context.updateLoggers();
}
代码示例来源:origin: org.mule.munit/munit-runner
private static void doAddAppender(Configuration logConfiguration, Appender appender) {
appender.start();
logConfiguration.addAppender(appender);
LoggerConfig lc = ((AbstractConfiguration) logConfiguration).getRootLogger();
lc.addAppender(appender, Level.INFO, null);
}
}
代码示例来源:origin: org.mule.modules/mule-module-launcher
private void doAddAppender(LoggerContext context, Appender appender)
{
appender.start();
context.getConfiguration().addAppender(appender);
getRootLogger(context).addAppender(appender, Level.ALL, null);
}
代码示例来源:origin: org.apache.pulsar/pulsar-functions-instance
private void addLogTopicHandler() {
if (logAppender == null) return;
LoggerContext context = LoggerContext.getContext(false);
Configuration config = context.getConfiguration();
config.addAppender(logAppender);
for (final LoggerConfig loggerConfig : config.getLoggers().values()) {
loggerConfig.addAppender(logAppender, null, null);
}
config.getRootLogger().addAppender(logAppender, null, null);
}
代码示例来源:origin: org.infinispan/infinispan-core
@BeforeClass
public void installInMemoryAppender() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
config.addAppender(new InMemoryAppender());
}
代码示例来源:origin: org.infinispan/infinispan-commons-test
public void install() {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration config = loggerContext.getConfiguration();
this.start();
config.addAppender(this);
AppenderRef ref = AppenderRef.createAppenderRef(this.getName(), level, null);
AppenderRef[] refs = new AppenderRef[] {ref};
LoggerConfig loggerConfig = LoggerConfig.createLogger(true, level, category, null, refs, null, config, null);
loggerConfig.addAppender(this, null, null);
config.addLogger(category, loggerConfig);
loggerContext.updateLoggers();
}
内容来源于网络,如有侵权,请联系作者删除!