org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy Java Examples

The following examples show how to use org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy. 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
Source File: Log4j2Util.java    From summerframework with Apache License 2.0 7 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private static void createAppenderAndStart(String loggerName, String fileName, String filePattern) {

    Layout layout = PatternLayout.newBuilder().withConfiguration(config)
        .withPattern("[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n").build();
    TimeBasedTriggeringPolicy tbtp = TimeBasedTriggeringPolicy.createPolicy(null, null);
    TriggeringPolicy tp = SizeBasedTriggeringPolicy.createPolicy("10M");
    CompositeTriggeringPolicy policyComposite = CompositeTriggeringPolicy.createPolicy(tbtp, tp);

    String loggerDir = datalogDir + File.separator;

    String loggerPathPrefix = loggerDir + File.separator;
    RollingFileAppender.Builder builder = RollingFileAppender.newBuilder().withFilePattern(filePattern)
        .withStrategy(null).withPolicy(policyComposite).withConfiguration(config);
    RollingFileAppender appender = builder.build();
    appender.start();
    config.addAppender(appender);

    AppenderRef ref = AppenderRef.createAppenderRef(loggerName, Level.INFO, null);
    AppenderRef[] refs = new AppenderRef[] {ref};
    LoggerConfig loggerConfig =
        LoggerConfig.createLogger(false, Level.ALL, loggerName, "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger(loggerName, loggerConfig);
    ctx.updateLoggers();
}
 
Example #2
Source File: Log4JLogConfigInitializer.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns a rolling file appender.
 *
 * @param file the file to output to, including path and filename.
 */
private RollingFileAppender getRollingFileAppender( String file )
{
    RollingFileAppender appender = RollingFileAppender.newBuilder().withFileName( file )
        .setName("appender_" + file)
        .withFilePattern( file + "%i")
        .setLayout( PATTERN_LAYOUT )
        .withPolicy(
            SizeBasedTriggeringPolicy.createPolicy( config.getProperty( ConfigurationKey.LOGGING_FILE_MAX_SIZE ) ) )
        .withStrategy( DefaultRolloverStrategy.newBuilder()
            .withMax( config.getProperty( ConfigurationKey.LOGGING_FILE_MAX_ARCHIVES ) ).build() )
        .build();

    appender.start();
    return appender;
}
 
Example #3
Source File: Log4j1ConfigurationFactoryTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #4
Source File: Log4j1ConfigurationFactoryTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #5
Source File: RollingFileAppenderBuilder.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private Appender createAppender(final String name, final Log4j1Configuration config, final Layout layout,
        final Filter filter, final boolean bufferedIo, boolean immediateFlush, final String fileName,
        final String level, final String maxSize, final String maxBackups) {
    org.apache.logging.log4j.core.Layout<?> fileLayout = null;
    if (bufferedIo) {
        immediateFlush = true;
    }
    if (layout instanceof LayoutWrapper) {
        fileLayout = ((LayoutWrapper) layout).getLayout();
    } else if (layout != null) {
        fileLayout = new LayoutAdapter(layout);
    }
    org.apache.logging.log4j.core.Filter fileFilter = buildFilters(level, filter);
    if (fileName == null) {
        LOGGER.warn("Unable to create File Appender, no file name provided");
        return null;
    }
    String filePattern = fileName +"%d{yyy-MM-dd}";
    TriggeringPolicy timePolicy = TimeBasedTriggeringPolicy.newBuilder().setModulate(true).build();
    SizeBasedTriggeringPolicy sizePolicy = SizeBasedTriggeringPolicy.createPolicy(maxSize);
    CompositeTriggeringPolicy policy = CompositeTriggeringPolicy.createPolicy(sizePolicy, timePolicy);
    RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
            .setConfig(config)
            .setMax(maxBackups)
            .build();
    return new AppenderWrapper(RollingFileAppender.newBuilder()
            .setName(name)
            .setConfiguration(config)
            .setLayout(fileLayout)
            .setFilter(fileFilter)
            .setBufferedIo(bufferedIo)
            .setImmediateFlush(immediateFlush)
            .setFileName(fileName)
            .setFilePattern(filePattern)
            .setPolicy(policy)
            .setStrategy(strategy)
            .build());
}
 
Example #6
Source File: CloudStorageLoggerFactory.java    From ambari-logsearch with Apache License 2.0 4 votes vote down vote up
public static Logger createLogger(Input input, LoggerContext loggerContext, LogFeederProps logFeederProps) {
  boolean useJsonFormat = logFeederProps.isCloudStorageUseFilters();
  String type = input.getLogType().replace(LogFeederConstants.CLOUD_PREFIX, "");
  String uniqueThreadName = input.getThread().getName();
  Configuration config = loggerContext.getConfiguration();
  String baseDir = logFeederProps.getRolloverConfig().getRolloverArchiveBaseDir();
  String destination = logFeederProps.getCloudStorageDestination().getText();
  String clusterHostnameBaseDir = Paths.get(baseDir, destination, logFeederProps.getClusterName(), LogFeederUtil.hostName).toFile().getAbsolutePath();
  String activeLogDir = Paths.get(clusterHostnameBaseDir, ACTIVE_FOLDER, type).toFile().getAbsolutePath();
  String archiveLogDir = Paths.get(clusterHostnameBaseDir, ARCHIVED_FOLDER, type).toFile().getAbsolutePath();

  boolean useGzip = logFeederProps.getRolloverConfig().isUseGzip();
  final String archiveFilePattern;
  if (useJsonFormat) {
    archiveFilePattern = useGzip ? JSON_DATE_PATTERN_SUFFIX_GZ : JSON_DATE_PATTERN_SUFFIX;
  } else {
    archiveFilePattern = useGzip ? DATE_PATTERN_SUFFIX_GZ : DATE_PATTERN_SUFFIX;
  }

  String logSuffix = useJsonFormat ? ".json" : ".log";
  String fileName = String.join(File.separator, activeLogDir, type + logSuffix);
  String filePattern = String.join(File.separator, archiveLogDir, type + archiveFilePattern);
  PatternLayout layout = PatternLayout.newBuilder()
    .withPattern(PatternLayout.DEFAULT_CONVERSION_PATTERN).build();

  String rolloverSize = logFeederProps.getRolloverConfig().getRolloverSize().toString() + logFeederProps.getRolloverConfig().getRolloverSizeFormat();
  SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = SizeBasedTriggeringPolicy.createPolicy(rolloverSize);

  final Integer thresholdMin = logFeederProps.getRolloverConfig().getRolloverThresholdTimeMins();
  final Integer thresholdInterval = thresholdMin * 60000; // 1 min = 60000 milliseconds

  TimeBasedTriggeringPolicy timeBasedTriggeringPolicy = TimeBasedTriggeringPolicy.newBuilder()
    .withInterval(thresholdInterval)
    .build();

  final CompositeTriggeringPolicy compositeTriggeringPolicy;

  if (logFeederProps.getRolloverConfig().isRolloverOnStartup()) {
    OnStartupTriggeringPolicy onStartupTriggeringPolicy = OnStartupTriggeringPolicy.createPolicy(1);
    compositeTriggeringPolicy = CompositeTriggeringPolicy
      .createPolicy(sizeBasedTriggeringPolicy, timeBasedTriggeringPolicy, onStartupTriggeringPolicy);
  } else {
    compositeTriggeringPolicy = CompositeTriggeringPolicy
      .createPolicy(sizeBasedTriggeringPolicy, timeBasedTriggeringPolicy);
  }

  DefaultRolloverStrategy defaultRolloverStrategy = DefaultRolloverStrategy.newBuilder()
    .withMax(String.valueOf(logFeederProps.getRolloverConfig().getRolloverMaxBackupFiles()))
    .build();

  boolean immediateFlush = logFeederProps.getRolloverConfig().isImmediateFlush();
  RollingFileAppender appender = RollingFileAppender.newBuilder()
    .withFileName(fileName)
    .withFilePattern(filePattern)
    .withLayout(layout)
    .withName(type)
    .withPolicy(compositeTriggeringPolicy)
    .withStrategy(defaultRolloverStrategy)
    .withImmediateFlush(immediateFlush)
    .build();

  appender.start();
  config.addAppender(appender);

  AppenderRef ref = AppenderRef.createAppenderRef(type, null, null);
  AppenderRef[] refs = new AppenderRef[] {ref};

  LoggerConfig loggerConfig = LoggerConfig
    .createLogger(false, Level.ALL, input.getThread().getName(),
      "true", refs, null, config, null);
  loggerConfig.addAppender(appender, null, null);
  config.addLogger(uniqueThreadName, loggerConfig);
  loggerContext.updateLoggers();
  return loggerContext.getLogger(uniqueThreadName);
}
 
Example #7
Source File: ApsSystemUtils.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void init() throws Exception {
    String active = (String) this.systemParams.get(INIT_PROP_LOG_ACTIVE_FILE_OUTPUT);
    if (StringUtils.isEmpty(active) || !active.equalsIgnoreCase("true")) {
        return;
    }
    String appenderName = "ENTANDO";
    String conversionPattern = (String) this.systemParams.get("log4jConversionPattern");
    if (StringUtils.isBlank(conversionPattern)) {
        conversionPattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} - %-5p -  %c - %m%n";
    }
    String maxFileSize = (String) this.systemParams.get(INIT_PROP_LOG_FILE_SIZE);
    if (StringUtils.isBlank(maxFileSize)) {
        maxFileSize = "1MB"; //default size
    } else {
        long mega = new Long(maxFileSize) / KILOBYTE;
        maxFileSize = mega + "KB";
    }
    String filePattern = (String) this.systemParams.get(INIT_PROP_LOG_FILE_PATTERN);
    String filename = (String) this.systemParams.get(INIT_PROP_LOG_NAME);
    int maxBackupIndex = Integer.parseInt((String) this.systemParams.get(INIT_PROP_LOG_FILES_COUNT));
    String log4jLevelString = (String) this.systemParams.get(INIT_PROP_LOG_LEVEL);
    if (StringUtils.isBlank(log4jLevelString)) {
        log4jLevelString = "INFO"; //default level
    }
    Configurator.setRootLevel(Level.getLevel(log4jLevelString));
    LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    loggerContext.getRootLogger().setLevel(Level.getLevel(log4jLevelString));
    Configurator.setAllLevels(loggerContext.getRootLogger().getName(), Level.getLevel(log4jLevelString));
    Configuration configuration = loggerContext.getConfiguration();
    RollingFileAppender fileAppender = (RollingFileAppender) configuration.getAppender(appenderName);
    if (null == fileAppender) {
        PathCondition[] pathConditions = new PathCondition[]{IfAccumulatedFileCount.createFileCountCondition(maxBackupIndex)};
        String basePath = filePattern.substring(0, filePattern.lastIndexOf(File.separator));
        DeleteAction deleteAction = DeleteAction.createDeleteAction(basePath, true, 1, false, null, pathConditions, null, configuration);
        SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(maxFileSize);
        PatternLayout layout = PatternLayout.newBuilder().withPattern(conversionPattern).build();
        DefaultRolloverStrategy strategy = DefaultRolloverStrategy.newBuilder()
                .withConfig(configuration).withMax(String.valueOf(maxBackupIndex))
                .withCustomActions(new Action[]{deleteAction}).build();
        fileAppender = RollingFileAppender.newBuilder()
                .withName(appenderName)
                .setConfiguration(configuration)
                .withLayout(layout)
                .withFileName(filename)
                .withFilePattern(filePattern)
                .withPolicy(policy)
                .withStrategy(strategy)
                .build();
        configuration.addAppender(fileAppender);
        Configurator.setLevel(appenderName, Level.getLevel(log4jLevelString));
        fileAppender.start();
    }
    AsyncAppender async = (AsyncAppender) loggerContext.getRootLogger().getAppenders().get("async");
    if (null == async) {
        AppenderRef ref = AppenderRef.createAppenderRef(appenderName, Level.getLevel(log4jLevelString), null);
        async = AsyncAppender.newBuilder().setName("async")
                .setConfiguration(configuration)
                .setAppenderRefs(new AppenderRef[]{ref}).build();
        configuration.addAppender(async);
        loggerContext.getRootLogger().addAppender(async);
        async.start();
    }
    loggerContext.updateLoggers();
}
 
