org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy Java Examples
The following examples show how to use
org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy.
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: Log4j1ConfigurationFactoryTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
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(); }
Example #3
Source File: Log4j1ConfigurationFactoryTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
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(); }
Example #4
Source File: DailyRollingFileAppenderBuilder.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private <T extends Log4j1Configuration> Appender createAppender(final String name, final Layout layout, final Filter filter, final String fileName, final boolean append, boolean immediateFlush, final String level, final boolean bufferedIo, final int bufferSize, final T configuration) { 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 policy = TimeBasedTriggeringPolicy.newBuilder().setModulate(true).build(); RolloverStrategy strategy = DefaultRolloverStrategy.newBuilder() .setConfig(configuration) .setMax(Integer.toString(Integer.MAX_VALUE)) .build(); return new AppenderWrapper(RollingFileAppender.newBuilder() .setName(name) .setConfiguration(configuration) .setLayout(fileLayout) .setFilter(fileFilter) .setFileName(fileName) .setBufferSize(bufferSize) .setImmediateFlush(immediateFlush) .setFilePattern(filePattern) .setPolicy(policy) .setStrategy(strategy) .build()); }
Example #5
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 #6
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 #7
Source File: LoggerLoader.java From Zebra with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) public static synchronized void init() { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); Layout layout = PatternLayout.newBuilder().withPattern("%d{yyyy-MM-dd HH:mm:ss}:%p %t %c - %m%n") .withConfiguration(config).withRegexReplacement(null).withCharset(null).withAlwaysWriteExceptions(true) .withNoConsoleNoAnsi(false).withHeader(null).withFooter(null).build(); // file info Filter fileInfoFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.ACCEPT); Appender fileInfoAppender = RollingFileAppender.createAppender(LOG_ROOT + "/zebra.log", LOG_ROOT + "/zebra.log.%d{yyyy-MM-dd}.gz", "true", "FileInfo", "true", "4000", "true", TimeBasedTriggeringPolicy.createPolicy("1", "true"), ZebraRolloverStrategy.createStrategy("30", "1", null, Deflater.DEFAULT_COMPRESSION + "", config), layout, fileInfoFilter, "false", null, null, config); config.addAppender(fileInfoAppender); fileInfoAppender.start(); AppenderRef fileInfoRef = AppenderRef.createAppenderRef("FileInfo", null, fileInfoFilter); // console error Appender consoleErrorAppender = ConsoleAppender.createAppender(layout, null, "SYSTEM_ERR", "ConsoleError", "false", "false"); config.addAppender(consoleErrorAppender); consoleErrorAppender.start(); // console info Filter consoleWarnFilter = ThresholdFilter.createFilter(Level.ERROR, Result.DENY, Result.NEUTRAL); Appender consoleWarnAppender = ConsoleAppender.createAppender(layout, consoleWarnFilter, "SYSTEM_OUT", "ConsoleWarn", "false", "false"); config.addAppender(consoleWarnAppender); consoleWarnAppender.start(); AppenderRef consoleWarnAppenderRef = AppenderRef.createAppenderRef("ConsoleWarn", Level.WARN, consoleWarnFilter); AppenderRef consoleErrorAppenderRef = AppenderRef.createAppenderRef("ConsoleError", Level.WARN, null); AppenderRef[] refs = new AppenderRef[] { consoleErrorAppenderRef, consoleWarnAppenderRef, fileInfoRef }; LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "com.dianping.zebra", "true", refs, null, config, null); loggerConfig.addAppender(consoleErrorAppender, Level.ERROR, null); loggerConfig.addAppender(consoleWarnAppender, Level.INFO, null); loggerConfig.addAppender(fileInfoAppender, Level.INFO, null); config.addLogger("com.dianping.zebra", loggerConfig); ctx.updateLoggers(); context = ctx; }