ch.qos.logback.core.util.FileSize类的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(322)

本文整理了Java中ch.qos.logback.core.util.FileSize类的一些代码示例,展示了FileSize类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FileSize类的具体详情如下:
包路径:ch.qos.logback.core.util.FileSize
类名称:FileSize

FileSize介绍

[英]Instances of this class represent the size of a file. Internally, the size is stored as long.

The #valueOf method can convert strings such as "3 kb", "5 mb", into FileSize instances. The recognized unit specifications for file size are the "kb", "mb", and "gb". The unit name may be followed by an "s". Thus, "2 kbs" and "2 kb" are equivalent. In the absence of a time unit specification, byte is assumed.
[中]此类的实例表示文件的大小。在内部,大小存储为long。
#valueOf方法可以将诸如“3KB”、“5MB”之类的字符串转换为文件大小的实例。公认的文件大小单位规格为“kb”、“mb”和“gb”。装置名称后面可以跟一个“s”。因此,“2kb”和“2kb”是等价的。在没有时间单位规范的情况下,假定为字节。

代码示例

代码示例来源:origin: alibaba/nacos

/**
   * logback 1.1.8开始不再支持setMaxFileSize(String)方法
   */
  protected void setMaxFileSize(Object policy, String size) {
    try {
      try {
        Method setMaxFileSizeMethod = policy.getClass().getDeclaredMethod("setMaxFileSize", String.class);
        setMaxFileSizeMethod.invoke(policy, size);
      } catch (NoSuchMethodException e) {
        Method setMaxFileSizeMethod = policy.getClass().getDeclaredMethod("setMaxFileSize", FileSize.class);
        setMaxFileSizeMethod.invoke(policy, FileSize.valueOf(size));
      }
    } catch (Throwable t) {
      throw new RuntimeException("Failed to setMaxFileSize", t);
    }
  }
}

代码示例来源:origin: jmxtrans/jmxtrans

/**
 * Initializes the logger. This is called when we need to create a new
 * logger for the given file name.
 *
 * @param fileStr
 * @return a new Logger instance for the given fileStr
 * @throws IOException
 */
protected Logger initLogger(String fileStr) throws IOException {
  String loggerName = "NagiosWriter" + this.hashCode();
  final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
  encoder.setContext(loggerContext);
  encoder.setPattern(LOG_PATTERN);
  encoder.start();
  final FileAppender appender = new FileAppender();
  appender.setContext(loggerContext);
  appender.setName(loggerName + "File");
  appender.setAppend(true);
  appender.setBufferSize(new FileSize(LOG_IO_BUFFER_SIZE_BYTES));
  appender.setFile(fileStr);
  appender.setEncoder(encoder);
  appender.start();
  Logger logger = loggerContext.getLogger(loggerName);
  logger.addAppender(appender);
  logger.setLevel(Level.INFO);
  logger.setAdditive(false);
  return logger;
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void createRollingPolicy_size() throws Exception {
 props.set("sonar.log.rollingPolicy", "size:1MB");
 props.set("sonar.log.maxFiles", "20");
 LoggerContext ctx = underTest.getRootContext();
 LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
 Appender appender = policy.createAppender("SONAR_FILE");
 assertThat(appender).isInstanceOf(RollingFileAppender.class);
 // max 20 files of 1Mb
 RollingFileAppender fileAppender = (RollingFileAppender) appender;
 FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) fileAppender.getRollingPolicy();
 assertThat(rollingPolicy.getMaxIndex()).isEqualTo(20);
 assertThat(rollingPolicy.getFileNamePattern()).endsWith("sonar.%i.log");
 SizeBasedTriggeringPolicy triggeringPolicy = (SizeBasedTriggeringPolicy) fileAppender.getTriggeringPolicy();
 FileSize maxFileSize = (FileSize) FieldUtils.readField(triggeringPolicy, "maxFileSize", true);
 assertThat(maxFileSize.getSize()).isEqualTo(1024L * 1024);
}

代码示例来源:origin: tony19/logback-android

@Test
public void testValueOf() {
 {
  FileSize fs = FileSize.valueOf("8");
  assertEquals(8, fs.getSize());
 }
 
 {
  FileSize fs = FileSize.valueOf("8 kbs");
  assertEquals(8*KB_CO, fs.getSize());
 }

 {
  FileSize fs = FileSize.valueOf("8 kb");
  assertEquals(8*KB_CO, fs.getSize());
 }
 
 {
  FileSize fs = FileSize.valueOf("12 mb");
  assertEquals(12*MB_CO, fs.getSize());
 }
 {
  FileSize fs = FileSize.valueOf("5 GBs");
  assertEquals(5*GB_CO, fs.getSize());
 }
}

代码示例来源:origin: tony19/logback-android

@Test
public void testToString() {
 {
  FileSize fs = new FileSize(8);
  assertEquals("8 Bytes", fs.toString());
 }
 {
  FileSize fs = new FileSize(8 * 1024 + 3);
  assertEquals("8 KB", fs.toString());
 }
 {
  FileSize fs = new FileSize(8 * 1024 * 1024 + 3 * 1024);
  assertEquals("8 MB", fs.toString());
 }
 {
  FileSize fs = new FileSize(8*1024*1024*1024L);
  assertEquals("8 GB", fs.toString());
 }
}

代码示例来源:origin: tony19/logback-android

public void setTotalSizeCap(FileSize totalSizeCap) {
  addInfo("setting totalSizeCap to "+totalSizeCap.toString());
  this.totalSizeCap = totalSizeCap;
 }
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

public void setTotalSizeCap(FileSize totalSizeCap) {
    addInfo("setting totalSizeCap to "+totalSizeCap.toString());
    this.totalSizeCap = totalSizeCap;
  }
}

