You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/09/21 19:59:17 UTC
[1/3] git commit: Simplify ListAppender plugin factory.
Repository: logging-log4j2
Updated Branches:
refs/heads/master fd32b63f4 -> a1fb000e8
Simplify ListAppender plugin factory.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/939ef584
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/939ef584
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/939ef584
Branch: refs/heads/master
Commit: 939ef584a029c2b053055a0f847ff8754d232b14
Parents: fd32b63
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 21 12:42:36 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 21 12:42:36 2014 -0500
----------------------------------------------------------------------
.../log4j/test/appender/ListAppender.java | 22 +++++++-------------
1 file changed, 8 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/939ef584/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/ListAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/ListAppender.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/ListAppender.java
index 0885aff..1e5fb94 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/ListAppender.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/appender/ListAppender.java
@@ -31,6 +31,7 @@ import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
import org.apache.logging.log4j.core.layout.SerializedLayout;
/**
@@ -44,7 +45,7 @@ public class ListAppender extends AbstractAppender {
private static final long serialVersionUID = 1L;
// Use CopyOnWriteArrayList?
-
+
private final List<LogEvent> events = new ArrayList<LogEvent>();
private final List<String> messages = new ArrayList<String>();
@@ -163,21 +164,14 @@ public class ListAppender extends AbstractAppender {
@PluginFactory
public static ListAppender createAppender(
- @PluginAttribute("name") final String name,
- @PluginAttribute("entryPerNewLine") final String newLine,
- @PluginAttribute("raw") final String raw,
+ @PluginAttribute("name")
+ @Required(message = "No name provided for ListAppender")
+ final String name,
+ @PluginAttribute("entryPerNewLine") final boolean newLine,
+ @PluginAttribute("raw") final boolean raw,
@PluginElement("Layout") final Layout<? extends Serializable> layout,
@PluginElement("Filter") final Filter filter) {
-
- if (name == null) {
- LOGGER.error("No name provided for ListAppender");
- return null;
- }
-
- final boolean nl = Boolean.parseBoolean(newLine);
- final boolean r = Boolean.parseBoolean(raw);
-
- return new ListAppender(name, filter, layout, nl, r);
+ return new ListAppender(name, filter, layout, newLine, raw);
}
/**
[3/3] git commit: Add no-arg factory method to IoBuilder.
Posted by ma...@apache.org.
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 <ma...@apache.org>
Authored: Sun Sep 21 12:59:18 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
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>
[2/3] git commit: Add hamcrest-all to log4j-iostreams test
dependencies.
Posted by ma...@apache.org.
Add hamcrest-all to log4j-iostreams test dependencies.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/0387be8f
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/0387be8f
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/0387be8f
Branch: refs/heads/master
Commit: 0387be8f9fdac3d68cbbdcdc625155ff5eb70f63
Parents: 939ef58
Author: Matt Sicker <ma...@apache.org>
Authored: Sun Sep 21 12:42:55 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Sun Sep 21 12:42:55 2014 -0500
----------------------------------------------------------------------
log4j-iostreams/pom.xml | 5 +++++
1 file changed, 5 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/0387be8f/log4j-iostreams/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-iostreams/pom.xml b/log4j-iostreams/pom.xml
index cb795ad..9c7cdc5 100644
--- a/log4j-iostreams/pom.xml
+++ b/log4j-iostreams/pom.xml
@@ -59,6 +59,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-all</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>