Java 类org.apache.logging.log4j.core.appender.rolling.RolloverStrategy 实例源码

项目:logging-log4j2    文件:Log4j1ConfigurationFactoryTest.java   
private void testRollingFileAppender(final String configResource, final String name, final String filePattern) throws URISyntaxException {
    final Configuration configuration = getConfiguration(configResource);
    final Appender appender = configuration.getAppender(name);
    assertNotNull(appender);
    assertEquals(name, appender.getName());
    assertTrue(appender.getClass().getName(), appender instanceof RollingFileAppender);
    final RollingFileAppender rfa = (RollingFileAppender) appender;
    assertEquals("target/hadoop.log", rfa.getFileName());
    assertEquals(filePattern, rfa.getFilePattern());
    final TriggeringPolicy triggeringPolicy = rfa.getTriggeringPolicy();
    assertNotNull(triggeringPolicy);
    assertTrue(triggeringPolicy.getClass().getName(), triggeringPolicy instanceof CompositeTriggeringPolicy);
    final CompositeTriggeringPolicy ctp = (CompositeTriggeringPolicy) triggeringPolicy;
    final TriggeringPolicy[] triggeringPolicies = ctp.getTriggeringPolicies();
    assertEquals(1, triggeringPolicies.length);
    final TriggeringPolicy tp = triggeringPolicies[0];
    assertTrue(tp.getClass().getName(), tp instanceof SizeBasedTriggeringPolicy);
    final SizeBasedTriggeringPolicy sbtp = (SizeBasedTriggeringPolicy) tp;
    assertEquals(256 * 1024 * 1024, sbtp.getMaxFileSize());
    final RolloverStrategy rolloverStrategy = rfa.getManager().getRolloverStrategy();
    assertTrue(rolloverStrategy.getClass().getName(), rolloverStrategy instanceof DefaultRolloverStrategy);
    final DefaultRolloverStrategy drs = (DefaultRolloverStrategy) rolloverStrategy;
    assertEquals(20, drs.getMaxIndex());
    configuration.start();
    configuration.stop();
}
项目:logging-log4j2    文件:Log4j1ConfigurationFactoryTest.java   
private void testDailyRollingFileAppender(final String configResource, final String name, final String filePattern) throws URISyntaxException {
    final Configuration configuration = getConfiguration(configResource);
    final Appender appender = configuration.getAppender(name);
    assertNotNull(appender);
    assertEquals(name, appender.getName());
    assertTrue(appender.getClass().getName(), appender instanceof RollingFileAppender);
    final RollingFileAppender rfa = (RollingFileAppender) appender;
    assertEquals("target/hadoop.log", rfa.getFileName());
    assertEquals(filePattern, rfa.getFilePattern());
    final TriggeringPolicy triggeringPolicy = rfa.getTriggeringPolicy();
    assertNotNull(triggeringPolicy);
    assertTrue(triggeringPolicy.getClass().getName(), triggeringPolicy instanceof CompositeTriggeringPolicy);
    final CompositeTriggeringPolicy ctp = (CompositeTriggeringPolicy) triggeringPolicy;
    final TriggeringPolicy[] triggeringPolicies = ctp.getTriggeringPolicies();
    assertEquals(1, triggeringPolicies.length);
    final TriggeringPolicy tp = triggeringPolicies[0];
    assertTrue(tp.getClass().getName(), tp instanceof TimeBasedTriggeringPolicy);
    final TimeBasedTriggeringPolicy tbtp = (TimeBasedTriggeringPolicy) tp;
    assertEquals(1, tbtp.getInterval());
    final RolloverStrategy rolloverStrategy = rfa.getManager().getRolloverStrategy();
    assertTrue(rolloverStrategy.getClass().getName(), rolloverStrategy instanceof DefaultRolloverStrategy);
    final DefaultRolloverStrategy drs = (DefaultRolloverStrategy) rolloverStrategy;
    assertEquals(Integer.MAX_VALUE, drs.getMaxIndex());
    configuration.start();
    configuration.stop();
}
项目:log4j-configuration-builder    文件:ConfigurationBuilder.java   
/**
 * Creates a {@link FileAppender}.
 * 
 * @param configuration the owner configuration
 * @param fileName the name of log file
 * @param fileNamePattern the pattern of the name of the log
 *        file
 * @param appenderName the name of the appender
 * @param layout the {@link PatternLayout} to use for the
 *        appender
 * @param rolloverInterval how often the log files should be
 *        rolled over (in DAYS)
 * @param maximumFilesToKeep the maximum number of file to
 *        keep after every roll-over
 * @return an instance of {@link Appender}
 */
