ch.qos.logback.core.rolling.TimeBasedRollingPolicy类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(121)

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

TimeBasedRollingPolicy介绍

[英]TimeBasedRollingPolicy is both easy to configure and quite powerful. It allows the roll over to be made based on time. It is possible to specify that the roll over occur once per day, per week or per month.

For more information, please refer to the online manual at http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy
[中]TimeBasedRollingPolicy既易于配置,又非常强大。它允许根据时间进行翻滚。可以指定翻滚每天、每周或每月发生一次。
有关更多信息,请参阅在线手册:http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy

代码示例

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

@Override
 public FileAppender<ILoggingEvent> createAppender(String appenderName) {
  RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
  appender.setContext(context);
  appender.setName(appenderName);
  String filePath = new File(logsDir, filenamePrefix + ".log").getAbsolutePath();
  appender.setFile(filePath);
  TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
  rollingPolicy.setContext(context);
  rollingPolicy.setFileNamePattern(StringUtils.replace(filePath, filenamePrefix + ".log", filenamePrefix + ".%d{" + datePattern + "}.log"));
  rollingPolicy.setMaxHistory(maxFiles);
  rollingPolicy.setParent(appender);
  rollingPolicy.start();
  appender.setRollingPolicy(rollingPolicy);
  return appender;
 }
}

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

