ASF Bugzilla – Attachment 29137 Details for
Bug 53620
[juli] delay opening a file until something gets logged
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
patch
0001-[juli]-delay-opening-a-file-until-something-gets-logged.patch (text/plain), 9.67 KB, created by
Karol Bucek
on 2012-07-30 08:55:29 UTC
(
hide
)
Description:
patch
Filename:
MIME Type:
Creator:
Karol Bucek
Created:
2012-07-30 08:55:29 UTC
Size:
9.67 KB
patch
obsolete
>From 8ba8f6d1379b688488718516858025887129ae87 Mon Sep 17 00:00:00 2001 >From: kares <self@kares.org> >Date: Mon, 30 Jul 2012 10:38:29 +0200 >Subject: [PATCH] [juli] delay opening a file until something gets logged this > 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(-) > >diff --git a/java/org/apache/juli/AsyncFileHandler.java b/java/org/apache/juli/AsyncFileHandler.java >index c58050e..0c29f42 100644 >--- a/java/org/apache/juli/AsyncFileHandler.java >+++ b/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 >diff --git a/java/org/apache/juli/FileHandler.java b/java/org/apache/juli/FileHandler.java >index 60742d5..5ea3167 100644 >--- a/java/org/apache/juli/FileHandler.java >+++ b/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 <code>LogManager</code> 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 <code>date</code>. > */ >- 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; >+ } > > } >-- >1.7.5.4 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 53620
: 29137 |
36265