Java Code Examples for ch.qos.logback.core.util.FileSize

The following examples show how to use ch.qos.logback.core.util.FileSize. These examples are extracted from open source projects. 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
private void setMaxFileSize(RollingFileAppender<ILoggingEvent> appender, LogbackConfigurator config) {
	SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
	try {
		//
		// Start - for customized
		//
		String maxFileSize = this.logging.getProperty("policy.maxFileSize", "10MB");
		triggeringPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
		// End - for customized

	} catch (NoSuchMethodError ex) {
		// Logback < 1.1.8 used String configuration
		Method method = ReflectionUtils.findMethod(SizeBasedTriggeringPolicy.class, "setMaxFileSize", String.class);
		ReflectionUtils.invokeMethod(method, triggeringPolicy, "10MB");
	}
	appender.setTriggeringPolicy(triggeringPolicy);
	config.start(triggeringPolicy);
}
 
Example 2
Source Project: singer   Source File: AppenderUtils.java    License: 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 Project: Jpom   Source File: AutoBackLog.java    License: MIT License 5 votes vote down vote up
@PreLoadMethod
private static void startAutoBackLog() {
    if (projectInfoService == null) {
        projectInfoService = SpringUtil.getBean(ProjectInfoService.class);
    }
    // 获取cron 表达式
    String cron = StrUtil.emptyToDefault(AgentExtConfigBean.getInstance().autoBackConsoleCron, "none");
    if ("none".equalsIgnoreCase(cron.trim())) {
        DefaultSystemLog.getLog().info("没有配置自动备份控制台日志表达式");
        return;
    }
    String size = StrUtil.emptyToDefault(AgentExtConfigBean.getInstance().autoBackSize, "50MB");
    MAX_SIZE = FileSize.valueOf(size.trim());
    //
    CronUtil.schedule(ID, cron, () -> {
        try {
            List<ProjectInfoModel> list = projectInfoService.list();
            if (list == null) {
                return;
            }
            list.forEach(projectInfoModel -> {
                checkProject(projectInfoModel, null);
                //
                List<ProjectInfoModel.JavaCopyItem> javaCopyItemList = projectInfoModel.getJavaCopyItemList();
                if (javaCopyItemList == null) {
                    return;
                }
                javaCopyItemList.forEach(javaCopyItem -> checkProject(projectInfoModel, javaCopyItem));
            });
        } catch (Exception e) {
            DefaultSystemLog.getLog().error("定时备份日志失败", e);
        }
    });
    CronUtils.start();
}
 
Example 4
Source Project: spring-cloud-formula   Source File: SpacedLogbackSystem.java    License: Apache License 2.0 5 votes vote down vote up
private void setTotalSizeCap(SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy,
                             String totalSizeCap) {
    try {
        rollingPolicy.setContext(context);
        rollingPolicy.setTotalSizeCap(FileSize.valueOf(totalSizeCap));
    } catch (NoSuchMethodError ex) {
        // Logback < 1.1.8 used String configuration
        // copy from setMaxFileSize
        Method method = ReflectionUtils.findMethod(
                SizeAndTimeBasedRollingPolicy.class, "setTotalSizeCap", String.class);
        ReflectionUtils.invokeMethod(method, rollingPolicy, totalSizeCap);
    }
}
 
Example 5
Source Project: spring-cloud-formula   Source File: SpacedLogbackSystem.java    License: Apache License 2.0 5 votes vote down vote up
private void setMaxFileSize(SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy,
                            String maxFileSize) {
    try {
        rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
    } catch (NoSuchMethodError ex) {
        // Logback < 1.1.8 used String configuration
        Method method = ReflectionUtils.findMethod(
                SizeAndTimeBasedRollingPolicy.class, "setMaxFileSize", String.class);
        ReflectionUtils.invokeMethod(method, rollingPolicy, maxFileSize);
    }
}
 
