Skip to content

Commit

Permalink
Ensure the streams are closed to clean up the FileOutputStream resour…
Browse files Browse the repository at this point in the history
…ces.
  • Loading branch information
jamezp committed Mar 19, 2015
1 parent 38cb657 commit 5802ef8
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/main/java/org/apache/log4j/ConsoleAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ public class ConsoleAppender extends WriterAppender {
public static final String SYSTEM_OUT = "System.out";
public static final String SYSTEM_ERR = "System.err";

private static final PrintStream out =
new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out), 128), true);
private static final PrintStream err =
new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.err), 128), true);

protected String target = SYSTEM_OUT;

/**
Expand Down Expand Up @@ -121,6 +116,10 @@ String getTarget() {
/**
* Sets whether the appender honors reassignments of System.out
* or System.err made after configuration.
* <p>
* <strong>Note:</strong> The follow value is not used and streams will always be closed and recreated if necessary.
* </p>
*
* @param newValue if true, appender will use value of System.out or
* System.err in force at the time when logging events are appended.
* @since 1.2.13
Expand All @@ -132,6 +131,10 @@ public final void setFollow(final boolean newValue) {
/**
* Gets whether the appender honors reassignments of System.out
* or System.err made after configuration.
* <p>
* <strong>Note:</strong> The follow value is not used and streams will always be closed and recreated if necessary.
* </p>
*
* @return true if appender will use value of System.out or
* System.err in force at the time when logging events are appended.
* @since 1.2.13
Expand All @@ -149,19 +152,11 @@ void targetWarn(String val) {
* Prepares the appender for use.
*/
public void activateOptions() {
if (follow) {
if (target.equals(SYSTEM_ERR)) {
setWriter(createWriter(new SystemErrStream()));
} else {
setWriter(createWriter(new SystemOutStream()));
}
} else {
if (target.equals(SYSTEM_ERR)) {
setWriter(createWriter(err));
} else {
setWriter(createWriter(out));
}
}
if (target.equals(SYSTEM_ERR)) {
setWriter(createWriter(new SystemErrStream()));
} else {
setWriter(createWriter(new SystemOutStream()));
}

super.activateOptions();
}
Expand All @@ -172,22 +167,27 @@ public void activateOptions() {
protected
final
void closeWriter() {
if (follow) {
super.closeWriter();
}
super.closeWriter();
}

private static PrintStream createPrintStream(final FileDescriptor fd) {
return new PrintStream(new BufferedOutputStream(new FileOutputStream(fd), 128));
}


/**
* An implementation of OutputStream that redirects to the
* current System.err.
*
*/
private static class SystemErrStream extends OutputStream {
private final PrintStream err;
public SystemErrStream() {
err = createPrintStream(FileDescriptor.err);
}

public void close() {
err.close();
}

public void flush() {
Expand All @@ -214,10 +214,13 @@ public void write(final int b) throws IOException {
*
*/
private static class SystemOutStream extends OutputStream {
private final PrintStream out;
public SystemOutStream() {
out = createPrintStream(FileDescriptor.out);
}

public void close() {
out.close();
}

public void flush() {
Expand Down

0 comments on commit 5802ef8

Please sign in to comment.