protected FileAppender<E> buildAppender(LoggerContext context) {
  if (archive) {
    final RollingFileAppender<E> appender = new RollingFileAppender<>();
    appender.setContext(context);
    appender.setFile(currentLogFilename);
    appender.setBufferSize(new FileSize(bufferSize.toBytes()));
      final TimeBasedRollingPolicy<E> rollingPolicy;
      if (maxFileSize == null) {
        rollingPolicy = new TimeBasedRollingPolicy<>();
        rollingPolicy.setTotalSizeCap(new FileSize(totalSizeCap.toBytes()));
      rollingPolicy.setContext(context);
      rollingPolicy.setFileNamePattern(archivedLogFilenamePattern);
      rollingPolicy.setMaxHistory(archivedFileCount);
      appender.setRollingPolicy(rollingPolicy);
      rollingPolicy.setParent(appender);
      rollingPolicy.start();
      return appender;

代码示例来源:origin: uk.gov.dstl.baleen/baleen-core

RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
appender.setEncoder(encoder);
appender.setFile(file);
TimeBasedRollingPolicy<ILoggingEvent> rolling = new TimeBasedRollingPolicy<>();
rolling.setContext(context);
rolling.setParent(appender);
rolling.setFileNamePattern(getFileWithPattern("%d"));
 rolling.setMaxHistory(maxNumberLogs.get());
} else {
 rolling.setMaxHistory(1);
 sizeBased.setTimeBasedRollingPolicy(rolling);
 rolling.setTimeBasedFileNamingAndTriggeringPolicy(sizeBased);
rolling.start();
if (rolling.getTimeBasedFileNamingAndTriggeringPolicy() != null) {
 rolling.getTimeBasedFileNamingAndTriggeringPolicy().start();
appender.setRollingPolicy(rolling);

代码示例来源:origin: epam/DLab

/** Create and return synchronous the rolling file appender.
 * @param context the context of logger. 
 */
private RollingFileAppender<ILoggingEvent> getRollingFileAppender(LoggerContext context) throws InitializationException {
  if (archivedLogFilenamePattern == null || archivedLogFilenamePattern.trim().isEmpty()) {
    throw new InitializationException("Configuration property logging.appenders.archivedLogFilenamePattern cannot be null.");
  }
  RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();
  appender.setFile(currentLogFilename);
  appender.setAppend(true);
  TimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> triggerPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
  triggerPolicy.setContext(context);
  
  TimeBasedRollingPolicy<ILoggingEvent> rollPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
  rollPolicy.setContext(context);
  rollPolicy.setParent(appender);
  rollPolicy.setFileNamePattern(archivedLogFilenamePattern);
  rollPolicy.setMaxHistory(archivedFileCount);
  rollPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggerPolicy);
  rollPolicy.start();
  appender.setRollingPolicy(rollPolicy);
  
  return appender;
}

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

@SuppressWarnings("unchecked")
public void setUpTimeBasedTriggeringPolicy(RollingFileAppender<Object> rfa) {
 String datePattern = "yyyy-MM-dd'T'HH_mm_ss_SSS";
 TimeBasedRollingPolicy<Object> tbrp = new TimeBasedRollingPolicy();
 tbrp.setFileNamePattern(outputDirStr + "test-%d{" + datePattern + "}");
 tbrp.setContext(context);
 tbrp.setParent(rfa);
 tbrp.start();
 rfa.setRollingPolicy(tbrp);
 rfa.start();
}

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

@SuppressWarnings("unchecked")
static LoggerContext buildLoggerContext(String stamp, String filename,
  boolean safetyMode) {
 LoggerContext loggerContext = new LoggerContext();
 RollingFileAppender<ILoggingEvent> rfa = new RollingFileAppender<ILoggingEvent>();
 PatternLayoutEncoder patternLayout = new PatternLayoutEncoder();
 patternLayout.setPattern(stamp + " %5p - %-50m%n");
 patternLayout.setContext(loggerContext);
 patternLayout.start();
 rfa.setEncoder(patternLayout);
 
 rfa.setAppend(true);
 rfa.setPrudent(safetyMode);
 rfa.setContext(loggerContext);
 TimeBasedRollingPolicy<ILoggingEvent> tbrp = new TimeBasedRollingPolicy();
 
 tbrp.setContext(loggerContext);
 tbrp.setFileNamePattern(filename+"-%d{"+DATE_PATTERN+"}.log");
 tbrp.setParent(rfa);
 tbrp.start();

 rfa.setRollingPolicy(tbrp);
 
 rfa.start();
 ch.qos.logback.classic.Logger root = loggerContext
   .getLogger(Logger.ROOT_LOGGER_NAME);
 root.addAppender(rfa);
 return loggerContext;
}

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

private void buildRollingFileAppender(ConfigParameters cp) {
 rfa.setContext(context);
 rfa.setEncoder(encoder);
 tbrp.setContext(context);
 tbrp.setFileNamePattern(cp.fileNamePattern);
 tbrp.setMaxHistory(cp.maxHistory);
 tbrp.setTotalSizeCap(new FileSize(cp.sizeCap));
 tbrp.setParent(rfa);
 tbrp.setCleanHistoryOnStart(false);
 tbrp.timeBasedFileNamingAndTriggeringPolicy = tbfnatp;
 tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(cp.simulatedTime);
 tbrp.start();
 rfa.setRollingPolicy(tbrp);
 rfa.start();
}

代码示例来源:origin: Multibit-Legacy/multibit-hd

new RollingFileAppender<ILoggingEvent>() :
new FileAppender<ILoggingEvent>();
triggeringPolicy.setContext(context);
final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
rollingPolicy.setContext(context);
rollingPolicy.setFileNamePattern(fileConfiguration.getArchivedLogFilenamePattern());
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
rollingPolicy.setMaxHistory(fileConfiguration.getArchivedFileCount());
((RollingFileAppender<ILoggingEvent>) appender).setRollingPolicy(rollingPolicy);
((RollingFileAppender<ILoggingEvent>) appender).setTriggeringPolicy(triggeringPolicy);
rollingPolicy.setParent(appender);
rollingPolicy.start();

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

@Override
protected Appender<Object> getConfiguredAppender() {
 rfa.setContext(context);
 tbrp
     .setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log");
 tbrp.start();
 rfa.setRollingPolicy(tbrp);
 rfa.start();
 return rfa;
}

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

@Test
public void testPrudentModeLogicalImplications() {
 rfa.setContext(context);
 // prudent mode will force "file" property to be null
 rfa.setFile("some non null value");
 rfa.setAppend(false);
 rfa.setPrudent(true);
 tbrp
     .setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log");
 tbrp.start();
 rfa.setRollingPolicy(tbrp);
 rfa.start();
 assertTrue(rfa.isAppend());
 assertNull(rfa.rawFileProperty());
 assertTrue(rfa.isStarted());
}

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

TimeBasedRollingPolicy<HttpRequestEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
rollingPolicy.setContext(context);
rollingPolicy.setMaxHistory(maxHistory);
rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
rollingPolicy.setParent(fileAppender);
rollingPolicy.setFileNamePattern(filename + "-%d{yyyy-MM-dd}.%i.log");
if (compressionEnabled) {
  rollingPolicy.setFileNamePattern(rollingPolicy.getFileNamePattern() + ".gz");
asyncAppender.addAppender(fileAppender);
rollingPolicy.start();
triggeringPolicy.start();
fileAppender.start();

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

@Test
public void testPrudentModeLogicalImplicationsOnCompression() {
 rfa.setContext(context);
 rfa.setAppend(false);
 rfa.setPrudent(true);
 tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log.zip");
 tbrp.start();
 rfa.setRollingPolicy(tbrp);
 rfa.start();
 StatusChecker checker = new StatusChecker(context);
 assertFalse(rfa.isStarted());
 assertEquals(Status.ERROR, checker.getHighestLevel(0));
}

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

@Test
public void stopTimeBasedRollingPolicy() {
 rfa.setContext(context);
 tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d.log.zip");
 tbrp.start();
 rfa.setRollingPolicy(tbrp);
 rfa.start();
 StatusPrinter.print(context);
 assertTrue(tbrp.isStarted());
 assertTrue(rfa.isStarted());
 rfa.stop();
 assertFalse(rfa.isStarted());
 assertFalse(tbrp.isStarted());
}

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

private RollingPolicy rollingPolicy(final CharSequence baseName, final Context context) {
  final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<>();
  rollingPolicy.setContext(context);
  rollingPolicy.setFileNamePattern(filePath(baseName, "%d{yyyy-MM-dd}.%i.log"));
  rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(sizedBasedNaming(context));
  rollingPolicy.setMaxHistory(maxHistory);
  return rollingPolicy;
}

代码示例来源:origin: stackoverflow.com

rollingFileAppender.setAppend(true);
TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
rollingPolicy.setContext(loggerContext);
rollingPolicy.setParent(rollingFileAppender);
rollingPolicy.setFileNamePattern(logFileName + "-%d{yyyy-MM-dd}.log");
rollingPolicy.setMaxHistory(maxHistory);
rollingPolicy.start();

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

@Before
public void setUp() {
 rfa.setContext(context);
 tbrp.setContext(context);
 timeBasedFNATP.setContext(context);
 rfa.setRollingPolicy(tbrp);
 tbrp.setParent(rfa);
 tbrp.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedFNATP);
 timeBasedFNATP.setTimeBasedRollingPolicy(tbrp);
}

代码示例来源:origin: stackoverflow.com

private static final String LOG4J_ROLLING_FILE_NAME_TOKEN = "Launcher";

/*
 * Change the name of the the log file as configured through log4j.xml
 * by replacing the placeholder file name token ("Launcher") with the
 * a new "actionName".
 */
private static void log4jConfig(String actionName) {

  org.apache.log4j.Logger rootLogger = LogManager.getRootLogger();
  RollingFileAppender fileAppender = (RollingFileAppender)rootLogger.getAppender("fileAppender");

  // <param name="FileNamePattern" value="/var/log/Launcher.log.%d{yyyy-MM-dd}.gz"/>
  String currentLogFile = fileAppender.getFile();
  String newLogPattern = currentLogFile.replace(LOG4J_ROLLING_FILE_NAME_TOKEN, actionName);
  fileAppender.setFile(newLogPattern);

  TimeBasedRollingPolicy timeBasedRollingPolicy = (TimeBasedRollingPolicy) fileAppender.getRollingPolicy();
  String fileNamePattern = timeBasedRollingPolicy.getFileNamePattern();
  String newFileNamePattern = fileNamePattern.replace(LOG4J_ROLLING_FILE_NAME_TOKEN, actionName);;
  timeBasedRollingPolicy.setFileNamePattern(newFileNamePattern);
  timeBasedRollingPolicy.activateOptions();

  fileAppender.activateOptions();

  LOG.info("  Redirected launcher log output to log pattern: " + newFileNamePattern);
}

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

@Before
public void setUp() throws Exception {
 // noStartTest fails if the context is set in setUp
 // rfa.setContext(context);
 rfa.setEncoder(new DummyEncoder<Object>());
 rfa.setName("test");
 tbrp.setContext(context);
 tbrp.setParent(rfa);
}

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

determineCompressionMode();
} else {
 addWarn(FNP_NOT_SET);
 addWarn(CoreConstants.SEE_FNP_NOT_SET);
 throw new IllegalStateException(FNP_NOT_SET
   + CoreConstants.SEE_FNP_NOT_SET);
    fileNamePatternStr, compressionMode), this.context);
addInfo("Will use the pattern " + fileNamePatternWCS
  + " for the active file");
 String zipEntryFileNamePatternStr = transformFileNamePattern2ZipEntry(fileNamePatternStr);
 zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, context);
 archiveRemover.setMaxHistory(maxHistory);
 if(cleanHistoryOnStart) {
  addInfo("Cleaning on start up");
  archiveRemover.clean(new Date(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime()));

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

@Test
public void multiDate() {
 // Tuesday December 20th 17:59:01 CET 2011
 long startTime = 1324400341553L;
 tbrp.setFileNamePattern("foo-%d{yyyy-MM, AUX}/%d{mm}.log");
 tbrp.start();
 timeBasedFNATP.setCurrentTime(startTime);
 timeBasedFNATP.start();
 timeBasedFNATP.setCurrentTime(startTime+MILLIS_IN_MINUTE);
 timeBasedFNATP.isTriggeringEvent(null, null);
 String elapsedPeriodsFileName = timeBasedFNATP.getElapsedPeriodsFileName();
 assertEquals("foo-2011-12/59.log", elapsedPeriodsFileName);
}

相关文章

微信公众号

最新文章

更多