Java Code Examples for ch.qos.logback.classic.filter.ThresholdFilter#setLevel()

The following examples show how to use ch.qos.logback.classic.filter.ThresholdFilter#setLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DiagnosticLogger.java    From synopsys-detect with Apache License 2.0 7 votes vote down vote up
private FileAppender<ILoggingEvent> addAppender(final String file) {
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final PatternLayoutEncoder ple = new PatternLayoutEncoder();

    ple.setPattern("%date %level [%file:%line] %msg%n");
    ple.setContext(lc);
    ple.start();

    final FileAppender<ILoggingEvent> appender;
    appender = new FileAppender<>();
    appender.setFile(file);
    appender.setEncoder(ple);
    appender.setContext(lc);
    final ThresholdFilter levelFilter = new ThresholdFilter();
    levelFilter.setLevel(this.level.levelStr);
    levelFilter.start();
    appender.addFilter(levelFilter);

    appender.start();

    final ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    logbackLogger.addAppender(appender);

    return appender;
}
 
Example 2
Source File: SentryConfiguration.java    From Lavalink with MIT License 6 votes vote down vote up
private static synchronized SentryAppender getSentryLogbackAppender() {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    ch.qos.logback.classic.Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);

    SentryAppender sentryAppender = (SentryAppender) root.getAppender(SENTRY_APPENDER_NAME);
    if (sentryAppender == null) {
        sentryAppender = new SentryAppender();
        sentryAppender.setName(SENTRY_APPENDER_NAME);

        ThresholdFilter warningsOrAboveFilter = new ThresholdFilter();
        warningsOrAboveFilter.setLevel(Level.WARN.levelStr);
        warningsOrAboveFilter.start();
        sentryAppender.addFilter(warningsOrAboveFilter);

        sentryAppender.setContext(loggerContext);
        root.addAppender(sentryAppender);
    }
    return sentryAppender;
}
 
Example 3
Source File: LoggingConfigurator.java    From logging-java with Apache License 2.0 6 votes vote down vote up
/**
 * Add a sentry appender.
 * @param dsn the sentry dsn to use (as produced by the sentry webinterface).
 * @param logLevelThreshold the threshold for log events to be sent to sentry.
 * @return the configured sentry appender.
 */
public static SentryAppender addSentryAppender(final String dsn, Level logLevelThreshold) {
  final Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

  final LoggerContext context = rootLogger.getLoggerContext();

  SentryAppender appender = new SentryAppender();
  appender.setDsn(dsn);

  appender.setContext(context);
  ThresholdFilter levelFilter = new ThresholdFilter();
  levelFilter.setLevel(logLevelThreshold.logbackLevel.toString());
  levelFilter.start();
  appender.addFilter(levelFilter);

  appender.start();

  rootLogger.addAppender(appender);

  return appender;
}
 
Example 4
Source File: DiagnosticLogSystem.java    From synopsys-detect with Apache License 2.0 5 votes vote down vote up
private void restrictConsoleToDebug() {
    for (final Iterator<Appender<ILoggingEvent>> it = DiagnosticLogUtil.getRootLogger().iteratorForAppenders(); it.hasNext(); ) {
        final Appender appender = it.next();
        if (appender.getName() != null && appender.getName().equals("CONSOLE")) {
            final ThresholdFilter levelFilter = new ThresholdFilter();
            levelFilter.setLevel(Level.DEBUG.levelStr);
            levelFilter.start();
            appender.addFilter(levelFilter);
        }
    }
}
 
Example 5
Source File: SpacedLogbackSystem.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
protected Appender<ILoggingEvent> fileAppender(Space space) {
    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    String logPattern = this.patterns.getProperty("logging.pattern.file", FILE_LOG_PATTERN);
    encoder.setPattern(OptionHelper.substVars(logPattern, context));
    encoder.setCharset(DEFAULT_CHARSET);
    appender.setEncoder(encoder);
    start(encoder);

    // parse path and file
    // first consider spec.path, second, default_spec.path, third logging.path
    LogFile logFile = LogFile.get(patterns);
    Properties defaultProperties = new Properties();
    if (logFile != null) {
        logFile.applyTo(defaultProperties);
    }
    String path = space.getSpec().getPath() != null ? space.getSpec().getPath() :
            space.getDefaultSpec().getPath() != null ? space.getDefaultSpec().getPath() :
                    defaultProperties.contains(LoggingSystemProperties.LOG_PATH)
                            ? defaultProperties.getProperty(LoggingSystemProperties.LOG_PATH) :
                            DEFAULT_PATH;
    path = patterns.resolvePlaceholders(path);
    String file = space.getSpec().getFile() != null
            ? fileName(space.getSpec().getFile()) : fileName(space.getName());
    file = patterns.resolvePlaceholders(file);
    appender.setFile(path + "/" + file);
    setRollingPolicy(appender, space, path, file);

    //  threshold config
    ThresholdFilter thresholdFilter = new ThresholdFilter();
    if (space.getSpec().getThreshold() != null) {
        thresholdFilter.setLevel(space.getSpec().getThreshold());
        start(thresholdFilter);
        appender.addFilter(thresholdFilter);
    }

    appender("SPACE-" + space.getName(), appender);
    return appender;
}
 
Example 6
Source File: LogFilter.java    From nuls-v2 with MIT License 4 votes vote down vote up
/**
 * 通过level设置过滤器,小于该level的日志文件都打印
 * @param level
 * @return
 * */
public ThresholdFilter getThresholdFilter(Level level){
    ThresholdFilter thresholdFilter = new ThresholdFilter();
    thresholdFilter.setLevel(level.levelStr);
    return thresholdFilter;
}
 
Example 7
Source File: LogbackFactory.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
private static void addThresholdFilter(FilterAttachable<ILoggingEvent> appender, Level threshold) {
    final ThresholdFilter filter = new ThresholdFilter();
    filter.setLevel(threshold.toString());
    filter.start();
    appender.addFilter(filter);
}
 
Example 8
Source File: LogbackFactory.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
private static void addThresholdFilter(FilterAttachable<ILoggingEvent> appender, Level threshold) {
    final ThresholdFilter filter = new ThresholdFilter();
    filter.setLevel(threshold.toString());
    filter.start();
    appender.addFilter(filter);
}