代码示例来源:origin: dropwizard/dropwizard

appender.setContext(context);
  appender.setFile(currentLogFilename);
  appender.setBufferSize(new FileSize(bufferSize.toBytes()));
    triggeringPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
    triggeringPolicy.setContext(context);
    triggeringPolicy.start();
      sizeAndTimeBasedRollingPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
      rollingPolicy = sizeAndTimeBasedRollingPolicy;
      rollingPolicy.setTotalSizeCap(new FileSize(totalSizeCap.toBytes()));
appender.setContext(context);
appender.setFile(currentLogFilename);
appender.setBufferSize(new FileSize(bufferSize.toBytes()));
return appender;

代码示例来源:origin: org.springframework.boot/spring-boot

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);
  }
}

代码示例来源:origin: camunda/camunda-bpm-platform

public boolean isTriggeringEvent(final File activeFile, final E event) {
if(invocationGate.skipFurtherWork())
  return false;
 long now = System.currentTimeMillis();
 invocationGate.updateMaskIfNecessary(now);
 return (activeFile.length() >= maxFileSize.getSize());
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

public void setTotalSizeCap(FileSize totalSizeCap) {
    addInfo("setting totalSizeCap to "+totalSizeCap.toString());
    this.totalSizeCap = totalSizeCap;
  }
}

代码示例来源:origin: jmxtrans/jmxtrans

protected Appender buildAppender(String loggerName, String fileStr) {
  final RollingFileAppender appender = new RollingFileAppender();
  appender.setName(loggerName + "RollingFile");
  appender.setContext(loggerContext);
  appender.setImmediateFlush(true);
  appender.setBufferSize(new FileSize(LOG_IO_BUFFER_SIZE_BYTES));
  appender.setFile(fileStr);
  appender.setEncoder(buildEncoder());
  TriggeringPolicy triggeringPolicy = buildTriggeringPolicy();
  if (triggeringPolicy != null) {
    appender.setTriggeringPolicy(triggeringPolicy);
  }
  appender.setRollingPolicy(buildRollingPolicy(appender, fileStr));
  appender.start();
  return appender;
}

代码示例来源:origin: jmxtrans/jmxtrans

protected TriggeringPolicy buildTriggeringPolicy() {
  SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
  triggeringPolicy.setContext(loggerContext);
  triggeringPolicy.setMaxFileSize(FileSize.valueOf(maxLogFileSize));
  triggeringPolicy.start();
  return triggeringPolicy;
}

代码示例来源:origin: camunda/camunda-bpm-platform

public boolean isTriggeringEvent(File activeFile, final E event) {
 long time = getCurrentTime();
 if (time >= nextCheck) {
  Date dateInElapsedPeriod = dateInCurrentPeriod;
  elapsedPeriodsFileName = tbrp.fileNamePatternWCS
      .convertMultipleArguments(dateInElapsedPeriod, currentPeriodsCounter);
  currentPeriodsCounter = 0;
  setDateInCurrentPeriod(time);
  computeNextCheck();
  return true;
 }
 // for performance reasons, check for changes every 16,invocationMask invocations
 if (((++invocationCounter) & invocationMask) != invocationMask) {
  return false;
 }
 if (invocationMask < 0x0F) {
  invocationMask = (invocationMask << 1) + 1;
 }
 if (activeFile.length() >= maxFileSize.getSize()) {
  elapsedPeriodsFileName = tbrp.fileNamePatternWCS
      .convertMultipleArguments(dateInCurrentPeriod, currentPeriodsCounter);
  currentPeriodsCounter++;
  return true;
 }
 return false;
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

public void setBufferSize(FileSize bufferSize) {
  addInfo("Setting bufferSize to ["+bufferSize.toString()+"]");
  this.bufferSize = bufferSize;
}

代码示例来源:origin: camunda/camunda-bpm-platform

static public FileSize valueOf(String fileSizeStr) {
  Matcher matcher = FILE_SIZE_PATTERN.matcher(fileSizeStr);

  long coefficient;
  if (matcher.matches()) {
   String lenStr = matcher.group(DOUBLE_GROUP);
   String unitStr = matcher.group(UNIT_GROUP);

   long lenValue = Long.valueOf(lenStr);
   if (unitStr.equalsIgnoreCase("")) {
    coefficient = 1;
   } else if (unitStr.equalsIgnoreCase("kb")) {
    coefficient = KB_COEFFICIENT;
   } else if (unitStr.equalsIgnoreCase("mb")) {
    coefficient = MB_COEFFICIENT;
   } else if (unitStr.equalsIgnoreCase("gb")) {
    coefficient = GB_COEFFICIENT;
   } else {
    throw new IllegalStateException("Unexpected " + unitStr);
   }
   return new FileSize(lenValue * coefficient);
  } else {
   throw new IllegalArgumentException("String value [" + fileSizeStr
     + "] is not in the expected format.");
  }

 }
}

代码示例来源:origin: gocd/gocd

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();
}

代码示例来源:origin: tony19/logback-android

protected boolean isUnboundedTotalSizeCap() {
 return totalSizeCap.getSize() == UNBOUNDED_TOTAL_SIZE_CAP;
}

代码示例来源:origin: Nextdoor/bender

public void setBufferSize(FileSize bufferSize) {
  addInfo("Setting bufferSize to ["+bufferSize.toString()+"]");
  this.bufferSize = bufferSize;
}

代码示例来源:origin: org.jmxtrans/jmxtrans-output-log4j

private FileSize toFileSize(String s) {
  return new FileSize(parseLong(s));
}

相关文章