Java Code Examples for ch.qos.logback.core.rolling.RollingFileAppender#setPrudent()

The following examples show how to use ch.qos.logback.core.rolling.RollingFileAppender#setPrudent() . 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: Minister.java    From GOAi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 初始化日志
 */
private Logger initLog(Integer id) {
    // 实例化log,每个实例存放地方不一致
    ch.qos.logback.classic.Logger logger = ((ch.qos.logback.classic.Logger)
            LoggerFactory.getLogger(this.secretary.strategyName + "-" + id));

    LoggerContext context = logger.getLoggerContext();

    TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
    policy.setFileNamePattern(OptionHelper.substVars(
            "logs/past/" + id + "/%d{yyyy-MM-dd}.log.gz", context));
    policy.setMaxHistory(31);
    policy.setContext(context);

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %5level - [%thread] %logger : %msg%n");

    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    appender.setContext(context);
    appender.setName(this.secretary.strategyName + "-" + id);
    appender.setFile(OptionHelper.substVars("logs/" + id + "/log.log", context));
    appender.setAppend(true);
    // 同一文件多输入完整检查
    appender.setPrudent(false);
    appender.setRollingPolicy(policy);
    appender.setEncoder(encoder);
    policy.setParent(appender);

    policy.start();
    encoder.start();
    appender.start();

    logger.setLevel(Level.INFO);
    // 终端输出
    logger.setAdditive(true);
    logger.addAppender(appender);

    return logger;
}
 
Example 2
Source File: AppenderUtils.java    From singer with Apache License 2.0 5 votes vote down vote up
/**
 * Create the basic thrift appender which logs to a file
 * and rolls the file when it exceeds a certain size.
 *
 * @param basePath base directory the files are under.
 * @param topic the topic name for the current appender.
 * @param rotateThresholdKBytes threshold in kilobytes to rotate after.
 * @param context the logback context.
 */
public static Appender<LogMessage> createFileRollingThriftAppender(
    File basePath,
    String topic,
    long rotateThresholdKBytes,
    Context context,
    int maxRetentionHours) {
  RollingFileAppender<LogMessage> appender = new RollingFileAppender<LogMessage>();
  appender.setContext(context);
  appender.setAppend(true);
  appender.setPrudent(false);

  LogMessageEncoder encoder = new LogMessageEncoder();
  appender.setEncoder(encoder);
  appender.setFile(basePath + PATH_SEP + topic);

  TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
  policy.setMaxHistory(maxRetentionHours);
  policy.setFileNamePattern(basePath + PATH_SEP + topic + ".%d{yyyy-MM-dd-HH}.%i");
  policy.setCleanHistoryOnStart(false);
  policy.setContext(context);
  policy.setParent(appender);

  // Also impose a max size per file policy.
  SizeAndTimeBasedFNATP fnatp = new SizeAndTimeBasedFNATP();
  fnatp.setContext(context);
  fnatp.setTimeBasedRollingPolicy(policy);
  fnatp.setMaxFileSize(FileSize.valueOf(String.format("%sKB", rotateThresholdKBytes)));

  policy.setTimeBasedFileNamingAndTriggeringPolicy(fnatp);
  appender.setRollingPolicy(policy);
  appender.setTriggeringPolicy(policy);

  policy.start();
  appender.start();

  return appender;
}
 
Example 3
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 4
Source File: LogbackUtil.java    From summerframework with Apache License 2.0 2 votes vote down vote up
public RollingFileAppender getAppender(String name, Level level, String fileName) {
    DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
    LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();

    RollingFileAppender appender = new RollingFileAppender();

    LevelFilter levelFilter = getLevelFilter(level);
    levelFilter.start();
    appender.addFilter(levelFilter);

    appender.setContext(context);

    appender.setName(name);

    appender.setFile(OptionHelper.substVars("/opt/app/logs/" + fileName + ".json", context));

    appender.setAppend(true);

    appender.setPrudent(false);

    SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy();

    String fp = OptionHelper.substVars(
        "/opt/app/logs/histroy" + fileName + format.format(new Date()) + "/.%d{yyyy-MM-dd}.%i.json", context);

    policy.setMaxFileSize(FileSize.valueOf("128MB"));

    policy.setFileNamePattern(fp);

    policy.setMaxHistory(7);

    policy.setTotalSizeCap(FileSize.valueOf("32GB"));

    policy.setParent(appender);

    policy.setContext(context);
    policy.start();

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();

    encoder.setContext(context);

    encoder.setPattern("%d %p (%file:%line\\)- %m%n");
    encoder.start();

    appender.setRollingPolicy(policy);
    appender.setEncoder(encoder);
    appender.start();
    return appender;
}