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(); }
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(); }
/** * Creates a new {@link RollingFileAppender} * @param fileName The log file * @param loggingConfig The logging config * @param layout The layout * @param strategy the rollover strategy * @param rolledFileFormat The log file roll pattern * @param trigger The roll trigger * @return the new appender */ protected Appender createRollingRandomAccessFileAppender(final String fileName, Configuration loggingConfig, PatternLayout layout, final DefaultRolloverStrategy strategy, final String rolledFileFormat, final TriggeringPolicy trigger) { RollingRandomAccessFileManager fileManager = RollingRandomAccessFileManager.getRollingRandomAccessFileManager( fileName, rolledFileFormat, true, true, 8192, trigger, strategy, null, layout ); trigger.initialize(fileManager); return RollingRandomAccessFileAppender.createAppender( fileName, // file name rolledFileFormat, // rolled file name pattern "true", // append getClass().getSimpleName(), // appender name "true", // immediate flush "8192", // buffer size trigger, // triggering policy strategy, // rollover strategy layout, // layout null, // filter "true", // ignore exceptions null, // advertise null, // advertise uri loggingConfig); // config }
/** * Creates a new {@link RollingFileAppender} * @param fileName The log file * @param loggingConfig The logging config * @param layout The layout * @param strategy the rollover strategy * @param rolledFileFormat The log file roll pattern * @param trigger The roll trigger * @return the new appender */ protected Appender createRollingFileAppender(final String fileName, Configuration loggingConfig, PatternLayout layout, final DefaultRolloverStrategy strategy, final String rolledFileFormat, final TriggeringPolicy trigger) { RollingFileManager fileManager = RollingFileManager.getFileManager( fileName, rolledFileFormat, false, true, trigger, strategy, null, layout, 8192, true ); trigger.initialize(fileManager); return RollingFileAppender.createAppender( fileName, // file name rolledFileFormat, // rolled file name pattern "true", // append getClass().getSimpleName(), // appender name "true", // buffered io "8192", // buffer size "true", // immediate flush trigger, // triggering policy strategy, // rollover strategy layout, // layout null, // filter "true", // ignore exceptions null, // advertise null, // advertise uri loggingConfig); // config }
/** * 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); }
/** * {@inheritDoc} * @see com.heliosapm.streams.tracing.AbstractMetricWriter#configure(java.util.Properties) */ @Override public void configure(final Properties config) { super.configure(config); final String loggerName = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_LOGGER_NAME, null, config); if(loggerName==null || !LogManager.getContext(true).hasLogger(loggerName)) { /* * =================================================== * FIXME: this is super ugly * =================================================== * TODO: * - log4j2 async appender * - low gc message objects */ final String entryPrefix = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_ENTRY_PREFIX, DEFAULT_ENTRY_PREFIX, config); this.config.put("entryPrefix", entryPrefix); final String entrySuffix = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_ENTRY_SUFFIX, DEFAULT_ENTRY_SUFFIX, config); this.config.put("entrySuffix", entrySuffix); final boolean randomAccessFile = ConfigurationHelper.getBooleanSystemThenEnvProperty(CONFIG_RA_FILE, DEFAULT_RA_FILE, config); this.config.put("randomAccessFile", randomAccessFile); final String fileName = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_FILE_NAME, DEFAULT_FILE_NAME, config); this.config.put("fileName", fileName); final File file = new File(fileName); final File dir = file.getParentFile(); if(dir.exists()) { if(!dir.isDirectory()) throw new IllegalArgumentException("The logging directory is a file [" + dir + "]"); } else { if(!dir.mkdirs()) throw new IllegalArgumentException("Cannot create the logging directory [" + dir + "]"); } LoggerContext context= (LoggerContext) LogManager.getContext(); Configuration loggingConfig = context.getConfiguration(); PatternLayout layout= PatternLayout.createLayout(entryPrefix + "%m" + entrySuffix + "%n" , null, loggingConfig, null, UTF8, false, false, null, null); final DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy("10", "1", null, null, null, true, loggingConfig); // final int lastIndex = fileName.lastIndexOf('.'); final String format = ConfigurationHelper.getSystemThenEnvProperty(CONFIG_ROLL_PATTERN, DEFAULT_ROLL_PATTERN, config); this.config.put("format", format); final StringBuilder b = new StringBuilder(fileName).append(format); // if(lastIndex==-1) { // b.append(".").append(format); // } else { // b.insert(lastIndex, format); // } final String rolledFileFormat = b.toString(); final TriggeringPolicy trigger = TimeBasedTriggeringPolicy.createPolicy("1", "true"); if(randomAccessFile) { appender = createRollingRandomAccessFileAppender(fileName, loggingConfig, layout, strategy, rolledFileFormat, trigger); } else { appender = createRollingFileAppender(fileName, loggingConfig, layout, strategy, rolledFileFormat, trigger); } loggingConfig.addAppender(appender); AppenderRef ref = AppenderRef.createAppenderRef(getClass().getSimpleName(), Level.INFO, null); AppenderRef[] refs = new AppenderRef[] { ref }; LoggerConfig loggerConfig = LoggerConfig.createLogger( false, Level.INFO, getClass().getSimpleName() + "Logger", "false", refs, null, loggingConfig, null); loggerConfig.addAppender(appender, Level.INFO, null); loggingConfig.addLogger(getClass().getSimpleName() + "Logger", loggerConfig); context.updateLoggers(); org.apache.logging.log4j.core.Logger xlogger = context.getLogger(getClass().getName() + "Logger"); for(Appender app: xlogger.getAppenders().values()) { xlogger.removeAppender(app); } xlogger.addAppender(appender); metricLog = context.getLogger(getClass().getName() + "Logger"); } else { metricLog = LogManager.getLogger(loggerName); } }
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!"); }
/** * 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); }
public TriggeringPolicy getPolicy() { return policy; }
public B withPolicy(final TriggeringPolicy policy) { this.policy = policy; return asBuilder(); }
/** * 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 }
/** * 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(); }
/** * Returns the triggering policy. * @param <T> TriggeringPolicy type * @return The TriggeringPolicy */ public <T extends TriggeringPolicy> T getTriggeringPolicy() { return getManager().getTriggeringPolicy(); }
public abstract Builder withPolicy(TriggeringPolicy policy);