
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "Logback: the generic, reliable, fast and flexible logging framework.". The branch, encoder has been updated via 6a3e7041abd4d363afa4b8dd53a132f1f624a65c (commit) from d2e55bbf1ee4c99949cc82117a3481b8d13dcee7 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- http://git.qos.ch/gitweb/?p=logback.git;a=commit;h=6a3e7041abd4d363afa4b8dd5... http://github.com/ceki/logback/commit/6a3e7041abd4d363afa4b8dd53a132f1f624a6... commit 6a3e7041abd4d363afa4b8dd53a132f1f624a65c Author: Ceki Gulcu <ceki@qos.ch> Date: Fri Feb 19 14:12:08 2010 +0100 - The Encoder interface is handed an ObjectStream when the init(ObjectStream) method is called. The Encoder needs to reference the OS passed to it in future operations. The enclosing Appender is responsible of signaling the Encoder when the current OS is closed (by invoking encopder.close()) and when a new one is opened (by invoking encoder.inig(os)); - All tests pass. diff --git a/logback-core/src/main/java/ch/qos/logback/core/Encoder.java b/logback-core/src/main/java/ch/qos/logback/core/Encoder.java index 28cb9fc..1734e4a 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/Encoder.java +++ b/logback-core/src/main/java/ch/qos/logback/core/Encoder.java @@ -21,9 +21,8 @@ import ch.qos.logback.core.spi.LifeCycle; public interface Encoder<E> extends ContextAware, LifeCycle { - void doEncode(E event, OutputStream os) throws IOException; - + void doEncode(E event) throws IOException; void init(OutputStream os) throws IOException; - void close(OutputStream os) throws IOException; + void close() throws IOException; } diff --git a/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java b/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java index 63d4867..9dd0eaf 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java +++ b/logback-core/src/main/java/ch/qos/logback/core/WriterAppender.java @@ -203,7 +203,7 @@ public class WriterAppender<E> extends UnsynchronizedAppenderBase<E> { void encoderClose() { if (encoder != null && this.outputStream != null) { try { - encoder.close(outputStream); + encoder.close(); } catch (IOException ioe) { this.started = false; addStatus(new ErrorStatus("Failed to write footer for appender named [" @@ -232,16 +232,12 @@ public class WriterAppender<E> extends UnsynchronizedAppenderBase<E> { return; } - try { - encoder.init(outputStream); - } catch (IOException e) { - addError("Failied to initialize encoder", e); - } + encoderInit(); } } protected void writeOut(E event) throws IOException { - this.encoder.doEncode(event, outputStream); + this.encoder.doEncode(event); } /** diff --git a/logback-core/src/test/java/ch/qos/logback/core/encoder/EchoEncoder.java b/logback-core/src/main/java/ch/qos/logback/core/encoder/EchoEncoder.java similarity index 79% rename from logback-core/src/test/java/ch/qos/logback/core/encoder/EchoEncoder.java rename to logback-core/src/main/java/ch/qos/logback/core/encoder/EchoEncoder.java index 5cc19f5..e239629 100644 --- a/logback-core/src/test/java/ch/qos/logback/core/encoder/EchoEncoder.java +++ b/logback-core/src/main/java/ch/qos/logback/core/encoder/EchoEncoder.java @@ -26,22 +26,23 @@ public class EchoEncoder<E> extends EncoderBase<E> { public EchoEncoder() { } - public void doEncode(E event, OutputStream os) throws IOException { + public void doEncode(E event) throws IOException { String val = event + CoreConstants.LINE_SEPARATOR; - os.write(val.getBytes()); + outputStream.write(val.getBytes()); } - public void close(OutputStream os) throws IOException { + public void close() throws IOException { if (fileFooter == null) { return; } - os.write(fileFooter.getBytes()); + outputStream.write(fileFooter.getBytes()); } public void init(OutputStream os) throws IOException { + super.init(os); if (fileHeader == null) { return; } - os.write(fileHeader.getBytes()); + outputStream.write(fileHeader.getBytes()); } } diff --git a/logback-core/src/main/java/ch/qos/logback/core/encoder/EncoderBase.java b/logback-core/src/main/java/ch/qos/logback/core/encoder/EncoderBase.java index 6d53222..ac5febf 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/encoder/EncoderBase.java +++ b/logback-core/src/main/java/ch/qos/logback/core/encoder/EncoderBase.java @@ -1,13 +1,21 @@ package ch.qos.logback.core.encoder; +import java.io.IOException; +import java.io.OutputStream; + import ch.qos.logback.core.Encoder; import ch.qos.logback.core.spi.ContextAwareBase; abstract public class EncoderBase<E> extends ContextAwareBase implements Encoder<E> { protected boolean started; - + protected OutputStream outputStream; + + public void init(OutputStream os) throws IOException { + this.outputStream = os; + } + public boolean isStarted() { return started; } diff --git a/logback-core/src/main/java/ch/qos/logback/core/html/LayoutWrappingEncoder.java b/logback-core/src/main/java/ch/qos/logback/core/html/LayoutWrappingEncoder.java index 11d938b..cbbc966 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/html/LayoutWrappingEncoder.java +++ b/logback-core/src/main/java/ch/qos/logback/core/html/LayoutWrappingEncoder.java @@ -4,14 +4,11 @@ import java.io.IOException; import java.io.OutputStream; import ch.qos.logback.core.CoreConstants; -import ch.qos.logback.core.Encoder; import ch.qos.logback.core.Layout; -import ch.qos.logback.core.spi.ContextAwareBase; +import ch.qos.logback.core.encoder.EncoderBase; -public class LayoutWrappingEncoder<E> extends ContextAwareBase implements - Encoder<E> { +public class LayoutWrappingEncoder<E> extends EncoderBase<E> { - boolean started; protected Layout<E> layout; public Layout<E> getLayout() { @@ -24,11 +21,12 @@ public class LayoutWrappingEncoder<E> extends ContextAwareBase implements public void init(OutputStream os) throws IOException { - writeHeader(os); + super.init(os); + writeHeader(); } - public void close(OutputStream os) throws IOException { - writeFooter(os); + public void close() throws IOException { + writeFooter(); } private void appendIfNotNull(StringBuilder sb, String s) { @@ -37,8 +35,8 @@ public class LayoutWrappingEncoder<E> extends ContextAwareBase implements } } - void writeHeader(OutputStream os) throws IOException { - if (layout != null && (os != null)) { + void writeHeader() throws IOException { + if (layout != null && (outputStream != null)) { StringBuilder sb = new StringBuilder(); appendIfNotNull(sb, layout.getFileHeader()); appendIfNotNull(sb, layout.getPresentationHeader()); @@ -47,29 +45,29 @@ public class LayoutWrappingEncoder<E> extends ContextAwareBase implements // If at least one of file header or presentation header were not // null, then append a line separator. // This should be useful in most cases and should not hurt. - os.write(sb.toString().getBytes()); - os.flush(); + outputStream.write(sb.toString().getBytes()); + outputStream.flush(); } } } - void writeFooter(OutputStream os) throws IOException { - if (layout != null && os != null) { + void writeFooter() throws IOException { + if (layout != null && outputStream != null) { StringBuilder sb = new StringBuilder(); appendIfNotNull(sb, layout.getPresentationFooter()); appendIfNotNull(sb, layout.getFileFooter()); if (sb.length() > 0) { - os.write(sb.toString().getBytes()); - os.flush(); + outputStream.write(sb.toString().getBytes()); + outputStream.flush(); } } } - public void doEncode(E event, OutputStream os) throws IOException { + public void doEncode(E event) throws IOException { String txt = layout.doLayout(event); - os.write(txt.getBytes()); - os.flush(); + outputStream.write(txt.getBytes()); + outputStream.flush(); } public boolean isStarted() { diff --git a/logback-core/src/test/java/ch/qos/logback/core/appender/DummyAppenderTest.java b/logback-core/src/test/java/ch/qos/logback/core/appender/DummyAppenderTest.java index 44f2f51..6443b0a 100644 --- a/logback-core/src/test/java/ch/qos/logback/core/appender/DummyAppenderTest.java +++ b/logback-core/src/test/java/ch/qos/logback/core/appender/DummyAppenderTest.java @@ -16,10 +16,12 @@ package ch.qos.logback.core.appender; import static org.junit.Assert.assertEquals; import java.io.ByteArrayOutputStream; +import java.io.IOException; import org.junit.Test; import ch.qos.logback.core.Appender; +import ch.qos.logback.core.Encoder; import ch.qos.logback.core.encoder.DummyEncoder; import ch.qos.logback.core.layout.DummyLayout; @@ -40,8 +42,10 @@ public class DummyAppenderTest extends AbstractAppenderTest<Object> { } @Test - public void testBasic() { - da.setEncoder(new DummyEncoder<Object>()); + public void testBasic() throws IOException { + Encoder<Object> encoder = new DummyEncoder<Object>(); + encoder.init(baos); + da.setEncoder(encoder); da.start(); da.doAppend(new Object()); assertEquals(DummyLayout.DUMMY, baos.toString()); diff --git a/logback-core/src/test/java/ch/qos/logback/core/encoder/DummyEncoder.java b/logback-core/src/test/java/ch/qos/logback/core/encoder/DummyEncoder.java index 998ed52..f214557 100644 --- a/logback-core/src/test/java/ch/qos/logback/core/encoder/DummyEncoder.java +++ b/logback-core/src/test/java/ch/qos/logback/core/encoder/DummyEncoder.java @@ -41,8 +41,8 @@ public class DummyEncoder<E> extends EncoderBase<E> { this.val = val; } - public void doEncode(E event, OutputStream os) throws IOException { - writeOut(os, val); + public void doEncode(E event) throws IOException { + writeOut(val); } private void appendIfNotNull(StringBuilder sb, String s) { @@ -51,15 +51,15 @@ public class DummyEncoder<E> extends EncoderBase<E> { } } - void writeOut(OutputStream os, String s) throws IOException { + void writeOut(String s) throws IOException { if (encodingName == null) { - os.write(s.getBytes()); + outputStream.write(s.getBytes()); } else { - os.write(s.getBytes(encodingName)); + outputStream.write(s.getBytes(encodingName)); } } - void writeHeader(OutputStream os) throws IOException { + void writeHeader() throws IOException { StringBuilder sb = new StringBuilder(); appendIfNotNull(sb, fileHeader); if (sb.length() > 0) { @@ -67,22 +67,23 @@ public class DummyEncoder<E> extends EncoderBase<E> { // If at least one of file header or presentation header were not // null, then append a line separator. // This should be useful in most cases and should not hurt. - writeOut(os, sb.toString()); + writeOut(sb.toString()); } } public void init(OutputStream os) throws IOException { - writeHeader(os); + super.init(os); + writeHeader(); } - public void close(OutputStream os) throws IOException { + public void close() throws IOException { if (fileFooter == null) { return; } if (encodingName == null) { - os.write(fileFooter.getBytes()); + outputStream.write(fileFooter.getBytes()); } else { - os.write(fileFooter.getBytes(encodingName)); + outputStream.write(fileFooter.getBytes(encodingName)); } } diff --git a/logback-core/src/test/java/ch/qos/logback/core/encoder/NopEncoder.java b/logback-core/src/test/java/ch/qos/logback/core/encoder/NopEncoder.java index 62afe4f..e3c4d4d 100644 --- a/logback-core/src/test/java/ch/qos/logback/core/encoder/NopEncoder.java +++ b/logback-core/src/test/java/ch/qos/logback/core/encoder/NopEncoder.java @@ -18,10 +18,10 @@ import java.io.OutputStream; public class NopEncoder<E> extends EncoderBase<E> { - public void close(OutputStream os) throws IOException { + public void close() throws IOException { } - public void doEncode(E event, OutputStream os) throws IOException { + public void doEncode(E event) throws IOException { } public void init(OutputStream os) throws IOException { diff --git a/logback-examples/src/main/java/chapter11/TrivialLogbackAppender.java b/logback-examples/src/main/java/chapter11/TrivialLogbackAppender.java index e2b3d74..bbcad79 100644 --- a/logback-examples/src/main/java/chapter11/TrivialLogbackAppender.java +++ b/logback-examples/src/main/java/chapter11/TrivialLogbackAppender.java @@ -37,6 +37,10 @@ public class TrivialLogbackAppender extends AppenderBase<ILoggingEvent> { addError("No encoder set for the appender named [" + name + "]."); return; } + try { + encoder.init(System.out); + } catch (IOException e) { + } super.start(); } @@ -45,7 +49,7 @@ public class TrivialLogbackAppender extends AppenderBase<ILoggingEvent> { // note that AppenderBase.doAppend will invoke this method only if // this appender was successfully started. try { - this.encoder.doEncode(loggingevent, System.out); + this.encoder.doEncode(loggingevent); } catch (IOException e) { // we can't do much with the exception except halting super.stop(); diff --git a/logback-examples/src/main/java/chapter4/CountingConsoleAppender.java b/logback-examples/src/main/java/chapter4/CountingConsoleAppender.java index b4f0bec..5e18f14 100644 --- a/logback-examples/src/main/java/chapter4/CountingConsoleAppender.java +++ b/logback-examples/src/main/java/chapter4/CountingConsoleAppender.java @@ -45,6 +45,10 @@ public class CountingConsoleAppender extends AppenderBase<ILoggingEvent> { return; } + try { + encoder.init(System.out); + } catch (IOException e) { + } super.start(); } @@ -54,7 +58,7 @@ public class CountingConsoleAppender extends AppenderBase<ILoggingEvent> { } // output the events as formatted by our layout try { - this.encoder.doEncode(event, System.out); + this.encoder.doEncode(event); } catch (IOException e) { } ----------------------------------------------------------------------- Summary of changes: .../src/main/java/ch/qos/logback/core/Encoder.java | 5 +-- .../java/ch/qos/logback/core/WriterAppender.java | 10 ++---- .../ch/qos/logback/core/encoder/EchoEncoder.java | 11 +++--- .../ch/qos/logback/core/encoder/EncoderBase.java | 10 +++++- .../logback/core/html/LayoutWrappingEncoder.java | 36 +++++++++---------- .../logback/core/appender/DummyAppenderTest.java | 8 +++- .../ch/qos/logback/core/encoder/DummyEncoder.java | 23 ++++++------ .../ch/qos/logback/core/encoder/NopEncoder.java | 4 +- .../java/chapter11/TrivialLogbackAppender.java | 6 +++- .../java/chapter4/CountingConsoleAppender.java | 6 +++- 10 files changed, 67 insertions(+), 52 deletions(-) rename logback-core/src/{test => main}/java/ch/qos/logback/core/encoder/EchoEncoder.java (79%) hooks/post-receive -- Logback: the generic, reliable, fast and flexible logging framework.