You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ca...@apache.org on 2006/01/03 05:13:07 UTC
svn commit: r365534 - in /logging/log4j/trunk/src/java/org/apache/log4j: ./
helpers/ rolling/ spi/
Author: carnold
Date: Mon Jan 2 20:13:03 2006
New Revision: 365534
URL: http://svn.apache.org/viewcvs?rev=365534&view=rev
Log:
Bug 35452: Restore WriterAppender and FileAppender compatibility
Added:
logging/log4j/trunk/src/java/org/apache/log4j/helpers/CountingQuietWriter.java
logging/log4j/trunk/src/java/org/apache/log4j/helpers/QuietWriter.java
logging/log4j/trunk/src/java/org/apache/log4j/helpers/SyslogQuietWriter.java
logging/log4j/trunk/src/java/org/apache/log4j/spi/ErrorCode.java
Modified:
logging/log4j/trunk/src/java/org/apache/log4j/FileAppender.java
logging/log4j/trunk/src/java/org/apache/log4j/WriterAppender.java
logging/log4j/trunk/src/java/org/apache/log4j/helpers/OnlyOnceErrorHandler.java
logging/log4j/trunk/src/java/org/apache/log4j/rolling/RollingFileAppender.java
Modified: logging/log4j/trunk/src/java/org/apache/log4j/FileAppender.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/FileAppender.java?rev=365534&r1=365533&r2=365534&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/FileAppender.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/FileAppender.java Mon Jan 2 20:13:03 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 1999,2005 The Apache Software Foundation.
+ * Copyright 1999,2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
package org.apache.log4j;
import java.io.*;
+import org.apache.log4j.spi.*;
+import org.apache.log4j.helpers.*;
import org.apache.log4j.helpers.OptionConverter;
@@ -144,23 +146,18 @@
@since 0.8.1 */
public void activateOptions() {
- int errors = 0;
if (fileName != null) {
try {
setFile(fileName, fileAppend, bufferedIO, bufferSize);
+ super.activateOptions();
} catch (java.io.IOException e) {
- errors++;
getLogger().error(
"setFile(" + fileName + "," + fileAppend + ") call failed.", e);
}
} else {
- errors++;
getLogger().error("File option not set for appender [{}].", name);
getLogger().warn("Are you using FileAppender instead of ConsoleAppender?");
}
- if(errors == 0) {
- super.activateOptions();
- }
}
/**
@@ -280,11 +277,12 @@
throw ex;
}
}
- this.writer = createWriter(ostream);
+ Writer fw = createWriter(ostream);
if (bufferedIO) {
- this.writer = new BufferedWriter(this.writer, bufferSize);
+ fw = new BufferedWriter(this.qw, bufferSize);
}
+ setQWForFiles(fw);
this.fileAppend = append;
this.bufferedIO = bufferedIO;
@@ -293,5 +291,29 @@
writeHeader();
getLogger().debug("setFile ended");
}
+
+ /**
+ Sets the quiet writer being used.
+
+ This method is overriden by {@link RollingFileAppender}.
+ */
+ protected
+ void setQWForFiles(final Writer writer) {
+ this.qw = createQuietWriter(writer);
+ }
+
+
+ /**
+ Close any previously opened file and call the parent's
+ <code>reset</code>.
+ @deprecated
+ */
+ protected
+ void reset() {
+ closeFile();
+ this.fileName = null;
+ super.reset();
+ }
+
}
Modified: logging/log4j/trunk/src/java/org/apache/log4j/WriterAppender.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/WriterAppender.java?rev=365534&r1=365533&r2=365534&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/WriterAppender.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/WriterAppender.java Mon Jan 2 20:13:03 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 1999,2004 The Apache Software Foundation.
+ * Copyright 1999,2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,12 +17,14 @@
package org.apache.log4j;
import org.apache.log4j.spi.LoggingEvent;
+import org.apache.log4j.spi.ErrorHandler;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
-
+import org.apache.log4j.helpers.QuietWriter;
+import org.apache.log4j.helpers.LogLog;
// Contibutors: Jens Uwe Pipka <je...@gmx.de>
// Ben Sandee
@@ -59,9 +61,9 @@
protected String encoding;
/**
- * This is the {@link Writer Writer} where we will write to.
+ * This is the {@link QuietWriter quietWriter} where we will write to.
*/
- protected Writer writer;
+ protected QuietWriter qw;
/**
* The default constructor does nothing.
@@ -135,7 +137,7 @@
errors++;
}
- if(this.writer == null) {
+ if(this.qw == null) {
getLogger().error("No writer set for the appender named [{}].", name);
errors++;
}
@@ -187,7 +189,7 @@
return false;
}
- if (this.writer == null) {
+ if (this.qw == null) {
getNonFloodingLogger().error(
"No output stream or file set for the appender named [{}].", name);
@@ -218,12 +220,12 @@
* Close the underlying {@link java.io.Writer}.
* */
protected void closeWriter() {
- if (this.writer != null) {
+ if (this.qw != null) {
try {
// before closing we have to output out layout's footer
writeFooter();
- this.writer.close();
- this.writer = null;
+ this.qw.close();
+ this.qw = null;
} catch (IOException e) {
getLogger().error("Could not close writer for WriterAppener named "+name, e);
}
@@ -266,6 +268,64 @@
encoding = value;
}
+ /**
+ * Set the {@link org.apache.log4j.spi.ErrorHandler} for this WriterAppender and also the
+ * underlying {@link QuietWriter} if any.
+ * @deprecated
+ */
+ public synchronized void setErrorHandler(org.apache.log4j.spi.ErrorHandler eh) {
+ if(eh == null) {
+ getLogger().warn("You have tried to set a null error-handler.");
+ } else {
+ this.errorHandler = eh;
+ if(this.qw != null) {
+ this.qw.setErrorHandler(eh);
+ }
+ }
+ }
+
+ private static final class DefaultErrorHandler implements ErrorHandler {
+ private final WriterAppender appender;
+ public DefaultErrorHandler(final WriterAppender appender) {
+ this.appender = appender;
+ }
+ /**@since 1.2*/
+ public void setLogger(Logger logger) {
+
+ }
+ public void error(String message, Exception ioe, int errorCode) {
+ appender.active = false;
+ appender.getNonFloodingLogger().error("IO failure for appender named "+ appender.getName(), ioe);
+ }
+ public void error(String message) {
+
+ }
+ /**@since 1.2*/
+ public void error(String message, Exception e, int errorCode, LoggingEvent event) {
+
+ }
+ /**@since 1.2*/
+ public void setAppender(Appender appender) {
+
+ }
+ /**@since 1.2*/
+ public void setBackupAppender(Appender appender) {
+
+ }
+
+ public void activateOptions() {
+ }
+
+ }
+
+ protected QuietWriter createQuietWriter(final Writer writer) {
+ ErrorHandler handler = errorHandler;
+ if (handler == null) {
+ handler = new DefaultErrorHandler(this);
+ }
+ return new QuietWriter(writer, handler);
+ }
+
/**
<p>Sets the Writer where the log output will go. The
specified Writer must be opened by the user and be
@@ -281,9 +341,8 @@
public synchronized void setWriter(Writer writer) {
// close any previously opened writer
closeWriter();
-
- this.writer = writer;
- //this.tp = new TracerPrintWriter(qw);
+
+ this.qw = createQuietWriter(writer);
writeHeader();
}
@@ -299,8 +358,7 @@
return;
}
- try {
- this.writer.write(this.layout.format(event));
+ this.qw.write(this.layout.format(event));
if (layout.ignoresThrowable()) {
String[] s = event.getThrowableStrRep();
@@ -309,19 +367,23 @@
int len = s.length;
for (int i = 0; i < len; i++) {
- this.writer.write(s[i]);
- this.writer.write(Layout.LINE_SEP);
+ this.qw.write(s[i]);
+ this.qw.write(Layout.LINE_SEP);
}
}
}
if (this.immediateFlush) {
- this.writer.flush();
+ this.qw.flush();
}
- } catch(IOException ioe) {
- this.active = false;
- getNonFloodingLogger().error("IO failure for appender named "+name, ioe);
- }
+ }
+
+ /**
+ * Gets whether appender requires a layout.
+ * @return false
+ */
+ public boolean requiresLayout() {
+ return true;
}
/**
@@ -332,8 +394,9 @@
*
* @deprecated Use {@link #closeWriter} method instead.
* */
- protected void xreset() {
+ protected void reset() {
closeWriter();
+ this.qw = null;
}
/**
@@ -344,14 +407,9 @@
if (layout != null) {
String f = layout.getFooter();
- if ((f != null) && (this.writer != null)) {
- try {
- this.writer.write(f);
- this.writer.flush();
- } catch(IOException ioe) {
- active = false;
- getLogger().error("Failed to write footer for Appender named "+name, ioe);
- }
+ if ((f != null) && (this.qw != null)) {
+ this.qw.write(f);
+ this.qw.flush();
}
}
}
@@ -364,23 +422,11 @@
if (layout != null) {
String h = layout.getHeader();
- if ((h != null) && (this.writer != null)) {
- try {
- this.writer.write(h);
- } catch(IOException ioe) {
- this.active = false;
- getLogger().error("Failed to write header for WriterAppender named "+name, ioe);
- }
+ if ((h != null) && (this.qw != null)) {
+ this.qw.write(h);
}
}
}
- /**
- * Gets whether appender requires a layout.
- * @return false
- */
- public boolean requiresLayout() {
- return true;
- }
}
Added: logging/log4j/trunk/src/java/org/apache/log4j/helpers/CountingQuietWriter.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/helpers/CountingQuietWriter.java?rev=365534&view=auto
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/helpers/CountingQuietWriter.java (added)
+++ logging/log4j/trunk/src/java/org/apache/log4j/helpers/CountingQuietWriter.java Mon Jan 2 20:13:03 2006
@@ -0,0 +1,67 @@
+/*
+ * Copyright 1999,2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j.helpers;
+
+import java.io.Writer;
+import java.io.IOException;
+
+import org.apache.log4j.spi.ErrorCode;
+
+/**
+ Counts the number of bytes written.
+
+ @author Heinz Richter, heinz.richter@frogdot.com
+ @since 0.8.1
+ @deprecated
+
+ */
+public class CountingQuietWriter extends QuietWriter {
+
+ protected long count;
+
+ /**
+ * @deprecated
+ * @param writer
+ * @param eh
+ */
+ public
+ CountingQuietWriter(Writer writer, org.apache.log4j.spi.ErrorHandler eh) {
+ super(writer, eh);
+ }
+
+ public
+ void write(String string) {
+ try {
+ out.write(string);
+ count += string.length();
+ }
+ catch(IOException e) {
+ errorHandler.error("Write failure.", e, ErrorCode.WRITE_FAILURE);
+ }
+ }
+
+ public
+ long getCount() {
+ return count;
+ }
+
+ public
+ void setCount(long count) {
+ this.count = count;
+ }
+
+}
Modified: logging/log4j/trunk/src/java/org/apache/log4j/helpers/OnlyOnceErrorHandler.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/helpers/OnlyOnceErrorHandler.java?rev=365534&r1=365533&r2=365534&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/helpers/OnlyOnceErrorHandler.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/helpers/OnlyOnceErrorHandler.java Mon Jan 2 20:13:03 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
-import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.LoggingEvent;
/**
@@ -32,7 +31,7 @@
@since 0.9.0
@deprecated As of 1.3
*/
-public class OnlyOnceErrorHandler implements ErrorHandler {
+public class OnlyOnceErrorHandler implements org.apache.log4j.spi.ErrorHandler {
public void setLogger(Logger logger) {}
public void activateOptions() {}
public void error(String message, Exception e, int errorCode) {}
Added: logging/log4j/trunk/src/java/org/apache/log4j/helpers/QuietWriter.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/helpers/QuietWriter.java?rev=365534&view=auto
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/helpers/QuietWriter.java (added)
+++ logging/log4j/trunk/src/java/org/apache/log4j/helpers/QuietWriter.java Mon Jan 2 20:13:03 2006
@@ -0,0 +1,83 @@
+/*
+ * Copyright 1999-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j.helpers;
+
+import java.io.Writer;
+import java.io.FilterWriter;
+import java.io.IOException;
+import org.apache.log4j.spi.ErrorCode;
+
+
+/**
+ QuietWriter does not throw exceptions when things go
+ wrong. Instead, it delegates error handling to its {@link
+ org.apache.log4j.spi.ErrorHandler}.
+
+ @author Ceki Gülcü
+
+ @since 0.7.3
+ @deprecated
+*/
+public class QuietWriter extends FilterWriter {
+
+ /**
+ * @deprecated
+ */
+ protected org.apache.log4j.spi.ErrorHandler errorHandler;
+
+ /**
+ * @deprecated
+ * @param writer
+ * @param errorHandler
+ */
+ public
+ QuietWriter(Writer writer, org.apache.log4j.spi.ErrorHandler errorHandler) {
+ super(writer);
+ setErrorHandler(errorHandler);
+ }
+
+ public
+ void write(String string) {
+ try {
+ out.write(string);
+ } catch(IOException e) {
+ errorHandler.error("Failed to write ["+string+"].", e,
+ ErrorCode.WRITE_FAILURE);
+ }
+ }
+
+ public
+ void flush() {
+ try {
+ out.flush();
+ } catch(IOException e) {
+ errorHandler.error("Failed to flush writer,", e,
+ ErrorCode.FLUSH_FAILURE);
+ }
+ }
+
+
+ public
+ void setErrorHandler(org.apache.log4j.spi.ErrorHandler eh) {
+ if(eh == null) {
+ // This is a programming error on the part of the enclosing appender.
+ throw new IllegalArgumentException("Attempted to set null ErrorHandler.");
+ } else {
+ this.errorHandler = eh;
+ }
+ }
+}
Added: logging/log4j/trunk/src/java/org/apache/log4j/helpers/SyslogQuietWriter.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/helpers/SyslogQuietWriter.java?rev=365534&view=auto
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/helpers/SyslogQuietWriter.java (added)
+++ logging/log4j/trunk/src/java/org/apache/log4j/helpers/SyslogQuietWriter.java Mon Jan 2 20:13:03 2006
@@ -0,0 +1,61 @@
+/*
+ * Copyright 1999-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j.helpers;
+
+
+
+import java.io.Writer;
+
+/**
+ SyslogQuietWriter extends QuietWriter by prepending the syslog
+ level code before each printed String.
+
+ @since 0.7.3
+ @deprecated
+*/
+public class SyslogQuietWriter extends QuietWriter {
+
+ int syslogFacility;
+ int level;
+
+ /**
+ * @deprecated
+ * @param writer
+ * @param syslogFacility
+ * @param eh
+ */
+ public
+ SyslogQuietWriter(Writer writer, int syslogFacility, org.apache.log4j.spi.ErrorHandler eh) {
+ super(writer, eh);
+ this.syslogFacility = syslogFacility;
+ }
+
+ public
+ void setLevel(int level) {
+ this.level = level;
+ }
+
+ public
+ void setSyslogFacility(int syslogFacility) {
+ this.syslogFacility = syslogFacility;
+ }
+
+ public
+ void write(String string) {
+ super.write("<"+(syslogFacility | level)+">" + string);
+ }
+}
Modified: logging/log4j/trunk/src/java/org/apache/log4j/rolling/RollingFileAppender.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/rolling/RollingFileAppender.java?rev=365534&r1=365533&r2=365534&view=diff
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/rolling/RollingFileAppender.java (original)
+++ logging/log4j/trunk/src/java/org/apache/log4j/rolling/RollingFileAppender.java Mon Jan 2 20:13:03 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 1999,2005 The Apache Software Foundation.
+ * Copyright 1999,2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -266,7 +266,7 @@
rollover.getActiveFileName(), rollover.getAppend()));
closeWriter();
setFile(rollover.getActiveFileName());
- this.writer = newWriter;
+ this.qw = createQuietWriter(newWriter);
boolean success = true;
Added: logging/log4j/trunk/src/java/org/apache/log4j/spi/ErrorCode.java
URL: http://svn.apache.org/viewcvs/logging/log4j/trunk/src/java/org/apache/log4j/spi/ErrorCode.java?rev=365534&view=auto
==============================================================================
--- logging/log4j/trunk/src/java/org/apache/log4j/spi/ErrorCode.java (added)
+++ logging/log4j/trunk/src/java/org/apache/log4j/spi/ErrorCode.java Mon Jan 2 20:13:03 2006
@@ -0,0 +1,35 @@
+/*
+ * Copyright 1999-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.log4j.spi;
+
+
+/**
+ This interface defines commonly encoutered error codes.
+
+ @author Ceki Gülcü
+ @since 0.9.0
+ */
+public interface ErrorCode {
+
+ public final int GENERIC_FAILURE = 0;
+ public final int WRITE_FAILURE = 1;
+ public final int FLUSH_FAILURE = 2;
+ public final int CLOSE_FAILURE = 3;
+ public final int FILE_OPEN_FAILURE = 4;
+ public final int MISSING_LAYOUT = 5;
+ public final int ADDRESS_PARSE_FAILURE = 6;
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org