Example 6
Source Project: qpid-broker-j   Source File: AppenderUtils.java    License: Apache License 2.0 5 votes vote down vote up
public SizeTriggeringPolicy(boolean isRollOnRestart, String maxFileSize)
{
    _rollOnRestart = isRollOnRestart;
    _maxFileSize = FileSize.valueOf(maxFileSize);
    setMaxFileSize(_maxFileSize);

}
 
Example 7
Source Project: gocd   Source File: LogHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void rollingPolicyForAppender(RollingFileAppender rollingFileAppender, String maxFileSize, String totalSizeCap, int maxHistory) {
    SizeAndTimeBasedRollingPolicy rollingPolicy = new SizeAndTimeBasedRollingPolicy();
    rollingPolicy.setContext(LOGGER_CONTEXT);
    rollingPolicy.setMaxHistory(maxHistory);
    rollingPolicy.setMaxFileSize(FileSize.valueOf(maxFileSize));
    rollingPolicy.setTotalSizeCap(FileSize.valueOf(totalSizeCap));
    rollingPolicy.setFileNamePattern(rollingFileAppender.rawFileProperty() + ".%d{yyyy-MM-dd}.%i.gz");
    rollingPolicy.setParent(rollingFileAppender);
    rollingFileAppender.setRollingPolicy(rollingPolicy);
    rollingPolicy.start();
}
 
Example 8
@BeforeEach
void setUp() {
    artifactsDir = spy(new File("."));
    when(artifactsDir.getUsableSpace()).thenReturn(FileSize.valueOf("1GB").getSize());
    GoConfigService goConfigService = mock(GoConfigService.class);
    when(goConfigService.artifactsDir()).thenReturn(artifactsDir);

    response = new MockHttpServletResponse();
    ArtifactsDirHolder artifactsDirHolder = new ArtifactsDirHolder(null, goConfigService);
    artifactsDirHolder.initialize();
    filter = new ArtifactSizeEnforcementFilterChain(new ArtifactSizeEnforcementFilter(artifactsDirHolder, new SystemEnvironment()));
    filterChain = mock(FilterChain.class);
}
 
Example 9
@ParameterizedTest
@ValueSource(strings = {"/files/bar/foo.zip", "/remoting/files/bar/foo.zip"})
void shouldAllowIfEnoughDiskSpaceIsAvailable(String path) throws IOException, ServletException {
    request = HttpRequestBuilder.POST(path).withHeader("X-GO-ARTIFACT-SIZE", FileSize.valueOf("100MB").getSize()).build();

    filter.doFilter(request, response, filterChain);

    assertThat(response)
            .isOk();
    verify(filterChain).doFilter(wrap(request), wrap(response));
}
 
Example 10
@ParameterizedTest
@ValueSource(strings = {"/files/bar/foo.zip", "/remoting/files/bar/foo.zip"})
void shouldDisallowIfNotEnoughDiskSpaceIsAvailable(String path) throws IOException, ServletException {
    request = HttpRequestBuilder.POST(path).withHeader("X-GO-ARTIFACT-SIZE", FileSize.valueOf("600MB").getSize()).build();

    filter.doFilter(request, response, filterChain);

    assertThat(response)
            .isEntityTooLarge();

    verify(filterChain, never()).doFilter(request, response);
}
 