protected static Appender createFileAppender(final Configuration configuration,
    final String fileName, final String fileNamePattern, final String appenderName,
    final PatternLayout layout, String rolloverInterval, String maximumFilesToKeep) {
  final String append = Boolean.TRUE.toString();
  final String bufferedIO = Boolean.TRUE.toString();
  final String bufferSizeStr = null;
  final String immediateFlush = Boolean.TRUE.toString();
  final Filter filter = null;
  final String ignore = null;
  final String advertise = null;
  final String advertiseURI = null;

  // Trigger Policy
  final String modulate = Boolean.TRUE.toString();
  final TriggeringPolicy policy =
      TimeBasedTriggeringPolicy.createPolicy(rolloverInterval, modulate);

  // Rollover strategy
  final String minFilesToKeep = "1";
  final String fileIndex = null;
  final String compressionLevelStr = Integer.toString(Deflater.DEFAULT_COMPRESSION);
  final RolloverStrategy rolloverStrategy = DefaultRolloverStrategy.createStrategy(
      maximumFilesToKeep, minFilesToKeep, fileIndex, compressionLevelStr, configuration);

  return RollingFileAppender.createAppender(fileName, fileNamePattern, append, appenderName,
      bufferedIO, bufferSizeStr, immediateFlush, policy, rolloverStrategy, layout, filter, ignore,
      advertise, advertiseURI, configuration);
}
项目:WorldDownloader-Serverside-Companion    文件:RollingRandomAccessFileAppender.java   
public static RollingRandomAccessFileAppender createAppender(final String fileName, final String filePattern, final String append, final String name, final String immediateFlush, final TriggeringPolicy policy, RolloverStrategy strategy, Layout<? extends Serializable> layout, final Filter filter, final String ignore, final String advertise, final String advertiseURI, final Configuration config) {
    throw new AssertionError("This is test code!");
}
项目:log4j2    文件:RollingFileAppender.java   
/**
 * Create a RollingFileAppender.
 * @param fileName The name of the file that is actively written to. (required).
 * @param filePattern The pattern of the file name to use on rollover. (required).
 * @param append If true, events are appended to the file. If false, the file
 * is overwritten when opened. Defaults to "true"
 * @param name The name of the Appender (required).
 * @param bufferedIO When true, I/O will be buffered. Defaults to "true".
 * @param immediateFlush When true, events are immediately flushed. Defaults to "true".
 * @param policy The triggering policy. (required).
 * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy.
 * @param layout The layout to use (defaults to the default PatternLayout).
 * @param filter The Filter or null.
 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
 *               they are propagated to the caller.
 * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
 * @param advertiseURI The advertised URI which can be used to retrieve the file contents.
 * @param config The Configuration.
 * @return A RollingFileAppender.
 */
@PluginFactory
public static RollingFileAppender createAppender(
        @PluginAttribute("fileName") final String fileName,
        @PluginAttribute("filePattern") final String filePattern,
        @PluginAttribute("append") final String append,
        @PluginAttribute("name") final String name,
        @PluginAttribute("bufferedIO") final String bufferedIO,
        @PluginAttribute("immediateFlush") final String immediateFlush,
        @PluginElement("Policy") final TriggeringPolicy policy,
        @PluginElement("Strategy") RolloverStrategy strategy,
        @PluginElement("Layout") Layout<? extends Serializable> layout,
        @PluginElement("Filter") final Filter filter,
        @PluginAttribute("ignoreExceptions") final String ignore,
        @PluginAttribute("advertise") final String advertise,
        @PluginAttribute("advertiseURI") final String advertiseURI,
        @PluginConfiguration final Configuration config) {

    final boolean isAppend = Booleans.parseBoolean(append, true);
    final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    final boolean isBuffered = Booleans.parseBoolean(bufferedIO, true);
    final boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
    final boolean isAdvertise = Boolean.parseBoolean(advertise);
    if (name == null) {
        LOGGER.error("No name provided for FileAppender");
        return null;
    }

    if (fileName == null) {
        LOGGER.error("No filename was provided for FileAppender with name "  + name);
        return null;
    }

    if (filePattern == null) {
        LOGGER.error("No filename pattern provided for FileAppender with name "  + name);
        return null;
    }

    if (policy == null) {
        LOGGER.error("A TriggeringPolicy must be provided");
        return null;
    }

    if (strategy == null) {
        strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
                String.valueOf(Deflater.DEFAULT_COMPRESSION), config);
    }

    if (layout == null) {
        layout = PatternLayout.createLayout(null, null, null, null, null);
    }

    final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, isAppend,
        isBuffered, policy, strategy, advertiseURI, layout);
    if (manager == null) {
        return null;
    }

    return new RollingFileAppender(name, layout, filter, manager, fileName, filePattern,
            ignoreExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null);
}
项目:logging-log4j2    文件:RollingFileAppender.java   
public RolloverStrategy getStrategy() {
    return strategy;
}
项目:logging-log4j2    文件:RollingFileAppender.java   
public B withStrategy(final RolloverStrategy strategy) {
    this.strategy = strategy;
    return asBuilder();
}
项目:logging-log4j2    文件:RollingFileAppender.java   
/**
 * Creates a RollingFileAppender.
 * @param fileName The name of the file that is actively written to. (required).
 * @param filePattern The pattern of the file name to use on rollover. (required).
 * @param append If true, events are appended to the file. If false, the file
 * is overwritten when opened. Defaults to "true"
 * @param name The name of the Appender (required).
 * @param bufferedIO When true, I/O will be buffered. Defaults to "true".
 * @param bufferSizeStr buffer size for buffered IO (default is 8192).
 * @param immediateFlush When true, events are immediately flushed. Defaults to "true".
 * @param policy The triggering policy. (required).
 * @param strategy The rollover strategy. Defaults to DefaultRolloverStrategy.
 * @param layout The layout to use (defaults to the default PatternLayout).
 * @param filter The Filter or null.
 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
 *               they are propagated to the caller.
 * @param advertise "true" if the appender configuration should be advertised, "false" otherwise.
 * @param advertiseUri The advertised URI which can be used to retrieve the file contents.
 * @param config The Configuration.
 * @return A RollingFileAppender.
 * @deprecated Use {@link #newBuilder()}.
 */
