下面列出了怎么用org.apache.log4j.helpers.CountingQuietWriter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Handles append time behavior for CompositeRollingAppender. This checks
* if a roll over either by date (checked first) or time (checked second)
* is need and then appends to the file last.
*/
protected void subAppend(LoggingEvent event) {
if (rollDate) {
long n = System.currentTimeMillis();
if (n >= nextCheck) {
now.setTime(n);
nextCheck = rc.getNextCheckMillis(now);
rollOverTime();
}
}
if (rollSize) {
if ((fileName != null) && ((CountingQuietWriter) qw).getCount() >= maxFileSize) {
rollOverSize();
}
}
super.subAppend(event);
}
/**
* Implements the usual roll over behaviour.
* <p>
* <code>File</code> is renamed <code>File.yyyyMMddHHmmss</code> and closed. A
* new <code>File</code> is created to receive further log output.
*/
// synchronization not necessary since doAppend is alreasy synched
public void rollOver() {
if (qw != null) {
long size = ((CountingQuietWriter) qw).getCount();
LogLog.debug("rolling over count=" + size);
// if operation fails, do not roll again until
// maxFileSize more bytes are written
nextRollover = size + maxFileSize;
}
this.closeFile(); // keep windows happy.
String newFileName = getLogFileName(fileName);
try {
// This will also close the file. This is OK since multiple
// close operations are safe.
this.setFile(newFileName, false, bufferedIO, bufferSize);
nextRollover = 0;
} catch (IOException e) {
if (e instanceof InterruptedIOException) {
Thread.currentThread().interrupt();
}
LogLog.error("setFile(" + newFileName + ", false) call failed.", e);
}
}
/**
* Implements the usual roll over behaviour.
* <p>
* <code>File</code> is renamed <code>File.yyyyMMddHHmmss</code> and closed. A
* new <code>File</code> is created to receive further log output.
*/
// synchronization not necessary since doAppend is alreasy synched
public void rollOver() {
if (qw != null) {
long size = ((CountingQuietWriter) qw).getCount();
LogLog.debug("rolling over count=" + size);
// if operation fails, do not roll again until
// maxFileSize more bytes are written
nextRollover = size + maxFileSize;
}
this.closeFile(); // keep windows happy.
String newFileName = getLogFileName(fileName);
try {
// This will also close the file. This is OK since multiple
// close operations are safe.
this.setFile(newFileName, false, bufferedIO, bufferSize);
nextRollover = 0;
} catch (IOException e) {
if (e instanceof InterruptedIOException) {
Thread.currentThread().interrupt();
}
LogLog.error("setFile(" + newFileName + ", false) call failed.", e);
}
}
/**
This method differentiates RollingFileAppender from its super
class.
@since 0.9.0
*/
@Override
protected void subAppend(
LoggingEvent event ) {
super.subAppend(event);
if (fileName != null && qw != null) {
long size = ((CountingQuietWriter) qw).getCount();
if (size >= maxFileSize && size >= nextRollover) {
rollOver();
}
}
}
public
synchronized
void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
super.setFile(fileName, append, this.bufferedIO, this.bufferSize);
if(append) {
File f = new File(fileName);
((CountingQuietWriter) qw).setCount(f.length());
}
}
/**
This method differentiates RollingFileAppender from its super
class.
@since 0.9.0
*/
protected
void subAppend(LoggingEvent event) {
super.subAppend(event);
if(fileName != null && qw != null) {
long size = ((CountingQuietWriter) qw).getCount();
if (size >= maxFileSize && size >= nextRollover) {
rollOver();
}
}
}
/**
* Creates and opens the file for logging. If <code>staticLogFileName</code>
* is false then the fully qualified name is determined and used.
*/
public synchronized void setFile(String fileName, boolean append) throws IOException {
if (!staticLogFileName) {
scheduledFilename = fileName = fileName.trim() + sdf.format(now);
if (countDirection > 0) {
scheduledFilename = fileName = fileName + '.' + (++curSizeRollBackups);
}
}
super.setFile(fileName, append);
if(append) {
File f = new File(fileName);
((CountingQuietWriter) qw).setCount(f.length());
}
}
public synchronized void setFile(String fileName, boolean append,
boolean bufferedIO, int bufferSize) throws IOException {
super.setFile(fileName, append, this.bufferedIO, this.bufferSize);
if (append) {
File f = new File(fileName);
((CountingQuietWriter) qw).setCount(f.length());
}
}
/**
* This method differentiates RollingFileAppender from its super class.
*/
protected void subAppend(LoggingEvent event) {
super.subAppend(event);
if (fileName != null && qw != null) {
long size = ((CountingQuietWriter) qw).getCount();
if (size >= maxFileSize && size >= nextRollover) {
rollOver();
}
}
}
public synchronized void setFile(String fileName, boolean append,
boolean bufferedIO, int bufferSize) throws IOException {
super.setFile(fileName, append, this.bufferedIO, this.bufferSize);
if (append) {
File f = new File(fileName);
((CountingQuietWriter) qw).setCount(f.length());
}
}
/**
* This method differentiates RollingFileAppender from its super class.
*/
protected void subAppend(LoggingEvent event) {
super.subAppend(event);
if (fileName != null && qw != null) {
long size = ((CountingQuietWriter) qw).getCount();
if (size >= maxFileSize && size >= nextRollover) {
rollOver();
}
}
}
protected
void setQWForFiles(Writer writer) {
this.qw = new CountingQuietWriter(writer, errorHandler);
}
protected void setQWForFiles(Writer writer) {
qw = new CountingQuietWriter(writer, errorHandler);
}
/**
Implements roll overs base on file size.
<p>If the maximum number of size based backups is reached
(<code>curSizeRollBackups == maxSizeRollBackups</code) then the oldest
file is deleted -- it's index determined by the sign of countDirection.<br>
If <code>countDirection</code> < 0, then files
{<code>File.1</code>, ..., <code>File.curSizeRollBackups -1</code>}
are renamed to {<code>File.2</code>, ...,
<code>File.curSizeRollBackups</code>}. Moreover, <code>File</code> is
renamed <code>File.1</code> and closed.<br>
A new file is created to receive further log output.
<p>If <code>maxSizeRollBackups</code> is equal to zero, then the
<code>File</code> is truncated with no backup files created.
<p>If <code>maxSizeRollBackups</code> < 0, then <code>File</code> is
renamed if needed and no files are deleted.
*/
// synchronization not necessary since doAppend is alreasy synched
protected void rollOverSize() {
File file;
this.closeFile(); // keep windows happy.
LogLog.debug("rolling over count=" + ((CountingQuietWriter) qw).getCount());
LogLog.debug("maxSizeRollBackups = " + maxSizeRollBackups);
LogLog.debug("curSizeRollBackups = " + curSizeRollBackups);
LogLog.debug("countDirection = " + countDirection);
// If maxBackups <= 0, then there is no file renaming to be done.
if (maxSizeRollBackups != 0) {
if (countDirection < 0) {
// Delete the oldest file, to keep Windows happy.
if (curSizeRollBackups == maxSizeRollBackups) {
deleteFile(fileName + '.' + maxSizeRollBackups);
curSizeRollBackups--;
}
// Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
for (int i = curSizeRollBackups; i >= 1; i--) {
rollFile((fileName + "." + i), (fileName + '.' + (i + 1)));
}
curSizeRollBackups++;
// Rename fileName to fileName.1
rollFile(fileName, fileName + ".1");
} //REMOVE This code branching for Alexander Cerna's request
else if (countDirection == 0) {
//rollFile based on date pattern
curSizeRollBackups++;
now.setTime(System.currentTimeMillis());
scheduledFilename = fileName + sdf.format(now);
rollFile(fileName, scheduledFilename);
}
else { //countDirection > 0
if (curSizeRollBackups >= maxSizeRollBackups && maxSizeRollBackups > 0) {
//delete the first and keep counting up.
int oldestFileIndex = curSizeRollBackups - maxSizeRollBackups + 1;
deleteFile(fileName + '.' + oldestFileIndex);
}
if (staticLogFileName) {
curSizeRollBackups++;
rollFile(fileName, fileName + '.' + curSizeRollBackups);
}
}
}
try {
// This will also close the file. This is OK since multiple
// close operations are safe.
this.setFile(baseFileName, false);
}
catch(IOException e) {
LogLog.error("setFile("+fileName+", false) call failed.", e);
}
}
protected void setQWForFiles(Writer writer) {
this.qw = new CountingQuietWriter(writer, errorHandler);
}
protected void setQWForFiles(Writer writer) {
this.qw = new CountingQuietWriter(writer, errorHandler);
}