Example #8
Source File: ReconfigureAppenderTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
private void createAndAddAppender()
{
	ConfigurationBuilder<BuiltConfiguration> config_builder =
		ConfigurationBuilderFactory.newConfigurationBuilder();

	// All loggers must have a root logger. The default root logger logs ERRORs to the console.
	// Override this with a root logger that does not log anywhere as we leave it up the the
	// appenders on the logger.
	config_builder.add(config_builder.newRootLogger(Level.INFO));

	// Initialise the logger context.
	LoggerContext logger_context =
		Configurator.initialize(config_builder.build());

	// Retrieve the logger.
	Logger logger = (Logger) LogManager.getLogger(this.getClass());

	Builder pattern_builder = PatternLayout.newBuilder().setPattern(
		"[%d{dd-MM-yy HH:mm:ss}] %p %m %throwable %n");

	PatternLayout pattern_layout = (PatternLayout) pattern_builder.build();

	appender = RollingFileAppender
		.newBuilder()
		.setLayout(pattern_layout)
		.setName("rollingfileappender")
		.setFilePattern("target/filepattern.%i.log")
		.setPolicy(SizeBasedTriggeringPolicy.createPolicy("5 MB"))
		.setAppend(true)
		.setStrategy(
			DirectWriteRolloverStrategy
				.newBuilder()
				.setConfig(logger_context.getConfiguration())
				.setMaxFiles("5")
				.build())
		.setConfiguration(logger_context.getConfiguration())
		.build();

	appender.start();

	logger.addAppender(appender);
}