Java Code Examples for org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy#createPolicy()
The following examples show how to use
org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy#createPolicy() .
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 |
@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: RollingFileAppenderBuilder.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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 3
Source File: CloudStorageLoggerFactory.java From ambari-logsearch with Apache License 2.0 | 4 votes |
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 4
Source File: ApsSystemUtils.java From entando-core with GNU Lesser General Public License v3.0 | 4 votes |
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(); }