private RollingRandomAccessFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter, final RollingRandomAccessFileManager manager, final String fileName, final String filePattern, final boolean ignoreExceptions, final boolean immediateFlush, final int bufferSize, 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); } else { advertisement = null; } this.fileName = fileName; this.filePattern = filePattern; this.advertiser = advertiser; }
/** * Write the log entry rolling over the file when required. * * @param event The LogEvent. */ @Override public void append(final LogEvent event) { final RollingRandomAccessFileManager manager = getManager(); manager.checkRollover(event); // Leverage the nice batching behaviour of async Loggers/Appenders: // we can signal the file manager that it needs to flush the buffer // to disk at the end of a batch. // From a user's point of view, this means that all log events are // _always_ available in the log file, without incurring the overhead // of immediateFlush=true. manager.setEndOfBatch(event.isEndOfBatch()); // FIXME manager's EndOfBatch threadlocal can be deleted // LOG4J2-1292 utilize gc-free Layout.encode() method: taken care of in superclass super.append(event); }
/** * 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 }
/** * Write the log entry rolling over the file when required. * * @param event The LogEvent. */ @Override public void append(final LogEvent event) { final RollingRandomAccessFileManager manager = (RollingRandomAccessFileManager) getManager(); manager.checkRollover(event); // Leverage the nice batching behaviour of async Loggers/Appenders: // we can signal the file manager that it needs to flush the buffer // to disk at the end of a batch. // From a user's point of view, this means that all log events are // _always_ available in the log file, without incurring the overhead // of immediateFlush=true. manager.setEndOfBatch(event.isEndOfBatch()); super.append(event); }
public Builder() { super(); withBufferSize(RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE); withIgnoreExceptions(true); withImmediateFlush(true); }
@Override public RollingRandomAccessFileAppender build() { final String name = getName(); if (name == null) { LOGGER.error("No name provided for FileAppender"); 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; } 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; } final Layout<? extends Serializable> layout = getOrCreateLayout(); final boolean immediateFlush = isImmediateFlush(); final int bufferSize = getBufferSize(); final RollingRandomAccessFileManager manager = RollingRandomAccessFileManager .getRollingRandomAccessFileManager(fileName, filePattern, append, immediateFlush, bufferSize, policy, strategy, advertiseURI, layout, filePermissions, fileOwner, fileGroup, getConfiguration()); if (manager == null) { return null; } manager.initialize(); return new RollingRandomAccessFileAppender(name, layout,getFilter(), manager, fileName, filePattern, isIgnoreExceptions(), immediateFlush, bufferSize, advertise ? getConfiguration().getAdvertiser() : null); }
/** * 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(); }