You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2012/09/26 00:09:50 UTC
svn commit: r1390188 - in /logging/log4j/log4j2/trunk:
api/src/main/java/org/apache/logging/log4j/status/
core/src/main/java/org/apache/logging/log4j/core/config/
core/src/test/java/org/apache/logging/log4j/core/config/
core/src/test/resources/ src/cha...
Author: rgoers
Date: Tue Sep 25 22:09:49 2012
New Revision: 1390188
URL: http://svn.apache.org/viewvc?rev=1390188&view=rev
Log:
Allow status output to be directed to stderr or a file
Added:
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java
- copied, changed from r1389231, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java
logging/log4j/log4j2/trunk/core/src/test/resources/log4j-filetest.xml
- copied, changed from r1389231, logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test1.xml
Modified:
logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
logging/log4j/log4j2/trunk/src/changes/changes.xml
logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml
Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java?rev=1390188&r1=1390187&r2=1390188&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java (original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/status/StatusConsoleListener.java Tue Sep 25 22:09:49 2012
@@ -18,6 +18,8 @@ package org.apache.logging.log4j.status;
import org.apache.logging.log4j.Level;
+import java.io.PrintStream;
+
/**
* StatusListener that writes to the Console.
*/
@@ -29,6 +31,8 @@ public class StatusConsoleListener imple
private String[] filters = null;
+ private final PrintStream stream;
+
/**
* Creates the StatusConsoleListener using either the level configured by the
* "org.apache.logging.log4j.StatusLevel" system property if it is set or to a
@@ -39,6 +43,7 @@ public class StatusConsoleListener imple
if (str != null) {
level = Level.toLevel(str, Level.FATAL);
}
+ stream = System.out;
}
/**
@@ -47,6 +52,16 @@ public class StatusConsoleListener imple
*/
public StatusConsoleListener(Level level) {
this.level = level;
+ stream = System.out;
+ }
+
+ /**
+ * Creates the StatusConsoleListener using the supplied Level.
+ * @param level The Level of status messages that should appear on the console.
+ */
+ public StatusConsoleListener(Level level, PrintStream stream) {
+ this.level = level;
+ this.stream = stream;
}
/**
@@ -63,7 +78,7 @@ public class StatusConsoleListener imple
*/
public void log(StatusData data) {
if (data.getLevel().isAtLeastAsSpecificAs(level) && !filtered(data)) {
- System.out.println(data.getFormattedStatus());
+ stream.println(data.getFormattedStatus());
}
}
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java?rev=1390188&r1=1390187&r2=1390188&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/JSONConfiguration.java Tue Sep 25 22:09:49 2012
@@ -20,6 +20,7 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.plugins.PluginManager;
import org.apache.logging.log4j.core.config.plugins.PluginType;
import org.apache.logging.log4j.core.config.plugins.ResolverUtil;
+import org.apache.logging.log4j.core.helpers.FileUtils;
import org.apache.logging.log4j.status.StatusConsoleListener;
import org.apache.logging.log4j.status.StatusListener;
import org.apache.logging.log4j.status.StatusLogger;
@@ -33,8 +34,12 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.PrintStream;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -74,6 +79,7 @@ public class JSONConfiguration extends B
processAttributes(rootNode, root);
Level status = Level.OFF;
boolean verbose = false;
+ PrintStream stream = System.out;
for (Map.Entry<String, String> entry : rootNode.getAttributes().entrySet()) {
if ("status".equalsIgnoreCase(entry.getKey())) {
status = Level.toLevel(getSubst().replace(entry.getValue()).toUpperCase(), null);
@@ -81,6 +87,20 @@ public class JSONConfiguration extends B
status = Level.ERROR;
messages.add("Invalid status specified: " + entry.getValue() + ". Defaulting to ERROR");
}
+ } else if ("dest".equalsIgnoreCase(entry.getKey())) {
+ String dest = entry.getValue();
+ if (dest != null) {
+ if (dest.equalsIgnoreCase("err")) {
+ stream = System.err;
+ } else {
+ try {
+ File destFile = FileUtils.fileFromURI(new URI(dest));
+ stream = new PrintStream(new FileOutputStream(destFile));
+ } catch (URISyntaxException use) {
+ System.err.println("Unable to write to " + dest + ". Writing to stdout");
+ }
+ }
+ }
} else if ("verbose".equalsIgnoreCase(entry.getKey())) {
verbose = Boolean.parseBoolean(getSubst().replace(entry.getValue()));
} else if ("packages".equalsIgnoreCase(entry.getKey())) {
@@ -111,7 +131,7 @@ public class JSONConfiguration extends B
}
}
if (!found && status != Level.OFF) {
- StatusConsoleListener listener = new StatusConsoleListener(status);
+ StatusConsoleListener listener = new StatusConsoleListener(status, stream);
if (!verbose) {
listener.setFilters(VERBOSE_CLASSES);
}
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java?rev=1390188&r1=1390187&r2=1390188&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java Tue Sep 25 22:09:49 2012
@@ -20,6 +20,7 @@ import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.plugins.PluginManager;
import org.apache.logging.log4j.core.config.plugins.PluginType;
import org.apache.logging.log4j.core.config.plugins.ResolverUtil;
+import org.apache.logging.log4j.core.helpers.FileUtils;
import org.apache.logging.log4j.status.StatusConsoleListener;
import org.apache.logging.log4j.status.StatusListener;
import org.apache.logging.log4j.status.StatusLogger;
@@ -46,8 +47,13 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.PrintStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -91,6 +97,7 @@ public class XMLConfiguration extends Ba
Map<String, String> attrs = processAttributes(rootNode, rootElement);
Level status = Level.OFF;
boolean verbose = false;
+ PrintStream stream = System.out;
for (Map.Entry<String, String> entry : attrs.entrySet()) {
if ("status".equalsIgnoreCase(entry.getKey())) {
@@ -99,6 +106,20 @@ public class XMLConfiguration extends Ba
status = Level.ERROR;
messages.add("Invalid status specified: " + entry.getValue() + ". Defaulting to ERROR");
}
+ } else if ("dest".equalsIgnoreCase(entry.getKey())) {
+ String dest = entry.getValue();
+ if (dest != null) {
+ if (dest.equalsIgnoreCase("err")) {
+ stream = System.err;
+ } else {
+ try {
+ File destFile = FileUtils.fileFromURI(new URI(dest));
+ stream = new PrintStream(new FileOutputStream(destFile));
+ } catch (URISyntaxException use) {
+ System.err.println("Unable to write to " + dest + ". Writing to stdout");
+ }
+ }
+ }
} else if ("verbose".equalsIgnoreCase(entry.getKey())) {
verbose = Boolean.parseBoolean(getSubst().replace(entry.getValue()));
} else if ("packages".equalsIgnoreCase(getSubst().replace(entry.getKey()))) {
@@ -132,7 +153,7 @@ public class XMLConfiguration extends Ba
}
}
if (!found && status != Level.OFF) {
- StatusConsoleListener listener = new StatusConsoleListener(status);
+ StatusConsoleListener listener = new StatusConsoleListener(status, stream);
if (!verbose) {
listener.setFilters(VERBOSE_CLASSES);
}
Copied: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java (from r1389231, logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java?p2=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java&p1=logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java&r1=1389231&r2=1390188&rev=1390188&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/XMLConfigurationTest.java (original)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/FileOutputTest.java Tue Sep 25 22:09:49 2012
@@ -16,39 +16,29 @@
*/
package org.apache.logging.log4j.core.config;
-import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.Appender;
-import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.filter.ThreadContextMapFilter;
import org.apache.logging.log4j.status.StatusLogger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
-import java.io.BufferedInputStream;
-import java.io.DataInputStream;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.util.Iterator;
-import java.util.Map;
-
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertEquals;
+import java.io.File;
+
+import static org.junit.Assert.*;
/**
*
*/
-public class XMLConfigurationTest {
+public class FileOutputTest {
- private static final String CONFIG = "log4j-test1.xml";
- private static final String LOGFILE = "target/test.log";
+ private static final String CONFIG = "log4j-filetest.xml";
+ private static final String STATUS_LOG = "target/status.log";
@BeforeClass
public static void setupClass() {
+ File file = new File(STATUS_LOG);
+ file.delete();
System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
LoggerContext ctx = (LoggerContext) LogManager.getContext();
Configuration config = ctx.getConfiguration();
@@ -68,51 +58,15 @@ public class XMLConfigurationTest {
LoggerContext ctx = (LoggerContext) LogManager.getContext();
ctx.reconfigure();
StatusLogger.getLogger().reset();
+ File file = new File(STATUS_LOG);
+ file.delete();
}
@Test
- public void testLogger() {
- Logger logger = LogManager.getLogger("org.apache.logging.log4j.test1.Test");
- assertTrue(logger instanceof org.apache.logging.log4j.core.Logger);
- org.apache.logging.log4j.core.Logger l = (org.apache.logging.log4j.core.Logger) logger;
- assertEquals(Level.DEBUG, l.getLevel());
- int filterCount = l.filterCount();
- assertTrue("number of filters - " + filterCount, filterCount == 1);
- Iterator<Filter> iter = l.getFilters();
- Filter filter = iter.next();
- assertTrue(filter instanceof ThreadContextMapFilter);
- Map<String, Appender> appenders = l.getAppenders();
- assertNotNull(appenders);
- assertTrue("number of appenders = " + appenders.size(), appenders.size() == 1);
- Appender a = appenders.get("STDOUT");
- assertNotNull(a);
- assertEquals(a.getName(), "STDOUT");
- }
-
- public void testConfiguredAppenders() {
- LoggerContext ctx = (LoggerContext) LogManager.getContext();
- Configuration c = ctx.getConfiguration();
- Map<String, Appender> apps = c.getAppenders();
- assertNotNull(apps);
- assertEquals(apps.size(), 3);
- }
-
- @Test
- public void logToFile() throws Exception {
- FileOutputStream fos = new FileOutputStream(LOGFILE, false);
- fos.flush();
- fos.close();
- Logger logger = LogManager.getLogger("org.apache.logging.log4j.test2.Test");
- logger.debug("This is a test");
- DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(LOGFILE)));
- int count = 0;
- String str = "";
- while (is.available() != 0) {
- str = is.readLine();
- ++count;
- }
- assertTrue("Incorrect count " + count, count == 1);
- assertTrue("Bad data", str.endsWith("This is a test"));
+ public void testConfig() {
+ File file = new File(STATUS_LOG);
+ assertTrue("Status output file does not exist", file.exists());
+ assertTrue("File is empty", file.length() > 0);
}
}
Copied: logging/log4j/log4j2/trunk/core/src/test/resources/log4j-filetest.xml (from r1389231, logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test1.xml)
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/log4j-filetest.xml?p2=logging/log4j/log4j2/trunk/core/src/test/resources/log4j-filetest.xml&p1=logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test1.xml&r1=1389231&r2=1390188&rev=1390188&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/log4j-test1.xml (original)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/log4j-filetest.xml Tue Sep 25 22:09:49 2012
@@ -16,7 +16,7 @@
limitations under the License.
-->
-<configuration status="warn" name="XMLConfigTest" packages="org.apache.logging.log4j.test">
+<configuration status="debug" dest="target/status.log" name="XMLConfigTest" packages="org.apache.logging.log4j.test">
<properties>
<property name="filename">target/test.log</property>
</properties>
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1390188&r1=1390187&r2=1390188&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Tue Sep 25 22:09:49 2012
@@ -23,6 +23,9 @@
<body>
<release version="2.0-beta2" date="TBD" description="Bug fixes and enhancements">
+ <action dev="rgoers" type="add">
+ Allow the status logging to be directed to stderr or to a file.
+ </action>
<action issue="LOG4J2-91" dev="rgoers" type="fix">
Log4j 1.2 adapter's Category class was missing 3 log methods.
</action>
Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml?rev=1390188&r1=1390187&r2=1390188&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml Tue Sep 25 22:09:49 2012
@@ -274,6 +274,10 @@
<th>Description</th>
</tr>
<tr>
+ <td>dest</td>
+ <td>Either "err", which will send output to stderr, or a file path or URL.</td>
+ </tr>
+ <tr>
<td>monitorInterval</td>
<td>The minimum amount of time, in seconds, that must elapse before the file configuration
is checked for changes.</td>
@@ -886,8 +890,9 @@
</p>
<p>
Applications may wish to direct the status output to some other destination. This can be accomplished
- by insuring the configured status is set to OFF and then configuring the application programmatically
- such as:
+ by setting the dest attribute to either "err" to send the output to stderr or to a file location or URL.
+ This can also be done by insuring the configured status is set to OFF and then configuring the application
+ programmatically such as:
<source>
StatusConsoleListener listener = new StatusConsoleListener(Level.ERROR);
((StatusLogger) logger).registerListener(listener);