@@ -, +, @@
helps a lot when the FileHandler is programatically setup +
add a constructor where all 'configurable' properties of
the handler can be provided programatically - since
FileHandler.close() is public API so should be open()
---
java/org/apache/juli/AsyncFileHandler.java | 2 +-
java/org/apache/juli/FileHandler.java | 129 ++++++++++++++--------------
2 files changed, 65 insertions(+), 66 deletions(-)
--- a/java/org/apache/juli/AsyncFileHandler.java
+++ a/java/org/apache/juli/AsyncFileHandler.java
@@ -80,7 +80,7 @@ public class AsyncFileHandler extends FileHandler {
}
@Override
- protected void open() {
+ public void open() {
if(!closed) return;
closed = false;
// TODO Auto-generated method stub
--- a/java/org/apache/juli/FileHandler.java
+++ a/java/org/apache/juli/FileHandler.java
@@ -96,61 +96,60 @@ public class FileHandler
this.prefix = prefix;
this.suffix = suffix;
configure();
- openWriter();
}
-
-
- // ----------------------------------------------------- Instance Variables
+ public FileHandler(String directory, String prefix, String suffix, boolean rotatable, int bufferSize) {
+ this.directory = directory;
+ this.prefix = prefix;
+ this.suffix = suffix;
+ this.rotatable = rotatable;
+ this.bufferSize = bufferSize;
+ configure();
+ }
- /**
- * The as-of date for the currently open log file, or a zero-length
- * string if there is no open log file.
- */
- private volatile String date = "";
+ // ----------------------------------------------------- Instance Variables
/**
* The directory in which log files are created.
*/
- private String directory = null;
-
+ private String directory;
/**
* The prefix that is added to log file filenames.
*/
- private String prefix = null;
-
+ private String prefix;
/**
* The suffix that is added to log file filenames.
*/
- private String suffix = null;
-
+ private String suffix;
/**
* Determines whether the logfile is rotatable
*/
- private boolean rotatable = true;
+ private Boolean rotatable;
+ /**
+ * Log buffer size.
+ */
+ private Integer bufferSize;
/**
+ * The as-of date for the currently open log file, or null if there is no
+ * open log file.
+ */
+ private volatile String date = null;
+
+ /**
* The PrintWriter to which we are currently logging, if any.
*/
private volatile PrintWriter writer = null;
-
/**
* Lock used to control access to the writer.
*/
- protected ReadWriteLock writerLock = new ReentrantReadWriteLock();
-
-
- /**
- * Log buffer size.
- */
- private int bufferSize = -1;
-
+ protected final ReadWriteLock writerLock = new ReentrantReadWriteLock();
// --------------------------------------------------------- Public Methods
@@ -167,22 +166,22 @@ public class FileHandler
return;
}
- // Construct the timestamp we will use, if requested
- Timestamp ts = new Timestamp(System.currentTimeMillis());
- String tsString = ts.toString().substring(0, 19);
- String tsDate = tsString.substring(0, 10);
-
+ final String tsDate = rotatable ?
+ new Timestamp(System.currentTimeMillis()).toString().substring(0, 10) : "";
+
try {
writerLock.readLock().lock();
// If the date has changed, switch log files
- if (rotatable && !date.equals(tsDate)) {
+ // Construct the timestamp we will use, if requested
+ // (... if not rotatable this will happen only once)
+ if ( !tsDate.equals(date) ) {
try {
// Update to writeLock before we switch
writerLock.readLock().unlock();
writerLock.writeLock().lock();
-
+
// Make sure another thread hasn't already done this
- if (!date.equals(tsDate)) {
+ if ( !tsDate.equals(date) ) {
closeWriter();
date = tsDate;
openWriter();
@@ -206,7 +205,7 @@ public class FileHandler
try {
if (writer!=null) {
writer.write(result);
- if (bufferSize < 0) {
+ if (bufferSize == null || bufferSize < 0) {
writer.flush();
}
} else {
@@ -243,7 +242,7 @@ public class FileHandler
writer.flush();
writer.close();
writer = null;
- date = "";
+ date = null;
} catch (Exception e) {
reportError(null, e, ErrorManager.CLOSE_FAILURE);
} finally {
@@ -257,7 +256,7 @@ public class FileHandler
*/
@Override
public void flush() {
-
+
writerLock.readLock().lock();
try {
if (writer == null)
@@ -271,33 +270,32 @@ public class FileHandler
}
-
/**
* Configure from LogManager
properties.
*/
private void configure() {
-
+
Timestamp ts = new Timestamp(System.currentTimeMillis());
- String tsString = ts.toString().substring(0, 19);
- date = tsString.substring(0, 10);
+ date = ts.toString().substring(0, 10);
String className = this.getClass().getName(); //allow classes to override
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// Retrieve configuration of logging file name
- rotatable = Boolean.parseBoolean(getProperty(className + ".rotatable", "true"));
+ if (rotatable == null)
+ rotatable = Boolean.parseBoolean(getProperty(className + ".rotatable", "true"));
if (directory == null)
directory = getProperty(className + ".directory", "logs");
if (prefix == null)
prefix = getProperty(className + ".prefix", "juli.");
if (suffix == null)
suffix = getProperty(className + ".suffix", ".log");
- String sBufferSize = getProperty(className + ".bufferSize", String.valueOf(bufferSize));
- try {
- bufferSize = Integer.parseInt(sBufferSize);
- } catch (NumberFormatException ignore) {
- //no op
+ if (bufferSize == null) {
+ String sBufferSize = getProperty(className + ".bufferSize", null);
+ try {
+ bufferSize = sBufferSize != null ? Integer.parseInt(sBufferSize) : null;
+ } catch (NumberFormatException ignore) { /* no op */ }
}
// Get encoding for the logging file
String encoding = getProperty(className + ".encoding", null);
@@ -355,36 +353,30 @@ public class FileHandler
/**
* Open the new log file for the date specified by date
.
*/
- protected void open() {
+ public void open() {
openWriter();
}
protected void openWriter() {
-
+
// Create the directory if necessary
- File dir = new File(directory);
- if (!dir.mkdirs() && !dir.isDirectory()) {
- reportError("Unable to create [" + dir + "]", null,
- ErrorManager.OPEN_FAILURE);
- writer = null;
- return;
+ final File dir = new File(directory);
+ if ( !checkDir(dir) ) {
+ writer = null; return;
}
-
+
// Open the current log file
writerLock.writeLock().lock();
+ String logFileName = prefix + (rotatable ? date : "") + suffix;
try {
- File pathname = new File(dir.getAbsoluteFile(), prefix
- + (rotatable ? date : "") + suffix);
- File parent = pathname.getParentFile();
- if (!parent.mkdirs() && !parent.isDirectory()) {
- reportError("Unable to create [" + parent + "]", null,
- ErrorManager.OPEN_FAILURE);
- writer = null;
- return;
+ File logFile = new File(dir.getAbsoluteFile(), logFileName);
+ if ( !checkDir(logFile.getParentFile()) ) {
+ writer = null; return;
}
String encoding = getEncoding();
- FileOutputStream fos = new FileOutputStream(pathname, true);
- OutputStream os = bufferSize>0?new BufferedOutputStream(fos,bufferSize):fos;
+ FileOutputStream fos = new FileOutputStream(logFile, true);
+ OutputStream os = (bufferSize != null && bufferSize > 0) ?
+ new BufferedOutputStream(fos, bufferSize) : fos;
writer = new PrintWriter(
(encoding != null) ? new OutputStreamWriter(os, encoding)
: new OutputStreamWriter(os), false);
@@ -398,5 +390,12 @@ public class FileHandler
}
+ private boolean checkDir(final File dir) {
+ if ( !dir.mkdirs() && !dir.isDirectory() ) {
+ reportError("Unable to create [" + dir + "]", null, ErrorManager.OPEN_FAILURE);
+ return false;
+ }
+ return true;
+ }
}
--