Add no-arg factory method to IoBuilder.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/a1fb000e
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a1fb000e
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a1fb000e

Branch: refs/heads/master
Commit: a1fb000e82b770023c38795d221408510d715c02
Parents: 0387be8
Author: Matt Sicker <[email protected]>
Authored: Sun Sep 21 12:59:18 2014 -0500
Committer: Matt Sicker <[email protected]>
Committed: Sun Sep 21 12:59:18 2014 -0500

----------------------------------------------------------------------
 .../org/apache/logging/log4j/io/IoBuilder.java  | 31 ++++++++++++++++--
 .../apache/logging/log4j/io/IoBuilderTest.java  | 34 ++++++++++++++++++++
 .../resources/log4j2-streams-calling-info.xml   | 10 ++++--
 3 files changed, 70 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a1fb000e/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/IoBuilder.java
----------------------------------------------------------------------
diff --git 
a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/IoBuilder.java 
b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/IoBuilder.java
index aae74f8..292f890 100644
--- a/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/IoBuilder.java
+++ b/log4j-iostreams/src/main/java/org/apache/logging/log4j/io/IoBuilder.java
@@ -31,6 +31,7 @@ import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.LoggingException;
 import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.spi.ExtendedLogger;
+import org.apache.logging.log4j.util.ReflectionUtil;
 
 /**
  * Builder class to wrap {@link Logger Loggers} into Java IO compatible 
classes.
@@ -101,9 +102,25 @@ public class IoBuilder {
         return new IoBuilder(LogManager.getLogger(clazz));
     }
 
-    // TODO: arg-less factory (blocked by LOG4J2-809)
+    /**
+     * Creates a new builder using a Logger named after the calling Class. 
This is equivalent to the following:
+     * <pre>
+     *     IoBuilder builder = IoBuilder.forLogger(LogManager.getLogger());
+     * </pre>
+     *
+     * @return a new IoBuilder
+     */
+    public static IoBuilder forLogger() {
+        return new 
IoBuilder(LogManager.getLogger(ReflectionUtil.getCallerClass(2)));
+    }
 
-    private IoBuilder(final Logger logger) {
+    /**
+     * Constructs a new IoBuilder for the given Logger. This method is 
provided for extensibility of this builder
+     * class. The static factory methods should be used normally.
+     *
+     * @param logger the {@link ExtendedLogger} to wrap
+     */
+    protected IoBuilder(final Logger logger) {
         if (!(logger instanceof ExtendedLogger)) {
             throw new UnsupportedOperationException("The provided Logger [" + 
String.valueOf(logger) +
                 "] does not implement " + ExtendedLogger.class.getName());
@@ -135,7 +152,13 @@ public class IoBuilder {
         return this;
     }
 
-    // FIXME: without making this entire class more properly extensible, this 
field is pointless
+    /**
+     * Specifies the fully qualified class name of the IO wrapper class 
implementation. This method should only be
+     * used when making significant extensions to the provided classes in this 
component and is normally unnecessary.
+     *
+     * @param fqcn the fully qualified class name of the IO wrapper class 
being built
+     * @return {@code this}
+     */
     public IoBuilder setWrapperClassName(final String fqcn) {
         this.fqcn = fqcn;
         return this;
@@ -241,6 +264,8 @@ public class IoBuilder {
         return this;
     }
 
+    // TODO: could this builder use generics to infer the desired IO class?
+
     /**
      * Builds a new {@link Reader} that is wiretapped by its underlying 
Logger. If buffering is enabled, then a
      * {@link java.io.BufferedReader} will be returned.

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a1fb000e/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/IoBuilderTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/IoBuilderTest.java 
b/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/IoBuilderTest.java
new file mode 100644
index 0000000..2b1757a
--- /dev/null
+++ 
b/log4j-iostreams/src/test/java/org/apache/logging/log4j/io/IoBuilderTest.java
@@ -0,0 +1,34 @@
+package org.apache.logging.log4j.io;
+
+import java.io.PrintStream;
+import java.util.List;
+
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.startsWith;
+import static org.junit.Assert.*;
+
+public class IoBuilderTest {
+
+    @Rule
+    public InitialLoggerContext context = new 
InitialLoggerContext("log4j2-streams-calling-info.xml");
+
+    @Test
+    public void testNoArgBuilderCallerClassInfo() throws Exception {
+        final PrintStream ps = IoBuilder.forLogger().buildPrintStream();
+        ps.println("discarded");
+        final ListAppender app = context.getListAppender("IoBuilderTest");
+        final List<String> messages = app.getMessages();
+        assertThat(messages, not(empty()));
+        assertThat(messages, hasSize(1));
+        final String message = messages.get(0);
+        assertThat(message, startsWith(getClass().getName() + 
".testNoArgBuilderCallerClassInfo"));
+        app.clear();
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a1fb000e/log4j-iostreams/src/test/resources/log4j2-streams-calling-info.xml
----------------------------------------------------------------------
diff --git a/log4j-iostreams/src/test/resources/log4j2-streams-calling-info.xml 
b/log4j-iostreams/src/test/resources/log4j2-streams-calling-info.xml
index 5f8dfd9..fca0510 100644
--- a/log4j-iostreams/src/test/resources/log4j2-streams-calling-info.xml
+++ b/log4j-iostreams/src/test/resources/log4j2-streams-calling-info.xml
@@ -15,16 +15,22 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License.
   -->
-<Configuration name="CallerInformationTest" status="error" 
packages="org.apache.logging.log4j.test">
+<Configuration name="CallerInformationTest" status="error">
   <Appenders>
     <List name="ClassAndMethod">
       <PatternLayout pattern="%class.%method"/>
     </List>
+    <List name="IoBuilderTest">
+      <PatternLayout pattern="%class.%method"/>
+    </List>
   </Appenders>
   <Loggers>
     <Logger name="ClassAndMethodLogger" level="info">
       <AppenderRef ref="ClassAndMethod"/>
     </Logger>
+    <Logger name="org.apache.logging.log4j.io.IoBuilderTest" level="info" 
additivity="false">
+      <AppenderRef ref="IoBuilderTest"/>
+    </Logger>
     <Root level="off"/>
   </Loggers>
-</Configuration>
\ No newline at end of file
+</Configuration>

Reply via email to