ch.qos.logback.classic.filter.ThresholdFilter Java Examples

The following examples show how to use ch.qos.logback.classic.filter.ThresholdFilter. 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: SentryUtils.java    From andesite-node with MIT License 6 votes vote down vote up
static void setup(Config config) {
    var client = Sentry.init(config.getString("sentry.dsn"));
    client.setRelease(Version.VERSION);
    client.setTags(parseTags(config.getStringList("sentry.tags")));
    SentryUtils.client = client;
    var loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    var root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    
    var sentryAppender = (SentryAppender) root.getAppender(SENTRY_APPENDER_NAME);
    if(sentryAppender == null) {
        sentryAppender = new SentryAppender();
        sentryAppender.setName(SENTRY_APPENDER_NAME);
        sentryAppender.start();
        
        var warningsOrAboveFilter = new ThresholdFilter();
        warningsOrAboveFilter.setLevel(config.getString("sentry.log-level").toUpperCase());
        warningsOrAboveFilter.start();
        sentryAppender.addFilter(warningsOrAboveFilter);
        
        sentryAppender.setContext(loggerContext);
        root.addAppender(sentryAppender);
    }
}
 
Example #3
Source File: LogAppender.java    From nuls-v2 with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static Appender<ILoggingEvent> createConsoleAppender(Level level){
    ConsoleAppender appender = new ConsoleAppender();
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    appender.setContext(context);
    //这里设置级别过滤器
    LogFilter levelController = new LogFilter();
    ThresholdFilter levelFilter = levelController.getThresholdFilter(level);
    levelFilter.start();
    appender.addFilter(levelFilter);

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    //设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。
    //但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。
    encoder.setContext(context);
    //设置格式
    encoder.setPattern("%d %p [%t] - %msg%n");
    encoder.start();
    //加入下面两个节点
    appender.setEncoder(encoder);
    appender.start();
    return appender;
}
 
Example #4
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 #5
Source File: LogControlImpl.java    From cuba with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
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 #7
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 #8
Source File: LogbackConfiguration.java    From milkman with MIT License 5 votes vote down vote up
private void setupConsoleAppender(LoggerContext loggerContext) {
	ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>();
	ca.setContext(loggerContext);
	ca.setName("STDOUT");
	
	var filter = new ThresholdFilter();
	filter.setLevel("INFO");
	filter.start();
	ca.addFilter(filter);
	
	LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
	encoder.setContext(loggerContext);

	
	// same as
	// PatternLayout layout = new PatternLayout();
	// layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
	TTLLLayout layout = new TTLLLayout();

	layout.setContext(loggerContext);
	layout.start();
	encoder.setLayout(layout);

	
	
	ca.setEncoder(encoder);
	ca.start();

	Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	rootLogger.addAppender(ca);
}
 
Example #9
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 #10
Source File: LogControlImpl.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public void setAppenderThreshold(String appenderName, Level threshold) 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) {
                    ((ThresholdFilter) filter).setLevel(threshold.levelStr);
                    return;
                }
            }
        }
    }
    throw new AppenderThresholdNotSupported(appenderName);
}
 
Example #11
Source File: LogbackConfiguration.java    From milkman with MIT License 4 votes vote down vote up
private void setupFileAppender(LoggerContext loggerContext) {
	RollingFileAppender<ILoggingEvent> fa = new RollingFileAppender<ILoggingEvent>();
	fa.setFile(PlatformUtil.getWritableLocationForFile("errors.log"));
	
	SizeBasedTriggeringPolicy<ILoggingEvent> sizePolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
	sizePolicy.setContext(loggerContext);
	sizePolicy.setMaxFileSize(FileSize.valueOf("10mb"));
	sizePolicy.start();
	fa.setTriggeringPolicy(sizePolicy);
	var policy = new FixedWindowRollingPolicy();
	policy.setMaxIndex(3);
	policy.setMinIndex(1);
	policy.setFileNamePattern("errors.%i.log.zip");
	policy.setParent(fa);
	policy.setContext(loggerContext);
	policy.start();
	fa.setRollingPolicy(policy);
	
	
	fa.setContext(loggerContext);
	fa.setName("FILE");
	
	var filter = new ThresholdFilter();
	filter.setLevel("ERROR");
	filter.start();
	fa.addFilter(filter);

	LayoutWrappingEncoder<ILoggingEvent> encoder = new LayoutWrappingEncoder<ILoggingEvent>();
	encoder.setContext(loggerContext);

	// same as
	// PatternLayout layout = new PatternLayout();
	// layout.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
	PatternLayout layout = new PatternLayout();
	layout.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");

	layout.setContext(loggerContext);
	layout.start();
	encoder.setLayout(layout);
	fa.setEncoder(encoder);

	
	fa.start();

	Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	rootLogger.addAppender(fa);
}
 
Example #12
Source File: LogAppender.java    From nuls-v2 with MIT License 4 votes vote down vote up
/**
 * 通过传入的名字和级别,动态设置appender
 *
 * @param fileName
 * @return
 */
@SuppressWarnings("unchecked")
public static RollingFileAppender getAppender(String fileName, Level level){
    String rootPath = PROJECT_PATH;
    if(!rootPath.endsWith(File.separator)){
        rootPath += File.separator;
    }
    if(fileName.startsWith(File.separator)){
        fileName = fileName.substring(1);
    }
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender appender = new RollingFileAppender();
    /*设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。
    但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。*/
    appender.setContext(context);

    //这里设置级别过滤器
    LogFilter levelController = new LogFilter();
    ThresholdFilter levelFilter = levelController.getThresholdFilter(level);
    levelFilter.start();
    appender.addFilter(levelFilter);

    //设置文件名
    appender.setFile(OptionHelper.substVars(rootPath+fileName + ".log",context));
    appender.setAppend(true);
    appender.setPrudent(false);
    //设置文件创建时间及大小的类
    SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy();
    //文件名格式
    String fp = OptionHelper.substVars(rootPath+ fileName + ".%d{yyyy-MM-dd}.%i.zip",context);
    //最大日志文件大小
    policy.setMaxFileSize(FileSize.valueOf("100MB"));
    //设置文件名模式
    policy.setFileNamePattern(fp);
    //设置保存最近3天的日志
    policy.setMaxHistory(3);
    //总大小限制
    policy.setContext(context);
    policy.setTotalSizeCap(FileSize.valueOf("1GB"));
    //设置父节点是appender
    policy.setParent(appender);
    //设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。
    //但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。
    policy.start();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    //设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。
    //但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。
    encoder.setContext(context);
    //设置格式
    /*encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %replace(%caller{1}){'\\t|Caller.{1}0|\\r\\n', ''} - %msg%n");*/
    encoder.setPattern("%d %p [%t] - %msg%n");
    encoder.start();
    //加入下面两个节点
    appender.setRollingPolicy(policy);
    appender.setEncoder(encoder);
    appender.start();
    return appender;
}
 
Example #13
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 #14
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 #15
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);
}