private RollingRandomAccessFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, final RollingFileManager manager, final String fileName, final String filePattern, final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) { super(name, layout, filter, ignoreExceptions, immediateFlush, manager); if (advertiser != null) { final Map<String, String> configuration = new HashMap<String, String>( layout.getContentFormat()); configuration.put("contentType", layout.getContentType()); configuration.put("name", name); advertisement = advertiser.advertise(configuration); } this.fileName = fileName; this.filePattern = filePattern; this.advertiser = advertiser; }
/** * 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 }
/** * Perform the rollover. * @param manager The RollingFileManager name for current active log file. * @return A RolloverDescription. * @throws SecurityException if an error occurs. */ @Override public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException { if (maxIndex < 0) { return null; } final long start = System.nanoTime(); final int fileIndex = purge(minIndex, maxIndex, manager); if (fileIndex < 0) { return null; } if (LOGGER.isTraceEnabled()) { final double duration = (System.nanoTime() - start) / (1000.0 * 1000.0 * 1000.0); LOGGER.trace("DefaultRolloverStrategy.purge() took {} seconds", duration); } final StringBuilder buf = new StringBuilder(255); manager.getPatternProcessor().formatFileName(subst, buf, fileIndex); final String currentFileName = manager.getFileName(); String renameTo = buf.toString(); final String compressedName = renameTo; Action compressAction = null; if (renameTo.endsWith(EXT_GZIP)) { renameTo = renameTo.substring(0, renameTo.length() - EXT_GZIP.length()); compressAction = new GzCompressAction(new File(renameTo), new File(compressedName), true); } else if (renameTo.endsWith(EXT_ZIP)) { renameTo = renameTo.substring(0, renameTo.length() - EXT_ZIP.length()); compressAction = new ZipCompressAction(new File(renameTo), new File(compressedName), true, compressionLevel); } final FileRenameAction renameAction = new FileRenameAction(new File(currentFileName), new File(renameTo), false); return new RolloverDescriptionImpl(currentFileName, false, renameAction, compressAction); }
private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, final RollingFileManager manager, final String fileName, final String filePattern, final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) { super(name, layout, filter, ignoreExceptions, immediateFlush, manager); if (advertiser != null) { final Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat()); configuration.put("contentType", layout.getContentType()); configuration.put("name", name); advertisement = advertiser.advertise(configuration); } this.fileName = fileName; this.filePattern = filePattern; this.advertiser = advertiser; }
private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, final RollingFileManager manager, final String fileName, final String filePattern, final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) { super(name, layout, filter, ignoreExceptions, immediateFlush, manager); if (advertiser != null) { final Map<String, String> configuration = new HashMap<>(layout.getContentFormat()); configuration.put("contentType", layout.getContentType()); configuration.put("name", name); advertisement = advertiser.advertise(configuration); } this.fileName = fileName; this.filePattern = filePattern; this.advertiser = advertiser; }
@Override public final void initialize(final RollingFileManager manager) { this.manager = manager; }
private int purge(final int lowIndex, final int highIndex, final RollingFileManager manager) { return useMax ? purgeAscending(lowIndex, highIndex, manager) : purgeDescending(lowIndex, highIndex, manager); }
/** * Write the log entry rolling over the file when required. * @param event The LogEvent. */ @Override public void append(final LogEvent event) { ((RollingFileManager) getManager()).checkRollover(event); super.append(event); }
/** * 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); }
@Override public RollingFileAppender build() { // Even though some variables may be annotated with @Required, we must still perform validation here for // call sites that build builders programmatically. final boolean isBufferedIo = isBufferedIo(); final int bufferSize = getBufferSize(); if (getName() == null) { LOGGER.error("RollingFileAppender '{}': No name provided.", getName()); return null; } if (!isBufferedIo && bufferSize > 0) { LOGGER.warn("RollingFileAppender '{}': The bufferSize is set to {} but bufferedIO is not true", getName(), bufferSize); } if (filePattern == null) { LOGGER.error("RollingFileAppender '{}': No file name pattern provided.", getName()); return null; } if (policy == null) { LOGGER.error("RollingFileAppender '{}': No TriggeringPolicy provided.", getName()); return null; } if (strategy == null) { if (fileName != null) { strategy = DefaultRolloverStrategy.newBuilder() .withCompressionLevelStr(String.valueOf(Deflater.DEFAULT_COMPRESSION)) .withConfig(getConfiguration()) .build(); } else { strategy = DirectWriteRolloverStrategy.newBuilder() .withCompressionLevelStr(String.valueOf(Deflater.DEFAULT_COMPRESSION)) .withConfig(getConfiguration()) .build(); } } else if (fileName == null && !(strategy instanceof DirectFileRolloverStrategy)) { LOGGER.error("RollingFileAppender '{}': When no file name is provided a DirectFilenameRolloverStrategy must be configured"); return null; } final Layout<? extends Serializable> layout = getOrCreateLayout(); final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, append, isBufferedIo, policy, strategy, advertiseUri, layout, bufferSize, isImmediateFlush(), createOnDemand, filePermissions, fileOwner, fileGroup, getConfiguration()); if (manager == null) { return null; } manager.initialize(); return new RollingFileAppender(getName(), layout, getFilter(), manager, fileName, filePattern, isIgnoreExceptions(), isImmediateFlush(), advertise ? getConfiguration().getAdvertiser() : null); }
@Override public void initialize(RollingFileManager rfm) { }