Search in sources :

Example 1 with DefaultRolloverStrategy

use of org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy in project logging-log4j2 by apache.

the class Log4j1ConfigurationFactoryTest method testRollingFileAppender.

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();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) NullAppender(org.apache.logging.log4j.core.appender.NullAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy) RolloverStrategy(org.apache.logging.log4j.core.appender.rolling.RolloverStrategy) SizeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy) CompositeTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy) TriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy) CompositeTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy) TimeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy) SizeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy)

Example 2 with DefaultRolloverStrategy

use of org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy in project logging-log4j2 by apache.

the class Log4j1ConfigurationFactoryTest method testDailyRollingFileAppender.

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();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) NullAppender(org.apache.logging.log4j.core.appender.NullAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) TimeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy) Configuration(org.apache.logging.log4j.core.config.Configuration) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy) RolloverStrategy(org.apache.logging.log4j.core.appender.rolling.RolloverStrategy) CompositeTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy) TriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy) CompositeTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy) TimeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy) SizeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy)

Example 3 with DefaultRolloverStrategy

use of org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy in project incubator-servicecomb-java-chassis by apache.

the class Log4J2MetricsFileWriter method initLogger.

@SuppressWarnings("deprecation")
private RollingFileAppender initLogger(String loggerName, String filePrefix) {
    String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString();
    String filePattern = Paths.get(rootPath, filePrefix + "." + loggerName + "-%i.dat").toString();
    PatternLayout layout = PatternLayout.newBuilder().withPattern(PatternLayout.DEFAULT_CONVERSION_PATTERN).build();
    SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize);
    DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(String.valueOf(maxFileCount), null, null, null, null, false, config);
    // TODO:use RollingFileAppender.newBuilder throw No such static method exception,will improve later!
    return RollingFileAppender.createAppender(fileName, filePattern, "true", loggerName, "true", null, "true", policy, strategy, layout, null, null, null, null, config);
}
Also used : PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) SizeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy)

Example 4 with DefaultRolloverStrategy

use of org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy in project logging-log4j2 by apache.

the class AbstractLog4j1ConfigurationTest method testDailyRollingFileAppender.

private void testDailyRollingFileAppender(final String configResource, final String name, final String filePattern) throws Exception {
    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();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) NullAppender(org.apache.logging.log4j.core.appender.NullAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) TimeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy) Configuration(org.apache.logging.log4j.core.config.Configuration) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy) RolloverStrategy(org.apache.logging.log4j.core.appender.rolling.RolloverStrategy) CompositeTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy) TriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy) CompositeTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy) TimeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy) SizeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy)

Example 5 with DefaultRolloverStrategy

use of org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy in project neo4j by neo4j.

the class LogConfig method createRollingFileAppender.

private static Appender createRollingFileAppender(Builder builder, Layout<String> layout) {
    long rotationThreshold = builder.rotationThreshold;
    int maxArchives = builder.maxArchives;
    if (builder.rotationThreshold == 0 || builder.maxArchives == 0) {
        // Should not rotate - set threshold that won't be reached.
        rotationThreshold = Long.MAX_VALUE;
        maxArchives = 1;
    }
    SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(String.valueOf(rotationThreshold));
    DefaultRolloverStrategy rolloverStrategy = DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(maxArchives)).withFileIndex("min").build();
    return RollingFileAppender.newBuilder().setName(APPENDER_NAME).setLayout(layout).withCreateOnDemand(builder.createOnDemand).withFileName(builder.logPath.toString()).withFilePattern(builder.logPath + ".%i").withPolicy(policy).withStrategy(rolloverStrategy).build();
}
Also used : SizeBasedTriggeringPolicy(org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy) DefaultRolloverStrategy(org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy)

Aggregations

DefaultRolloverStrategy (org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy)6 SizeBasedTriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy)6 Appender (org.apache.logging.log4j.core.Appender)4 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 NullAppender (org.apache.logging.log4j.core.appender.NullAppender)4 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)4 CompositeTriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.CompositeTriggeringPolicy)4 RolloverStrategy (org.apache.logging.log4j.core.appender.rolling.RolloverStrategy)4 TimeBasedTriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy)4 TriggeringPolicy (org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy)4 Configuration (org.apache.logging.log4j.core.config.Configuration)4 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)1