@Deprecated
public static <B extends Builder<B>> RollingFileAppender createAppender(
        // @formatter:off
        final String fileName,
        final String filePattern,
        final String append,
        final String name,
        final String bufferedIO,
        final String bufferSizeStr,
        final String immediateFlush,
        final TriggeringPolicy policy,
        final RolloverStrategy strategy,
        final Layout<? extends Serializable> layout,
        final Filter filter,
        final String ignore,
        final String advertise,
        final String advertiseUri,
        final Configuration config) {
        // @formatter:on
    final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE);
    // @formatter:off
    return RollingFileAppender.<B>newBuilder()
            .withAdvertise(Boolean.parseBoolean(advertise))
            .withAdvertiseUri(advertiseUri)
            .withAppend(Booleans.parseBoolean(append, true))
            .withBufferedIo(Booleans.parseBoolean(bufferedIO, true))
            .withBufferSize(bufferSize)
            .setConfiguration(config)
            .withFileName(fileName)
            .withFilePattern(filePattern)
            .withFilter(filter)
            .withIgnoreExceptions(Booleans.parseBoolean(ignore, true))
            .withImmediateFlush(Booleans.parseBoolean(immediateFlush, true))
            .withLayout(layout)
            .withCreateOnDemand(false)
            .withLocking(false)
            .withName(name)
            .withPolicy(policy)
            .withStrategy(strategy)
            .build();
    // @formatter:on
}
项目:logging-log4j2    文件:RollingRandomAccessFileAppender.java   
public B withStrategy(final RolloverStrategy strategy) {
    this.strategy = strategy;
    return asBuilder();
}
项目:logging-log4j2    文件:RollingRandomAccessFileAppender.java   
/**
 * Create a RollingRandomAccessFileAppender.
 *
 * @param fileName The name of the file that is actively written to.
 *            (required).
 * @param filePattern The pattern of the file name to use on rollover.
 *            (required).
 * @param append If true, events are appended to the file. If false, the
 *            file is overwritten when opened. Defaults to "true"
 * @param name The name of the Appender (required).
 * @param immediateFlush When true, events are immediately flushed. Defaults
 *            to "true".
 * @param bufferSizeStr The buffer size, defaults to {@value RollingRandomAccessFileManager#DEFAULT_BUFFER_SIZE}.
 * @param policy The triggering policy. (required).
 * @param strategy The rollover strategy. Defaults to
 *            DefaultRolloverStrategy.
 * @param layout The layout to use (defaults to the default PatternLayout).
 * @param filter The Filter or null.
 * @param ignoreExceptions If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
 *               they are propagated to the caller.
 * @param advertise "true" if the appender configuration should be
 *            advertised, "false" otherwise.
 * @param advertiseURI The advertised URI which can be used to retrieve the
 *            file contents.
 * @param configuration The Configuration.
 * @return A RollingRandomAccessFileAppender.
 * @deprecated Use {@link #newBuilder()}.
 */
@Deprecated
public static <B extends Builder<B>> RollingRandomAccessFileAppender createAppender(
        final String fileName,
        final String filePattern,
        final String append,
        final String name,
        final String immediateFlush,
        final String bufferSizeStr,
        final TriggeringPolicy policy,
        final RolloverStrategy strategy,
        final Layout<? extends Serializable> layout,
        final Filter filter,
        final String ignoreExceptions,
        final String advertise,
        final String advertiseURI,
        final Configuration configuration) {

    final boolean isAppend = Booleans.parseBoolean(append, true);
    final boolean isIgnoreExceptions = Booleans.parseBoolean(ignoreExceptions, true);
    final boolean isImmediateFlush = Booleans.parseBoolean(immediateFlush, true);
    final boolean isAdvertise = Boolean.parseBoolean(advertise);
    final int bufferSize = Integers.parseInt(bufferSizeStr, RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE);

    return RollingRandomAccessFileAppender.<B>newBuilder()
       .withAdvertise(isAdvertise)
       .withAdvertiseURI(advertiseURI)
       .withAppend(isAppend)
       .withBufferSize(bufferSize)
       .setConfiguration(configuration)
       .withFileName(fileName)
       .withFilePattern(filePattern)
       .withFilter(filter)
       .withIgnoreExceptions(isIgnoreExceptions)
       .withImmediateFlush(isImmediateFlush)
       .withLayout(layout)
       .withName(name)
       .withPolicy(policy)
       .withStrategy(strategy)
       .build();
}