Example 11
Source Project: milkman   Source File: LogbackConfiguration.java    License: 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 Project: aion   Source File: AionLoggerFactory.java    License: MIT License 4 votes vote down vote up
private static List<Appender<ILoggingEvent>> constructAppenders(
        boolean shouldLogToFile, String _logDirectory) {
    List<Appender<ILoggingEvent>> appenders = new ArrayList<>();

    String logDirectory = DEFAULT_LOG_DIR;
    if (_logDirectory != null && !_logDirectory.trim().isEmpty()) logDirectory = _logDirectory;

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern(DEFAULT_LOG_PATTERN);
    encoder.start();

    ConsoleAppender<ILoggingEvent> consoleSync = new ConsoleAppender<>();
    consoleSync.setContext(context);
    consoleSync.setName("consoleSyncAppender"); // for logger debugging
    consoleSync.setEncoder(encoder);
    consoleSync.start();

    AsyncAppender consoleAsync = new AsyncAppender();
    consoleAsync.setContext(context);
    consoleAsync.setName("consoleAsyncAppender"); // for logger debugging
    consoleAsync.addAppender(consoleSync);

    consoleAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    consoleAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    consoleAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    consoleAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    consoleAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    consoleAsync.start();

    appenders.add(consoleAsync);
    if (!shouldLogToFile) return appenders;

    RollingFileAppender<ILoggingEvent> fileSync = new RollingFileAppender<>();

    SizeBasedTriggeringPolicy<ILoggingEvent> tp = new SizeBasedTriggeringPolicy<>();
    tp.setContext(context);
    tp.start();

    SizeAndTimeBasedRollingPolicy rp = new SizeAndTimeBasedRollingPolicy();
    rp.setContext(context);
    // roll-over each day
    // notice that we don't use the OS-agnostic File.separator here since logback is converts
    // the FileNamePattern
    // to a unix-style path using ch.qos.logback.core.rolling.helper.FileFilterUtil.slashify
    FileNamePattern fnp =
            new FileNamePattern(
                    logDirectory + "/%d{yyyy/MM, aux}/aion.%d{yyyy-MM-dd}.%i.log", context);
    rp.setFileNamePattern(fnp.getPattern());
    // max rollover file size = 100MB
    rp.setMaxFileSize(FileSize.valueOf("100mb"));
    rp.setParent(fileSync);
    rp.start();

    fileSync.setName("fileSyncAppender"); // for logger debugging
    fileSync.setContext(context);
    fileSync.setTriggeringPolicy(tp);
    fileSync.setRollingPolicy(rp);
    fileSync.setFile(logDirectory + File.separator + DEFAULT_LOG_FILE_CURRENT);
    fileSync.setEncoder(encoder);
    fileSync.setAppend(true);
    fileSync.start();

    AsyncAppender fileAsync = new AsyncAppender();
    fileAsync.setContext(context);
    fileAsync.setName("fileAsyncAppender"); // for logger debugging
    fileAsync.addAppender(fileSync);

    fileAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    fileAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    fileAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    fileAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    fileAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    fileAsync.start();

    appenders.add(fileAsync);
    return appenders;
}
 
Example 13
Source Project: nuls-v2   Source File: LogAppender.java    License: 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 14
Source Project: qpid-broker-j   Source File: AppenderUtils.java    License: Apache License 2.0 4 votes vote down vote up
public DailyTriggeringPolicy(boolean isRollOnRestart, String maxFileSize)
{
    _rollOnRestart = isRollOnRestart;
    _maxFileSize = FileSize.valueOf(maxFileSize);
    setMaxFileSize(_maxFileSize);
}
 
Example 15
Source Project: qpid-broker-j   Source File: AppenderUtils.java    License: Apache License 2.0 4 votes vote down vote up
public FileSize getMaxFileSize()
{
    return _maxFileSize;
}
 
Example 16
Source Project: qpid-broker-j   Source File: AppenderUtils.java    License: Apache License 2.0 4 votes vote down vote up
public FileSize getMaxFileSize()
{
    return _maxFileSize;
}
 
