/** * Create an Agent. * @param host The host name. * @param port The port number. * @return The Agent. */ @PluginFactory public static Agent createAgent(@PluginAttribute("host") String host, @PluginAttribute("port") final String port) { if (host == null) { host = DEFAULT_HOST; } int portNum; try { portNum = Integers.parseInt(port, DEFAULT_PORT); } catch (final Exception ex) { LOGGER.error("Error parsing port number " + port, ex); return null; } return new Agent(host, portNum); }
@Override public DirectWriteRolloverStrategy build() { int maxIndex = Integer.MAX_VALUE; if (maxFiles != null) { maxIndex = Integer.parseInt(maxFiles); if (maxIndex < 0) { maxIndex = Integer.MAX_VALUE; } else if (maxIndex < 2) { LOGGER.error("Maximum files too small. Limited to " + DEFAULT_MAX_FILES); maxIndex = DEFAULT_MAX_FILES; } } final int compressionLevel = Integers.parseInt(compressionLevelStr, Deflater.DEFAULT_COMPRESSION); return new DirectWriteRolloverStrategy(maxIndex, compressionLevel, config.getStrSubstitutor(), customActions, stopCustomActionsOnError, tempCompressedFilePattern); }
/** * Converts the specified region length to a valid value. */ private static int determineValidRegionLength(final String name, final int regionLength) { if (regionLength > MAX_REGION_LENGTH) { LOGGER.info("MemoryMappedAppender[{}] Reduced region length from {} to max length: {}", name, regionLength, MAX_REGION_LENGTH); return MAX_REGION_LENGTH; } if (regionLength < MIN_REGION_LENGTH) { LOGGER.info("MemoryMappedAppender[{}] Expanded region length from {} to min length: {}", name, regionLength, MIN_REGION_LENGTH); return MIN_REGION_LENGTH; } final int result = Integers.ceilingNextPowerOfTwo(regionLength); if (regionLength != result) { LOGGER.info("MemoryMappedAppender[{}] Rounded up region length from {} to next power of two: {}", name, regionLength, result); } return result; }
static int calculateRingBufferSize(final String propertyName) { int ringBufferSize = Constants.ENABLE_THREADLOCALS ? RINGBUFFER_NO_GC_DEFAULT_SIZE : RINGBUFFER_DEFAULT_SIZE; final String userPreferredRBSize = PropertiesUtil.getProperties().getStringProperty(propertyName, String.valueOf(ringBufferSize)); try { int size = Integer.parseInt(userPreferredRBSize); if (size < RINGBUFFER_MIN_SIZE) { size = RINGBUFFER_MIN_SIZE; LOGGER.warn("Invalid RingBufferSize {}, using minimum size {}.", userPreferredRBSize, RINGBUFFER_MIN_SIZE); } ringBufferSize = size; } catch (final Exception ex) { LOGGER.warn("Invalid RingBufferSize {}, using default size {}.", userPreferredRBSize, ringBufferSize); } return Integers.ceilingNextPowerOfTwo(ringBufferSize); }
/** * Load the value of {@link #structureId} from {@link #dataFile}. * * @return the value for {@link #structureId} * @throws IOException if an I/O error occurs while reading {@link #dataFile} */ private int loadStructureId() throws IOException { ByteBuffer buffer = ByteBuffer.allocate(Ints.checkedCast(dataFileChannel.size())); dataFileChannel.read(buffer); String fileContent = new String(buffer.array(), Charsets.UTF_8); return Integers.parseInt(fileContent); }
/** * Create the DefaultRolloverStrategy. * @param max The maximum number of files to keep. * @param min The minimum number of files to keep. * @param fileIndex If set to "max" (the default), files with a higher index will be newer than files with a * smaller index. If set to "min", file renaming and the counter will follow the Fixed Window strategy. * @param compressionLevelStr The compression level, 0 (less) through 9 (more); applies only to ZIP files. * @param config The Configuration. * @return A DefaultRolloverStrategy. */ @PluginFactory public static ZebraRolloverStrategy createStrategy( @PluginAttribute("max") final String max, @PluginAttribute("min") final String min, @PluginAttribute("fileIndex") final String fileIndex, @PluginAttribute("compressionLevel") final String compressionLevelStr, @PluginConfiguration final Configuration config) { final boolean useMax = fileIndex == null ? true : fileIndex.equalsIgnoreCase("max"); int minIndex = MIN_WINDOW_SIZE; if (min != null) { minIndex = Integer.parseInt(min); if (minIndex < 1) { LOGGER.error("Minimum window size too small. Limited to " + MIN_WINDOW_SIZE); minIndex = MIN_WINDOW_SIZE; } } int maxIndex = DEFAULT_WINDOW_SIZE; if (max != null) { maxIndex = Integer.parseInt(max); if (maxIndex < minIndex) { maxIndex = minIndex < DEFAULT_WINDOW_SIZE ? DEFAULT_WINDOW_SIZE : minIndex; LOGGER.error("Maximum window size must be greater than the minimum windows size. Set to " + maxIndex); } } final int compressionLevel = Integers.parseInt(compressionLevelStr, Deflater.DEFAULT_COMPRESSION); return new ZebraRolloverStrategy(minIndex, maxIndex, useMax, compressionLevel, config.getStrSubstitutor()); }
@PluginFactory public static FileLogAppender createAppender(@PluginAttribute("fileName") String fileName, @PluginAttribute("filePattern") String filePattern, @PluginAttribute("append") String append, @PluginAttribute("name") String name, @PluginAttribute("immediateFlush") String immediateFlush, @PluginAttribute("bufferSize") String bufferSizeStr, @PluginElement("Policy") TriggeringPolicy policy, @PluginElement("Strategy") RolloverStrategy strategy, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") Filter filter, @PluginAttribute("ignoreExceptions") String ignore, @PluginAttribute("advertise") String advertise, @PluginAttribute("advertiseURI") String advertiseURI, @PluginConfiguration Configuration config) { boolean isAppend = Booleans.parseBoolean(append, true); boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); boolean isFlush = Booleans.parseBoolean(immediateFlush, true); boolean isAdvertise = Boolean.parseBoolean(advertise); int bufferSize = Integers.parseInt(bufferSizeStr, 262144); if (name == null) { LOGGER.error("No name provided for FileAppender"); return null; } else if (fileName == null) { LOGGER.error("No filename was provided for FileAppender with name " + name); return null; } else if (filePattern == null) { LOGGER.error("No filename pattern provided for FileAppender with name " + name); return null; } else if (policy == null) { LOGGER.error("A TriggeringPolicy must be provided"); return null; } else { if (strategy == null) { strategy = DefaultRolloverStrategy.createStrategy(null, null, null, String.valueOf(-1), config); } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } RollingRandomAccessFileManager manager = RollingRandomAccessFileManager.getRollingRandomAccessFileManager(fileName, filePattern, isAppend, isFlush, bufferSize, policy, strategy, advertiseURI, layout); return manager == null ? null : new FileLogAppender(name, layout, filter, manager, fileName, filePattern, ignoreExceptions, isFlush, bufferSize, isAdvertise ? config.getAdvertiser() : null); } }
public static int parseInt(final String s, final int defaultValue) { try { return Integers.parseInt(s, defaultValue); } catch (final NumberFormatException e) { LOGGER.error("Could not parse \"{}\" as an integer, using default value {}: {}", s, defaultValue, e); return defaultValue; } }
/** * Creates a TimeBasedTriggeringPolicy. * @param interval The interval between rollovers. * @param modulate If true the time will be rounded to occur on a boundary aligned with the increment. * @return a TimeBasedTriggeringPolicy. * @deprecated Use {@link #newBuilder()}. */ @Deprecated public static TimeBasedTriggeringPolicy createPolicy( @PluginAttribute("interval") final String interval, @PluginAttribute("modulate") final String modulate) { return newBuilder() .withInterval(Integers.parseInt(interval, 1)) .withModulate(Boolean.parseBoolean(modulate)) .build(); }
@Override public DefaultRolloverStrategy build() { int minIndex; int maxIndex; boolean useMax; if (fileIndex != null && fileIndex.equalsIgnoreCase("nomax")) { minIndex = Integer.MIN_VALUE; maxIndex = Integer.MAX_VALUE; useMax = false; } else { useMax = fileIndex == null ? true : fileIndex.equalsIgnoreCase("max"); minIndex = MIN_WINDOW_SIZE; if (min != null) { minIndex = Integer.parseInt(min); if (minIndex < 1) { LOGGER.error("Minimum window size too small. Limited to " + MIN_WINDOW_SIZE); minIndex = MIN_WINDOW_SIZE; } } maxIndex = DEFAULT_WINDOW_SIZE; if (max != null) { maxIndex = Integer.parseInt(max); if (maxIndex < minIndex) { maxIndex = minIndex < DEFAULT_WINDOW_SIZE ? DEFAULT_WINDOW_SIZE : minIndex; LOGGER.error("Maximum window size must be greater than the minimum windows size. Set to " + maxIndex); } } } final int compressionLevel = Integers.parseInt(compressionLevelStr, Deflater.DEFAULT_COMPRESSION); return new DefaultRolloverStrategy(minIndex, maxIndex, useMax, compressionLevel, config.getStrSubstitutor(), customActions, stopCustomActionsOnError, tempCompressedFilePattern); }
/** * Create a Memory Mapped File Appender. * * @param fileName The name and path of the file. * @param append "True" if the file should be appended to, "false" if it should be overwritten. The default is * "true". * @param name The name of the Appender. * @param immediateFlush "true" if the contents should be flushed on every write, "false" otherwise. The default is * "false". * @param regionLengthStr The buffer size, defaults to {@value MemoryMappedFileManager#DEFAULT_REGION_LENGTH}. * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they * are propagated to the caller. * @param layout The layout to use to format the event. If no layout is provided the default PatternLayout will be * used. * @param filter The filter, if any, to use. * @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 The FileAppender. * @deprecated Use {@link #newBuilder()}. */ @Deprecated public static <B extends Builder<B>> MemoryMappedFileAppender createAppender( // @formatter:off final String fileName, // final String append, // final String name, // final String immediateFlush, // final String regionLengthStr, // final String ignore, // final Layout<? extends Serializable> layout, // final Filter filter, // final String advertise, // final String advertiseURI, // final Configuration config) { // @formatter:on final boolean isAppend = Booleans.parseBoolean(append, true); final boolean isImmediateFlush = Booleans.parseBoolean(immediateFlush, false); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final boolean isAdvertise = Boolean.parseBoolean(advertise); final int regionLength = Integers.parseInt(regionLengthStr, MemoryMappedFileManager.DEFAULT_REGION_LENGTH); // @formatter:off return MemoryMappedFileAppender.<B>newBuilder() .setAdvertise(isAdvertise) .setAdvertiseURI(advertiseURI) .setAppend(isAppend) .setConfiguration(config) .setFileName(fileName) .withFilter(filter) .withIgnoreExceptions(ignoreExceptions) .withImmediateFlush(isImmediateFlush) .withLayout(layout) .withName(name) .setRegionLength(regionLength) .build(); // @formatter:on }
/** * Create a File Appender. * * @param fileName The name and path of the file. * @param append "True" if the file should be appended to, "false" if it * should be overwritten. The default is "true". * @param name The name of the Appender. * @param immediateFlush "true" if the contents should be flushed on every * write, "false" otherwise. The default is "true". * @param bufferSizeStr The buffer size, defaults to {@value RandomAccessFileManager#DEFAULT_BUFFER_SIZE}. * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise * they are propagated to the caller. * @param layout The layout to use to format the event. If no layout is * provided the default PatternLayout will be used. * @param filter The filter, if any, to use. * @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 The FileAppender. * @deprecated Use {@link #newBuilder()}. */ @Deprecated public static <B extends Builder<B>> RandomAccessFileAppender createAppender( final String fileName, final String append, final String name, final String immediateFlush, final String bufferSizeStr, final String ignore, final Layout<? extends Serializable> layout, final Filter filter, final String advertise, final String advertiseURI, final Configuration configuration) { final boolean isAppend = Booleans.parseBoolean(append, true); final boolean isFlush = Booleans.parseBoolean(immediateFlush, true); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final boolean isAdvertise = Boolean.parseBoolean(advertise); final int bufferSize = Integers.parseInt(bufferSizeStr, RandomAccessFileManager.DEFAULT_BUFFER_SIZE); return RandomAccessFileAppender.<B>newBuilder() .setAdvertise(isAdvertise) .setAdvertiseURI(advertiseURI) .setAppend(isAppend) .withBufferSize(bufferSize) .setConfiguration(configuration) .setFileName(fileName) .withFilter(filter) .withIgnoreExceptions(ignoreExceptions) .withImmediateFlush(isFlush) .withLayout(layout) .withName(name) .build(); }
/** * Create a File Appender. * @param fileName The name and path of the file. * @param append "True" if the file should be appended to, "false" if it should be overwritten. * The default is "true". * @param locking "True" if the file should be locked. The default is "false". * @param name The name of the Appender. * @param immediateFlush "true" if the contents should be flushed on every write, "false" otherwise. The default * is "true". * @param ignoreExceptions If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise * they are propagated to the caller. * @param bufferedIo "true" if I/O should be buffered, "false" otherwise. The default is "true". * @param bufferSizeStr buffer size for buffered IO (default is 8192). * @param layout The layout to use to format the event. If no layout is provided the default PatternLayout * will be used. * @param filter The filter, if any, to use. * @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 The FileAppender. * @deprecated Use {@link #newBuilder()} */ @Deprecated public static <B extends Builder<B>> FileAppender createAppender( // @formatter:off final String fileName, final String append, final String locking, final String name, final String immediateFlush, final String ignoreExceptions, final String bufferedIo, final String bufferSizeStr, final Layout<? extends Serializable> layout, final Filter filter, final String advertise, final String advertiseUri, final Configuration config) { return FileAppender.<B>newBuilder() .withAdvertise(Boolean.parseBoolean(advertise)) .withAdvertiseUri(advertiseUri) .withAppend(Booleans.parseBoolean(append, true)) .withBufferedIo(Booleans.parseBoolean(bufferedIo, true)) .withBufferSize(Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE)) .setConfiguration(config) .withFileName(fileName) .withFilter(filter) .withIgnoreExceptions(Booleans.parseBoolean(ignoreExceptions, true)) .withImmediateFlush(Booleans.parseBoolean(immediateFlush, true)) .withLayout(layout) .withLocking(Boolean.parseBoolean(locking)) .withName(name) .build(); // @formatter:on }
/** * Create new Corp2WorldAppender instance * @param name appender name * @param ignore if ignore exceptions * @param bufferSizeStr internal buffer size as number of log events * @param apiToken Corp2World API access token * @param apiKey Corp2World API access key * @param layout layout * @param filter filter * @return appender instance */ @PluginFactory public static Corp2WorldAppender createAppender( @PluginAttribute("name") final String name, @PluginAttribute("ignoreExceptions") final String ignore, @PluginAttribute("bufferSize") final String bufferSizeStr, @PluginAttribute("apiToken") final String apiToken, @PluginAttribute("apiKey") final String apiKey, @PluginAttribute("topicPattern") final String topicPattern, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter ) { /* * Appender name must be specified */ if (name == null) { LOGGER.error("No name provided for FileAppender"); return null; } /* * Internal queue size , use default if not specified */ final int queueSize = Integers.parseInt(bufferSizeStr, DEFAULT_QUEUE_SIZE); /* * If ignore exceptions */ final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); /* * Create default layout if not specified */ if (layout == null) { layout = PatternLayout.createDefaultLayout(); } /* * Create instance and set the properties */ Corp2WorldAppender appender = new Corp2WorldAppender(name, filter, layout, ignoreExceptions); appender.setQueueSize(queueSize); appender.setApiToken(apiToken); appender.setApiKey(apiKey); if(topicPattern != null && !"".equals(topicPattern) ) appender.setTopicLayout( PatternLayout.newBuilder().withPattern(topicPattern).withAlwaysWriteExceptions(false).build() ); return appender; }
/** * 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(); }
@Test public void testMemMapLocation() throws Exception { final File f = new File(LOGFILE); if (f.exists()) { assertTrue("deleted ok", f.delete()); } assertTrue(!f.exists()); final int expectedFileLength = Integers.ceilingNextPowerOfTwo(32000); assertEquals(32768, expectedFileLength); final Logger log = LogManager.getLogger(); try { log.warn("Test log1"); assertTrue(f.exists()); assertEquals("initial length", expectedFileLength, f.length()); log.warn("Test log2"); assertEquals("not grown", expectedFileLength, f.length()); } finally { CoreLoggerContexts.stopLoggerContext(false); } final int LINESEP = System.lineSeparator().length(); assertEquals("Shrunk to actual used size", 474 + 2 * LINESEP, f.length()); String line1, line2, line3; try (final BufferedReader reader = new BufferedReader(new FileReader(LOGFILE))) { line1 = reader.readLine(); line2 = reader.readLine(); line3 = reader.readLine(); } assertNotNull(line1); assertThat(line1, containsString("Test log1")); final String location1 = "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderLocationTest.testMemMapLocation(MemoryMappedFileAppenderLocationTest.java:65)"; assertThat(line1, containsString(location1)); assertNotNull(line2); assertThat(line2, containsString("Test log2")); final String location2 = "org.apache.logging.log4j.core.appender.MemoryMappedFileAppenderLocationTest.testMemMapLocation(MemoryMappedFileAppenderLocationTest.java:69)"; assertThat(line2, containsString(location2)); assertNull("only two lines were logged", line3); }