本文整理汇总了Java中ch.qos.logback.core.Appender类的典型用法代码示例。如果您正苦于以下问题:Java Appender类的具体用法?Java Appender怎么用?Java Appender使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Appender类属于ch.qos.logback.core包,在下文中一共展示了Appender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAppenderThreshold
import ch.qos.logback.core.Appender; //导入依赖的package包/类
@Override
public Level getAppenderThreshold(String appenderName) throws AppenderThresholdNotSupported {
for (Map.Entry<String, Appender> entry : getAllAppenders().entrySet()) {
if (entry.getKey().equals(appenderName)) {
Appender appender = entry.getValue();
for (Object filter : appender.getCopyOfAttachedFiltersList()) {
if (filter instanceof ThresholdFilter) {
try {
Field field = filter.getClass().getDeclaredField("level");
field.setAccessible(true);
return (Level) field.get(filter);
} catch (NoSuchFieldException | IllegalAccessException e) {
log.error("Error getting appender " + appenderName + " level", e);
throw new AppenderThresholdNotSupported(appenderName);
}
}
}
}
}
throw new AppenderThresholdNotSupported(appenderName);
}
示例2: fileAppender
import ch.qos.logback.core.Appender; //导入依赖的package包/类
private Appender<ILoggingEvent> fileAppender(LogbackConfigurator config,
String logFile) {
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
String logPattern = this.patterns.getProperty("file", FILE_LOG_PATTERN);
encoder.setPattern(OptionHelper.substVars(logPattern, config.getContext()));
appender.setEncoder(encoder);
config.start(encoder);
appender.setFile(logFile);
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setFileNamePattern(logFile + ".%i");
appender.setRollingPolicy(rollingPolicy);
rollingPolicy.setParent(appender);
config.start(rollingPolicy);
SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
triggeringPolicy.setMaxFileSize("10MB");
appender.setTriggeringPolicy(triggeringPolicy);
config.start(triggeringPolicy);
config.appender("FILE", appender);
return appender;
}
示例3: setMetricsMarkerLogbackFilter
import ch.qos.logback.core.Appender; //导入依赖的package包/类
private void setMetricsMarkerLogbackFilter(LoggerContext context) {
log.info("Filtering metrics logs from all appenders except the {} appender", LOGSTASH_APPENDER_NAME);
OnMarkerEvaluator onMarkerMetricsEvaluator = new OnMarkerEvaluator();
onMarkerMetricsEvaluator.setContext(context);
onMarkerMetricsEvaluator.addMarker("metrics");
onMarkerMetricsEvaluator.start();
EvaluatorFilter<ILoggingEvent> metricsFilter = new EvaluatorFilter<>();
metricsFilter.setContext(context);
metricsFilter.setEvaluator(onMarkerMetricsEvaluator);
metricsFilter.setOnMatch(FilterReply.DENY);
metricsFilter.start();
for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
for (Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders(); it.hasNext();) {
Appender<ILoggingEvent> appender = it.next();
if (!appender.getName().equals(ASYNC_LOGSTASH_APPENDER_NAME)) {
log.debug("Filter metrics logs from the {} appender", appender.getName());
appender.setContext(context);
appender.addFilter(metricsFilter);
appender.start();
}
}
}
}
示例4: logToConsole
import ch.qos.logback.core.Appender; //导入依赖的package包/类
/**
* Starts or stops logging to the console.
*
* @param start starts or stops logging to the console.
*/
@SuppressWarnings("rawtypes")
public static void logToConsole(boolean start) {
Logger rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
if (rootLogger != null && rootLogger instanceof ch.qos.logback.classic.Logger) {
Appender appender = ((ch.qos.logback.classic.Logger) rootLogger).getAppender(CONSOLE_APPENDER_NAME);
if (appender != null) {
if (start) {
appender.start();
} else {
appender.stop();
}
return;
}
}
logger.warn("Unable to reconfigure Logback console logger (appender name '" + CONSOLE_APPENDER_NAME + "').");
}
示例5: setMetricsMarkerLogbackFilter
import ch.qos.logback.core.Appender; //导入依赖的package包/类
private void setMetricsMarkerLogbackFilter(LoggerContext context) {
log.info("Filtering metrics logs from all appenders except the {} appender", LOGSTASH_APPENDER_NAME);
OnMarkerEvaluator onMarkerMetricsEvaluator = new OnMarkerEvaluator();
onMarkerMetricsEvaluator.setContext(context);
onMarkerMetricsEvaluator.addMarker("metrics");
onMarkerMetricsEvaluator.start();
EvaluatorFilter<ILoggingEvent> metricsFilter = new EvaluatorFilter<>();
metricsFilter.setContext(context);
metricsFilter.setEvaluator(onMarkerMetricsEvaluator);
metricsFilter.setOnMatch(FilterReply.DENY);
metricsFilter.start();
for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
for (Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders(); it.hasNext(); ) {
Appender<ILoggingEvent> appender = it.next();
if (!appender.getName().equals(ASYNC_LOGSTASH_APPENDER_NAME)) {
log.debug("Filter metrics logs from the {} appender", appender.getName());
appender.setContext(context);
appender.addFilter(metricsFilter);
appender.start();
}
}
}
}
示例6: build
import ch.qos.logback.core.Appender; //导入依赖的package包/类
@Override
public Appender<E> build(final LoggerContext context,
final String applicationName,
final LayoutFactory<E> layoutFactory,
final LevelFilterFactory<E> levelFilterFactory,
final AsyncAppenderFactory<E> asyncAppenderFactory) {
final Appender<E> appender = new FifoLogbackAppender<>();
appender.setContext(context);
appender.addFilter(levelFilterFactory.build(threshold));
getFilterFactories().forEach(f -> appender.addFilter(f.build()));
appender.start();
appender.setName(APPENDER_NAME);
return appender;
}
示例7: init
import ch.qos.logback.core.Appender; //导入依赖的package包/类
@BeforeClass
@SuppressWarnings("unchecked")
public static void init() {
loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
appender = new CdpAppender();
appender.setContext(loggerContext);
appender.start();
appender.setName(CdpAppender.class.getName());
Logger logger = loggerContext.getLogger("cdp4j.flow");
logger.addAppender((Appender<ILoggingEvent>) appender);
factory = new Launcher().launch();
session = factory.create();
session.enableConsoleLog();
URL url = TestAll.class.getResource("/session-test.html");
session.navigate(url.toString());
}
示例8: shouldSetConsoleAppenderForServiceAndRequestLogs
import ch.qos.logback.core.Appender; //导入依赖的package包/类
@Test
public void shouldSetConsoleAppenderForServiceAndRequestLogs() {
final String requestLoggerName = "requestlogger";
SecondBaseLogger.setupLoggingStdoutOnly(KEYS, VALUES, requestLoggerName);
final ch.qos.logback.classic.Logger rootLogger
= (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(
org.slf4j.Logger.ROOT_LOGGER_NAME);
final Appender<ILoggingEvent> serviceConsoleAppender = rootLogger.getAppender(
"SERVICECONSOLEAPPENDER");
assertThat(serviceConsoleAppender, notNullValue());
final ch.qos.logback.classic.Logger requestLogger
= (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(requestLoggerName);
final Appender<ILoggingEvent> requestLogConsoleAppender = requestLogger.getAppender(
"REQUESTLOGCONSOLEAPPENDER");
assertThat(requestLogConsoleAppender, notNullValue());
}
示例9: build
import ch.qos.logback.core.Appender; //导入依赖的package包/类
@Override
public Appender<ILoggingEvent> build(LoggerContext context, String s, Layout<ILoggingEvent> layout) {
Preconditions.checkNotNull(context);
WiretapAppender appender = new WiretapAppender();
final PatternLayoutEncoder patternEncoder = new PatternLayoutEncoder();
patternEncoder.setContext(context);
patternEncoder.setPattern("%-5p [%thread] [%d] %c: %m%n%rEx");
patternEncoder.start();
appender.setContext(context);
appender.setName("wiretap-appender");
appender.setEncoder(patternEncoder);
addThresholdFilter(appender, threshold);
appender.start();
return wrapAsync(appender);
}
示例10: build
import ch.qos.logback.core.Appender; //导入依赖的package包/类
public Appender<ILoggingEvent> build(
LoggerContext context,
String applicationName,
LayoutFactory<ILoggingEvent> layoutFactory,
LevelFilterFactory<ILoggingEvent> levelFilterFactory,
AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory
) {
final LoggingEventCompositeJsonEncoder encoder = new LoggingEventCompositeJsonEncoder();
encoder.setContext(context);
encoder.getProviders().addProvider(withPattern("{ \"type\": \"system\" }", new LoggingEventPatternJsonProvider(), context));
encoder.getProviders().addProvider(withName("timestamp", new LoggingEventFormattedTimestampJsonProvider(), context));
encoder.getProviders().addProvider(new LogLevelJsonProvider() );
encoder.getProviders().addProvider(new MessageJsonProvider() );
encoder.getProviders().addProvider(new StackTraceJsonProvider() );
encoder.start();
final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
appender.setContext(context);
appender.setEncoder(encoder);
appender.start();
return wrapAsync(appender, asyncAppenderFactory);
}
示例11: startLogback
import ch.qos.logback.core.Appender; //导入依赖的package包/类
public static void startLogback() {
long startTime = System.nanoTime();
Config config = Configs.DEFAULT.logging();
LogbackConfig logbackConfig = getLogbackConfig(config);
LogbackManager logbackMgr = LogbackManager.INSTANCE;
RollingFileAppender<ILoggingEvent> fileAppender = logbackMgr.createRollingFileAppender(logbackConfig);
ConsoleAppender<ILoggingEvent> consoleAppender = logbackMgr
.createConsoleAppender(APPENDER_CONSOLE, config.getString(KEY_LOG_PATTERN_CONSOLE));
List<Appender<ILoggingEvent>> appenderList = new ArrayList<>();
appenderList.add(consoleAppender);
appenderList.add(fileAppender);
logbackMgr.getAppenders().addAll(appenderList);
LoggerContext context = logbackMgr.getLoggerContext();
initRootLogger(context, consoleAppender, config);
addLoggers(config, appenderList);
addAsyncAppender(config, fileAppender);
context.start();
context.getLogger(LogbackInitializer.class).info(INIT_MSG, elapsedMillis(startTime));
}
示例12: configure
import ch.qos.logback.core.Appender; //导入依赖的package包/类
/**
* Initializes the logger and the requested log level.
*
* @param level Logging level threshold
*/
@SuppressWarnings("unchecked")
public static void configure(final Level level) {
final Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(level);
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(root.getLoggerContext());
encoder.setPattern("%msg%n");
encoder.start();
Iterator<Appender<ILoggingEvent>> iterator = root.iteratorForAppenders();
while (iterator.hasNext()) {
Appender<ILoggingEvent> appender = iterator.next();
if (appender instanceof OutputStreamAppender) {
((OutputStreamAppender) appender).setEncoder(encoder);
}
appender.stop();
appender.start();
}
}
示例13: configureAppender
import ch.qos.logback.core.Appender; //导入依赖的package包/类
private Appender<ILoggingEvent> configureAppender(LoggerContext loggerContext, String name, Config config)
throws ReflectiveOperationException {
List<Object> children = new ArrayList<>();
@SuppressWarnings("unchecked")
Class<Appender<ILoggingEvent>> clazz = (Class<Appender<ILoggingEvent>>) Class
.forName(config.getString("class"));
Appender<ILoggingEvent> appender = this.configureObject(loggerContext, clazz, config, children);
appender.setName(name);
for (Object child : children) {
if (child instanceof RollingPolicy) {
((RollingPolicy) child).setParent((FileAppender<?>) appender);
}
if (child instanceof LifeCycle) {
((LifeCycle) child).start();
}
}
appender.start();
return appender;
}
示例14: buildDailyMaxRollingAppender
import ch.qos.logback.core.Appender; //导入依赖的package包/类
protected Appender buildDailyMaxRollingAppender(String name, String fileName, String pattern, int maxBackupIndex) {
RollingFileAppender appender = new RollingFileAppender();
appender.setContext(loggerContext);
appender.setName(name);
appender.setAppend(true);
appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());
TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
rolling.setContext(loggerContext);
rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd-HH}");
rolling.setMaxHistory(maxBackupIndex);
rolling.setParent(appender);
rolling.start();
appender.setRollingPolicy(rolling);
PatternLayoutEncoder layout = new PatternLayoutEncoder();
layout.setContext(loggerContext);
layout.setPattern(pattern);
layout.setCharset(Charset.forName(getEncoding()));
layout.start();
appender.setEncoder(layout);
// 启动
appender.start();
return appender;
}
示例15: testAppender
import ch.qos.logback.core.Appender; //导入依赖的package包/类
@org.junit.Test
public void testAppender() {
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
Appender mockAppender = mock(Appender.class);
root.addAppender(mockAppender);
Appender<ILoggingEvent> appender = root.getAppender("SNMP");
LOG.info("server error", new RuntimeException("info message"));
LOG.error("server error", new RuntimeException("error message"));
verify(mockAppender,times(2)).doAppend(argThat(new ArgumentMatcher<ILoggingEvent>() {
@Override
public boolean matches(final Object argument) {
return ((LoggingEvent) argument).getFormattedMessage().contains("server error");
}
}));
Assert.assertThat(appender.getClass().getName(), is(equalTo("com.sathwik.logback.appender.SNMPTrapAppender")));
}