Example 17
Source Project: cerberus   Source File: AthenaAuditLoggerConfiguration.java    License: Apache License 2.0 4 votes vote down vote up
@Autowired
public AthenaAuditLoggerConfiguration(
    @Value("${cerberus.audit.athena.log.path:#{null}}") String logPath,
    AuditLogsS3TimeBasedRollingPolicy<ILoggingEvent> auditLogsS3TimeBasedRollingPolicy) {

  if (StringUtils.isBlank(logPath)) {
    logPath = "";
  } else if (!logPath.endsWith("/")) {
    logPath += "/";
    FilenameUtils.getPath(
        logPath); // this shouldn't be necessary because the path is provided by Spring config,
    // but extra safety
  }
  this.auditLogsS3TimeBasedRollingPolicy = auditLogsS3TimeBasedRollingPolicy;

  LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
  PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
  patternLayoutEncoder.setPattern(MESSAGE_PATTERN);
  patternLayoutEncoder.setContext(loggerContext);
  patternLayoutEncoder.start();

  String hostname;
  try {
    hostname =
        System.getenv("HOSTNAME") != null
            ? System.getenv("HOSTNAME")
            : InetAddress.getLocalHost().getHostName();
  } catch (UnknownHostException e) {
    throw new RuntimeException("Unable to find host name");
  }

  FiveMinuteRollingFileAppender<ILoggingEvent> fiveMinuteRollingFileAppender =
      new FiveMinuteRollingFileAppender<>();
  fiveMinuteRollingFileAppender.setName(ATHENA_LOG_APPENDER_NAME);
  fiveMinuteRollingFileAppender.setContext(loggerContext);
  fiveMinuteRollingFileAppender.setFile(logPath + hostname + "-audit.log");
  fiveMinuteRollingFileAppender.setEncoder(patternLayoutEncoder);

  this.auditLogsS3TimeBasedRollingPolicy.setContext(loggerContext);
  this.auditLogsS3TimeBasedRollingPolicy.setFileNamePattern(
      logPath + hostname + "-audit.%d{yyyy-MM-dd_HH-mm, UTC}.log.gz");
  this.auditLogsS3TimeBasedRollingPolicy.setMaxHistory(100);
  this.auditLogsS3TimeBasedRollingPolicy.setParent(fiveMinuteRollingFileAppender);
  this.auditLogsS3TimeBasedRollingPolicy.setTotalSizeCap(FileSize.valueOf("10gb"));

  fiveMinuteRollingFileAppender.setTriggeringPolicy(this.auditLogsS3TimeBasedRollingPolicy);
  fiveMinuteRollingFileAppender.setRollingPolicy(this.auditLogsS3TimeBasedRollingPolicy);

  this.auditLogsS3TimeBasedRollingPolicy.start();
  fiveMinuteRollingFileAppender.start();

  var logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ATHENA_AUDIT_LOGGER_NAME);
  logger.addAppender(fiveMinuteRollingFileAppender);
  logger.setLevel(Level.INFO);
  logger.setAdditive(false);
  athenaAuditLogger = logger;
}
 
Example 18
Source Project: baleen   Source File: BaleenFileLoggerBuilder.java    License: Apache License 2.0 4 votes vote down vote up
private FileSize getMaxFileSize() {
  return FileSize.valueOf(maxSize.orElse(0) + "kb");
}
 
Example 19
Source Project: bisq   Source File: Log.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
public static void setup(String fileName) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    appender.setContext(loggerContext);
    appender.setFile(fileName + ".log");

    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(appender);
    rollingPolicy.setFileNamePattern(fileName + "_%i.log");
    rollingPolicy.setMinIndex(1);
    rollingPolicy.setMaxIndex(10);
    rollingPolicy.start();

    SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
    triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB"));
    triggeringPolicy.start();

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n");
    encoder.start();

    appender.setEncoder(encoder);
    appender.setRollingPolicy(rollingPolicy);
    appender.setTriggeringPolicy(triggeringPolicy);
    appender.start();

    logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    logbackLogger.addAppender(appender);
    logbackLogger.setLevel(Level.INFO);

    // log errors in separate file
    // not working as expected still.... damn logback...
   /* FileAppender errorAppender = new FileAppender();
    errorAppender.setEncoder(encoder);
    errorAppender.setName("Error");
    errorAppender.setContext(loggerContext);
    errorAppender.setFile(fileName + "_error.log");
    LevelFilter levelFilter = new LevelFilter();
    levelFilter.setLevel(Level.ERROR);
    levelFilter.setOnMatch(FilterReply.ACCEPT);
    levelFilter.setOnMismatch(FilterReply.DENY);
    levelFilter.start();
    errorAppender.addFilter(levelFilter);
    errorAppender.start();
    logbackLogger.addAppender(errorAppender);*/
}
 
Example 20
Source Project: summerframework   Source File: LogbackUtil.java    License: 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;
}