本文整理汇总了Java中org.apache.logging.log4j.core.Appender类的典型用法代码示例。如果您正苦于以下问题:Java Appender类的具体用法?Java Appender怎么用?Java Appender使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Appender类属于org.apache.logging.log4j.core包,在下文中一共展示了Appender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
@Setup
public void start() {
counter = 0;
if (log == null) {
System.setProperty(
"Log4jContextSelector",
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
LoggerContext context = LoggerContext.getContext();
log = context.getLogger("Log4j2");
log.setAdditive(false);
ArrayList<Appender> list = new ArrayList<Appender>();
list.addAll(log.getAppenders().values());
for (Appender a : list) {
log.removeAppender(a);
}
}
}
示例2: registerLogFileTailThreads
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
private void registerLogFileTailThreads() throws IOException {
final Collection<String> outputFileNames = new HashSet<>();
final Collection<Appender> loggerAppenders = this.loggerContext.getConfiguration().getAppenders().values();
loggerAppenders.forEach(appender -> {
if (appender instanceof FileAppender) {
outputFileNames.add(((FileAppender) appender).getFileName());
} else if (appender instanceof RandomAccessFileAppender) {
outputFileNames.add(((RandomAccessFileAppender) appender).getFileName());
} else if (appender instanceof RollingFileAppender) {
outputFileNames.add(((RollingFileAppender) appender).getFileName());
} else if (appender instanceof MemoryMappedFileAppender) {
outputFileNames.add(((MemoryMappedFileAppender) appender).getFileName());
} else if (appender instanceof RollingRandomAccessFileAppender) {
outputFileNames.add(((RollingRandomAccessFileAppender) appender).getFileName());
}
});
outputFileNames.forEach(s -> {
final Tailer t = new Tailer(new File(s), new LogTailerListener(), 100, false, true);
final Thread thread = new Thread(t);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setName(s);
thread.start();
});
}
示例3: Log4j2LoggerAdapter
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
public Log4j2LoggerAdapter() {
try {
org.apache.logging.log4j.Logger logger = LogManager.getLogger();
if (logger != null) {
Map<String, Appender> appenderMap = ((org.apache.logging.log4j.core.Logger) logger).getAppenders();
for (Map.Entry<String, Appender> entry : appenderMap.entrySet()) {
if (entry.getValue() instanceof FileAppender) {
String filename = ((FileAppender) entry.getValue()).getFileName();
file = new File(filename);
break;
}
}
}
} catch (Throwable t) {
}
}
示例4: registerParent
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
protected final void registerParent(Class<? extends BaseLog4j2Configuration> clazz){
BaseLog4j2Configuration configuration;
try{
configuration = clazz.newInstance();
}catch(InstantiationException | IllegalAccessException e){
throw new RuntimeException(e);
}
for(Appender appender : configuration.getAppenders()){
addAppender(appender);
}
for(LoggerConfig loggerConfig : configuration.getLoggerConfigs()){
addLoggerConfig(loggerConfig.getName(), loggerConfig.getLevel(), loggerConfig.isAdditive(), loggerConfig
.getAppenders().values());
}
for(Filter filter : configuration.getFilters()){
addFilter(filter);
}
}
示例5: watch
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
public void watch(Class<?> loggerClass, Level level) {
this.loggerClass = loggerClass;
Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) {
@Override
public void append(LogEvent event) {
logEvents.add(event);
}
};
appender.start();
final LoggerContext ctx = getLoggerContext();
LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName());
oldLevel = loggerConfig.getLevel();
loggerConfig.setLevel(level);
loggerConfig.addAppender(appender, level, null);
ctx.updateLoggers();
}
示例6: removeAndRestoreConsoleAppenderShouldAffectRootLogger
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
@Test
public void removeAndRestoreConsoleAppenderShouldAffectRootLogger() {
LogService.reconfigure();
assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation())
.isTrue();
final Logger rootLogger = (Logger) LogService.getRootLogger();
// assert "Console" is present for ROOT
Appender appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNotNull();
LogService.removeConsoleAppender();
// assert "Console" is not present for ROOT
appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNull();
LogService.restoreConsoleAppender();
// assert "Console" is present for ROOT
appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNotNull();
}
示例7: getActiveLogFilePaths
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
/**
* We cannot presume that the default file name/location setting won't be changed by the user.
* Therefore, we should be able to retrieve that info from the underlying logging mechanism
* by iterating over appenders.
*/
private List<Path> getActiveLogFilePaths() {
LoggerContextFactory factory = LogManager.getFactory();
ContextSelector selector = ((Log4jContextFactory) factory).getSelector();
List<Path> fileNameList = new ArrayList<>();
for (LoggerContext ctx : selector.getLoggerContexts()) {
for (Appender appender : ctx.getConfiguration().getAppenders().values()) {
String fileName = extractFileName(appender);
if (fileName != null) {
fileNameList.add(Paths.get(fileName));
}
}
}
return fileNameList;
}
示例8: extractFileName
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
/**
* File appender types do not share a "write-to-file" superclass.
*/
private String extractFileName(Appender a) {
if (a instanceof FileAppender) {
return ((FileAppender) a).getFileName();
} else if (a instanceof RollingFileAppender) {
return ((RollingFileAppender) a).getFileName();
} else if (a instanceof RollingRandomAccessFileAppender) {
return ((RollingRandomAccessFileAppender) a).getFileName();
} else if (a instanceof RandomAccessFileAppender) {
return ((RandomAccessFileAppender) a).getFileName();
} else if (a instanceof MemoryMappedFileAppender) {
return ((MemoryMappedFileAppender) a).getFileName();
} else {
return null;
}
}
示例9: deliversToAppenderRef
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
@Test
public void deliversToAppenderRef() {
// given
Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
Configuration configuration = mock(Configuration.class);
String testAppenderRef = "testAppenderRef";
when(configuration.getAppender(testAppenderRef)).thenReturn(appender);
FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration);
String failedMessage = "test failed message";
// when
failoverPolicy.deliver(failedMessage);
// then
verify(appender, times(1)).append(any(LogEvent.class));
}
示例10: resolvesAppenderRefOnlyOnce
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
@Test
public void resolvesAppenderRefOnlyOnce() {
// given
Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
Configuration configuration = mock(Configuration.class);
String testAppenderRef = "testAppenderRef";
when(configuration.getAppender(testAppenderRef)).thenReturn(appender);
FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration);
String failedMessage = "test failed message";
// when
failoverPolicy.deliver(failedMessage);
failoverPolicy.deliver(failedMessage);
// then
verify(configuration, times(1)).getAppender(anyString());
verify(appender, times(2)).append(any(LogEvent.class));
}
示例11: throwsExceptionOnUnresolvedAppender
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
@Test(expected = ConfigurationException.class)
public void throwsExceptionOnUnresolvedAppender() {
// given
Appender appender = mock(Appender.class);
when(appender.isStarted()).thenReturn(true);
Configuration configuration = mock(Configuration.class);
String testAppenderRef = "testAppenderRef";
when(configuration.getAppender(testAppenderRef)).thenReturn(null);
FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration);
String failedMessage = "test failed message";
// when
failoverPolicy.deliver(failedMessage);
}
示例12: testLogReconfiguration
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
/**
* This test asserts that after re-configuration of the log engine the total appenders are
* different. The 'comparison' will be done between:
*
* - default config (src/main/resources/log4j2.xml)
* - alternative config (src/test/resources/xml/config-for-LogManagerTest.xml)
*/
@Test (groups = { GROUP_CHANGE_CONFIG }, description = "Validate the numbers of Appenders associated with the LoggerContext")
public void testLogReconfiguration() {
LOGGER.debug("About to test re-configuration of the logger context");
final String pathToAlternativeLogConfig = "src/test/resources/config/log/config-for-LogManagerTest.xml";
LoggerContext loggerContext = LoggerContext.getContext(false);
Configuration configuration = loggerContext.getConfiguration();
Map<String, Appender> appenderMap = configuration.getAppenders();
Assert.assertTrue(appenderMap.size() == 1, "Expected 1 appender");
Assert.assertTrue(appenderMap.containsKey("console"));
LogManager.initializeLogging(pathToAlternativeLogConfig);
// Refresh reference of configuration as it changed
configuration = loggerContext.getConfiguration();
appenderMap = configuration.getAppenders();
Assert.assertTrue(appenderMap.size() == 2, "Expected 2 appenders");
Assert.assertTrue(appenderMap.containsKey("console"));
Assert.assertTrue(appenderMap.containsKey("filterAppender"));
}
示例13: onDisable
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
@Override
public void onDisable() {
//restore the old format
Appender terminalAppender = CommonLogInstaller.getTerminalAppender(TERMINAL_NAME);
Logger rootLogger = ((Logger) LogManager.getRootLogger());
ColorPluginAppender colorPluginAppender = null;
for (Appender value : rootLogger.getAppenders().values()) {
if (value instanceof ColorPluginAppender) {
colorPluginAppender = (ColorPluginAppender) value;
break;
}
}
if (colorPluginAppender != null) {
rootLogger.removeAppender(terminalAppender);
rootLogger.addAppender(colorPluginAppender.getOldAppender());
}
try {
CommonLogInstaller.setLayout(oldLayout, terminalAppender);
} catch (ReflectiveOperationException ex) {
getLogger().log(Level.WARNING, "Cannot revert log format", ex);
}
}
示例14: StandaloneLoggerConfiguration
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
/**
* Constructor to create the default configuration.
*/
public StandaloneLoggerConfiguration(ConfigurationSource source) {
super(source);
setName(CONFIG_NAME);
final Appender appender = StandaloneLogEventAppender.createAppender("StandaloneLogAppender", 1000);
appender.start();
addAppender(appender);
final LoggerConfig root = getRootLogger();
root.addAppender(appender, null, null);
final String levelName = PropertiesUtil.getProperties().getStringProperty(DEFAULT_LEVEL);
final Level level = levelName != null && Level.valueOf(levelName) != null ?
Level.valueOf(levelName) : Level.ALL;
root.setLevel(level);
}
示例15: configureLogging
import org.apache.logging.log4j.core.Appender; //导入依赖的package包/类
protected void configureLogging() {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, config, null, null,
true, false, null, null);
Appender appender = FileAppender.createAppender(workDir + "/logs/camel-standalone.log", "false", "false", "File", "true",
"false", "false", "4000", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
AppenderRef[] refs = new AppenderRef[] {ref};
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "StandaloneFileLoggerConfig",
"true", refs, null, config, null );
loggerConfig.addAppender(appender, null, null);
config.addLogger("StandaloneFileLoggerConfig", loggerConfig);
ctx.updateLoggers();
}