You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2013/09/16 21:32:51 UTC
svn commit: r1523770 - in /logging/log4j/log4j2/trunk:
log4j-core/src/main/java/org/apache/logging/log4j/core/appender/
log4j-core/src/test/java/org/apache/logging/log4j/core/appender/
log4j-core/src/test/java/org/apache/logging/log4j/core/layout/ src/...
Author: ggregory
Date: Mon Sep 16 19:32:50 2013
New Revision: 1523770
URL: http://svn.apache.org/r1523770
Log:
[LOG4J2-401] Configure FileAppender buffer size.
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
logging/log4j/log4j2/trunk/src/changes/changes.xml
logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml
Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java?rev=1523770&r1=1523769&r2=1523770&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileAppender.java Mon Sep 16 19:32:50 2013
@@ -29,6 +29,7 @@ import org.apache.logging.log4j.core.con
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.helpers.Booleans;
+import org.apache.logging.log4j.core.helpers.Integers;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.net.Advertiser;
@@ -38,6 +39,7 @@ import org.apache.logging.log4j.core.net
@Plugin(name = "File", category = "Core", elementType = "appender", printObject = true)
public final class FileAppender extends AbstractOutputStreamAppender {
+ private static final int DEFAULT_BUFFER_SIZE = 8192;
private final String fileName;
private final Advertiser advertiser;
private Object advertisement;
@@ -85,6 +87,7 @@ public final class FileAppender extends
* @param ignore 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.
@@ -102,6 +105,7 @@ public final class FileAppender extends
@PluginAttribute("immediateFlush") final String immediateFlush,
@PluginAttribute("ignoreExceptions") final String ignore,
@PluginAttribute("bufferedIO") final String bufferedIO,
+ @PluginAttribute("bufferSize") final String bufferSizeStr,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginElement("Filters") final Filter filter,
@PluginAttribute("advertise") final String advertise,
@@ -118,6 +122,10 @@ public final class FileAppender extends
}
isBuffered = false;
}
+ final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE);
+ if (!isBuffered && bufferSize > 0) {
+ LOGGER.warn("The bufferSize is set to {} but bufferedIO is not true: {}", bufferSize, bufferedIO);
+ }
final boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
@@ -135,7 +143,7 @@ public final class FileAppender extends
}
final FileManager manager = FileManager.getFileManager(fileName, isAppend, isLocking, isBuffered, advertiseURI,
- layout);
+ layout, bufferSize);
if (manager == null) {
return null;
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java?rev=1523770&r1=1523769&r2=1523770&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java Mon Sep 16 19:32:50 2013
@@ -58,17 +58,18 @@ public class FileManager extends OutputS
* @param bufferedIO true if the contents should be buffered as they are written.
* @param advertiseURI the URI to use when advertising the file
* @param layout The layout
+ * @param bufferSize buffer size for buffered IO
* @return A FileManager for the File.
*/
public static FileManager getFileManager(final String fileName, final boolean append, boolean locking,
- final boolean bufferedIO, final String advertiseURI,
- final Layout<? extends Serializable> layout) {
+ final boolean bufferedIO, final String advertiseURI, final Layout<? extends Serializable> layout,
+ final int bufferSize) {
if (locking && bufferedIO) {
locking = false;
}
- return (FileManager) getManager(fileName, new FactoryData(append, locking, bufferedIO, advertiseURI, layout),
- FACTORY);
+ return (FileManager) getManager(fileName, new FactoryData(append, locking, bufferedIO, bufferSize,
+ advertiseURI, layout), FACTORY);
}
@Override
@@ -142,6 +143,7 @@ public class FileManager extends OutputS
private final boolean append;
private final boolean locking;
private final boolean bufferedIO;
+ private final int bufferSize;
private final String advertiseURI;
private final Layout<? extends Serializable> layout;
@@ -150,13 +152,15 @@ public class FileManager extends OutputS
* @param append Append status.
* @param locking Locking status.
* @param bufferedIO Buffering flag.
+ * @param bufferSize Buffer size.
* @param advertiseURI the URI to use when advertising the file
*/
- public FactoryData(final boolean append, final boolean locking, final boolean bufferedIO,
- final String advertiseURI, final Layout<? extends Serializable> layout) {
+ public FactoryData(final boolean append, final boolean locking, final boolean bufferedIO, final int bufferSize,
+ final String advertiseURI, final Layout<? extends Serializable> layout) {
this.append = append;
this.locking = locking;
this.bufferedIO = bufferedIO;
+ this.bufferSize = bufferSize;
this.advertiseURI = advertiseURI;
this.layout = layout;
}
@@ -185,7 +189,7 @@ public class FileManager extends OutputS
try {
os = new FileOutputStream(name, data.append);
if (data.bufferedIO) {
- os = new BufferedOutputStream(os);
+ os = new BufferedOutputStream(os, data.bufferSize);
}
return new FileManager(name, os, data.append, data.locking, data.advertiseURI, data.layout);
} catch (final FileNotFoundException ex) {
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java?rev=1523770&r1=1523769&r2=1523770&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java Mon Sep 16 19:32:50 2013
@@ -72,6 +72,38 @@ public class FileAppenderTest {
}
@Test
+ public void testSmallestBufferSize() throws Exception {
+ final Layout<String> layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, null,
+ null, null);
+ final String bufferSizeStr = "1";
+ final FileAppender appender = FileAppender.createAppender(FILENAME, "true", "false", "test", "false", "false",
+ "false", bufferSizeStr, layout, null, "false", null, null);
+ appender.start();
+ File file = new File(FILENAME);
+ assertTrue("Appender did not start", appender.isStarted());
+ long curLen = file.length();
+ long prevLen = curLen;
+ assertTrue("File length: " + curLen, curLen == 0);
+ for (int i = 0; i < 100; ++i) {
+ final LogEvent event = new Log4jLogEvent("TestLogger", null, FileAppenderTest.class.getName(), Level.INFO,
+ new SimpleMessage("Test"), null, null, null, this.getClass().getSimpleName(), null,
+ System.currentTimeMillis());
+ try {
+ appender.append(event);
+ curLen = file.length();
+ assertTrue("File length: " + curLen, curLen > prevLen);
+ Thread.sleep(25); // Give up control long enough for another thread/process to occasionally do
+ // something.
+ } catch (final Exception ex) {
+ throw ex;
+ }
+ prevLen = curLen;
+ }
+ appender.stop();
+ assertFalse("Appender did not stop", appender.isStarted());
+ }
+
+ @Test
public void testLockingAppender() throws Exception {
writer(true, 1, "test");
verifyFile(1);
@@ -82,7 +114,7 @@ public class FileAppenderTest {
final ExecutorService pool = Executors.newFixedThreadPool(THREADS);
final int count = 10;
final Runnable runnable = new FileWriterRunnable(false, count);
- for (int i=0; i < THREADS; ++i) {
+ for (int i = 0; i < THREADS; ++i) {
pool.execute(runnable);
}
pool.shutdown();
@@ -90,13 +122,12 @@ public class FileAppenderTest {
verifyFile(THREADS * count);
}
-
@Test
public void testMultipleLockedAppenders() throws Exception {
final ExecutorService pool = Executors.newFixedThreadPool(THREADS);
final int count = 10;
final Runnable runnable = new FileWriterRunnable(true, count);
- for (int i=0; i < THREADS; ++i) {
+ for (int i = 0; i < THREADS; ++i) {
pool.execute(runnable);
}
pool.shutdown();
@@ -104,8 +135,7 @@ public class FileAppenderTest {
verifyFile(THREADS * count);
}
-
- //@Test
+ // @Test
public void testMultipleVMs() throws Exception {
final String classPath = System.getProperty("java.class.path");
@@ -113,16 +143,16 @@ public class FileAppenderTest {
final int processes = 3;
final Process[] process = new Process[processes];
final ProcessBuilder[] builders = new ProcessBuilder[processes];
- for (int index=0; index < processes; ++index) {
- builders[index] = new ProcessBuilder("java","-cp", classPath, ProcessTest.class.getName(),
- "Process " + index, count.toString(), "true");
+ for (int index = 0; index < processes; ++index) {
+ builders[index] = new ProcessBuilder("java", "-cp", classPath, ProcessTest.class.getName(), "Process "
+ + index, count.toString(), "true");
}
- for (int index=0; index < processes; ++index) {
+ for (int index = 0; index < processes; ++index) {
process[index] = builders[index].start();
}
- for (int index=0; index < processes; ++index) {
+ for (int index = 0; index < processes; ++index) {
final Process p = process[index];
- //System.out.println("Process " + index + " exited with " + p.waitFor());
+ // System.out.println("Process " + index + " exited with " + p.waitFor());
final InputStream is = p.getInputStream();
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
@@ -137,18 +167,19 @@ public class FileAppenderTest {
}
private static void writer(final boolean lock, final int count, final String name) throws Exception {
- final Layout<String> layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, null, null, null);
+ final Layout<String> layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, null,
+ null, null);
final FileAppender app = FileAppender.createAppender(FILENAME, "true", Boolean.toString(lock), "test", "false",
- "false", "false", layout, null, "false", null, null);
- final Thread t = Thread.currentThread();
+ "false", "false", null, layout, null, "false", null, null);
app.start();
assertTrue("Appender did not start", app.isStarted());
- for (int i=0; i < count; ++i) {
+ for (int i = 0; i < count; ++i) {
final LogEvent event = new Log4jLogEvent("TestLogger", null, FileAppenderTest.class.getName(), Level.INFO,
- new SimpleMessage("Test"), null, null, null, name, null, System.currentTimeMillis());
+ new SimpleMessage("Test"), null, null, null, name, null, System.currentTimeMillis());
try {
app.append(event);
- t.sleep(25); // Give up control long enough for another thread/process to occasionally do something.
+ Thread.sleep(25); // Give up control long enough for another thread/process to occasionally do
+ // something.
} catch (final Exception ex) {
throw ex;
}
@@ -158,7 +189,7 @@ public class FileAppenderTest {
}
private void verifyFile(final int count) throws Exception {
- //String expected = "[\\w]* \\[\\s*\\] INFO TestLogger - Test$";
+ // String expected = "[\\w]* \\[\\s*\\] INFO TestLogger - Test$";
final String expected = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3} \\[[^\\]]*\\] INFO TestLogger - Test";
final Pattern pattern = Pattern.compile(expected);
final FileInputStream fis = new FileInputStream(FILENAME);
@@ -167,7 +198,7 @@ public class FileAppenderTest {
String str = "";
while (is.ready()) {
str = is.readLine();
- //System.out.println(str);
+ // System.out.println(str);
++counter;
final Matcher matcher = pattern.matcher(str);
assertTrue("Bad data: " + str, matcher.matches());
@@ -178,7 +209,6 @@ public class FileAppenderTest {
}
-
private static void deleteFile() {
final File file = new File(FILENAME);
if (file.exists()) {
@@ -190,10 +220,11 @@ public class FileAppenderTest {
private final boolean lock;
private final int count;
- public FileWriterRunnable(final boolean lock, final int count) {
+ public FileWriterRunnable(final boolean lock, final int count) {
this.lock = lock;
this.count = count;
}
+
@Override
public void run() {
final Thread thread = Thread.currentThread();
@@ -225,17 +256,16 @@ public class FileAppenderTest {
}
final boolean lock = Boolean.parseBoolean(args[2]);
- //System.out.println("Got arguments " + id + ", " + count + ", " + lock);
+ // System.out.println("Got arguments " + id + ", " + count + ", " + lock);
try {
writer(lock, count, id);
- //thread.sleep(50);
+ // thread.sleep(50);
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
-
}
}
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java?rev=1523770&r1=1523769&r2=1523770&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/layout/PatternLayoutTest.java Mon Sep 16 19:32:50 2013
@@ -77,7 +77,7 @@ public class PatternLayoutTest {
final PatternLayout layout = PatternLayout.createLayout(msgPattern, ctx.getConfiguration(), null, null, null);
//FileOutputStream fos = new FileOutputStream(OUTPUT_FILE + "_mdc");
final FileAppender appender = FileAppender.createAppender(OUTPUT_FILE + "_mdc", "false", "false", "File", "false",
- "true", "false", layout, null, "false", null, null);
+ "true", "false", null, layout, null, "false", null, null);
appender.start();
// set appender on root and set level to debug
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1523770&r1=1523769&r2=1523770&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Mon Sep 16 19:32:50 2013
@@ -20,6 +20,11 @@
<title>Changes</title>
</properties>
<body>
+ <release version="2.0RC1" date="2013-MM-DD" description="Bug fixes and enhancements">
+ <action issue="LOG4J2-401" dev="ggregory" type="add">
+ Configure FileAppender buffer size.
+ </action>
+ </release>
<release version="2.0-beta9" date="2013-09-14" description="Bug fixes and enhancements">
<action issue="LOG4J2-317" dev="ggregory" type="update">
Renamed FastFileAppender and FastRollingFileAppender to RandomAccessFileAppender
Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml?rev=1523770&r1=1523769&r2=1523770&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/appenders.xml Mon Sep 16 19:32:50 2013
@@ -687,6 +687,11 @@
significantly improves performance, even if immediateFlush is enabled.</td>
</tr>
<tr>
+ <td>bufferSize</td>
+ <td>int</td>
+ <td>When bufferedIO is true, this is the buffer size, the default is 8192.</td>
+ </tr>
+ <tr>
<td>filter</td>
<td>Filter</td>
<td>A Filter to determine if the event should be handled by this Appender. More than one Filter