You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2013/05/04 12:31:02 UTC
svn commit: r1479072 - in /logging/log4j/log4j2/trunk: .settings/
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/changes/ src/site/ src/site/xdoc/manual/
Author: rpopma
Date: Sat May 4 10:31:02 2013
New Revision: 1479072
URL: http://svn.apache.org/r1479072
Log:
LOG4J2-219: don't ignore configured named loggers if root logger is not configured and a default root logger is installed. Improved docs.
Added:
logging/log4j/log4j2/trunk/.settings/
logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/BaseConfigurationTest.java (with props)
logging/log4j/log4j2/trunk/core/src/test/resources/missingRootLogger.xml (with props)
Modified:
logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
logging/log4j/log4j2/trunk/src/changes/changes.xml
logging/log4j/log4j2/trunk/src/site/site.xml
logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm
Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java?rev=1479072&r1=1479071&r2=1479072&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java (original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java Sat May 4 10:31:02 2013
@@ -206,9 +206,9 @@ public class BaseConfiguration extends A
setToDefault();
return;
} else if (!setRoot) {
- LOGGER.warn("No Root logger was configured, using default");
+ LOGGER.warn("No Root logger was configured, creating default ERROR-level Root logger with Console appender");
setToDefault();
- return;
+ // return; // LOG4J2-219: creating default root=ok, but don't exclude configured Loggers
}
for (final Map.Entry<String, LoggerConfig> entry : loggers.entrySet()) {
Added: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/BaseConfigurationTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/BaseConfigurationTest.java?rev=1479072&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/BaseConfigurationTest.java (added)
+++ logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/BaseConfigurationTest.java Sat May 4 10:31:02 2013
@@ -0,0 +1,58 @@
+package org.apache.logging.log4j.core.config;
+
+import static org.junit.Assert.*;
+
+import java.util.Map;
+
+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.LoggerContext;
+import org.apache.logging.log4j.core.config.plugins.PluginManager;
+import org.junit.Test;
+
+public class BaseConfigurationTest {
+
+
+ @Test
+ public void testMissingRootLogger() throws Exception {
+ PluginManager.addPackage("org.apache.logging.log4j.test.appender");
+ LoggerContext ctx = Configurator.initialize("Test1", null, "missingRootLogger.xml");
+ final Logger logger = LogManager.getLogger("sample.Logger1");
+ Configuration config = ctx.getConfiguration();
+ assertNotNull("Config not null", config);
+// final String MISSINGROOT = "MissingRootTest";
+// assertTrue("Incorrect Configuration. Expected " + MISSINGROOT + " but found " + config.getName(),
+// MISSINGROOT.equals(config.getName()));
+ final Map<String, Appender<?>> map = config.getAppenders();
+ assertNotNull("Appenders not null", map);
+ assertEquals("Appenders Size", 2, map.size());
+ assertTrue("Contains List", map.containsKey("List"));
+ assertTrue("Contains Console", map.containsKey("Console"));
+
+ final Map<String, LoggerConfig> loggerMap = config.getLoggers();
+ assertNotNull("loggerMap not null", loggerMap);
+ assertEquals("loggerMap Size", 1, loggerMap.size());
+ // only the sample logger, no root logger in loggerMap!
+ assertTrue("contains key=sample", loggerMap.containsKey("sample"));
+
+ LoggerConfig sample = loggerMap.get("sample");
+ Map<String, Appender<?>> sampleAppenders = sample.getAppenders();
+ assertEquals("sampleAppenders Size", 1, sampleAppenders.size());
+ // sample only has List appender, not Console!
+ assertTrue("sample has appender List", sampleAppenders.containsKey("List"));
+
+ BaseConfiguration baseConfig = (BaseConfiguration) config;
+ LoggerConfig root = baseConfig.getRootLogger();
+ Map<String, Appender<?>> rootAppenders = root.getAppenders();
+ assertEquals("rootAppenders Size", 1, rootAppenders.size());
+ // root only has Console appender!
+ assertTrue("root has appender Console", rootAppenders.containsKey("Console"));
+ assertEquals(Level.ERROR, root.getLevel());
+
+ logger.isDebugEnabled();
+ Configurator.shutdown(ctx);
+ }
+
+}
Propchange: logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/core/config/BaseConfigurationTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: logging/log4j/log4j2/trunk/core/src/test/resources/missingRootLogger.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/test/resources/missingRootLogger.xml?rev=1479072&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/test/resources/missingRootLogger.xml (added)
+++ logging/log4j/log4j2/trunk/core/src/test/resources/missingRootLogger.xml Sat May 4 10:31:02 2013
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration name="MissingRootTest" status="DEBUG">
+ <appenders>
+ <List name="List">
+ <PatternLayout>
+ <pattern>%d %p %c{1.} [%t] %X{aKey} %location %m %ex%n</pattern>
+ </PatternLayout>
+ </List>
+ </appenders>
+
+ <loggers>
+ <!--
+ <root level="info" includeLocation="false">
+ <appender-ref ref="List"/>
+ </root>
+ -->
+ <logger name="sample" level="info" includeLocation="false">
+ <appender-ref ref="List"/>
+ </logger>
+ </loggers>
+</configuration>
\ No newline at end of file
Propchange: logging/log4j/log4j2/trunk/core/src/test/resources/missingRootLogger.xml
------------------------------------------------------------------------------
svn:eol-style = native
Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1479072&r1=1479071&r2=1479072&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sat May 4 10:31:02 2013
@@ -23,6 +23,10 @@
<body>
<release version="2.0-beta6" date="@TBD@" description="Bug fixes and enhancements">
+ <action issue="LOG4J2-219" dev="rpopma" type="fix" due-to="Peter DePasquale">
+ Fix: install default root logger if not configured (this is unchanged),
+ but make sure to include configured named loggers. Clarified documentation.
+ </action>
<action issue="LOG4J2-159" dev="rgoers" type="fix">
Use OSGi version format in Fragment-Host
</action>
Modified: logging/log4j/log4j2/trunk/src/site/site.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/site.xml?rev=1479072&r1=1479071&r2=1479072&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/site.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/site.xml Sat May 4 10:31:02 2013
@@ -41,7 +41,13 @@
<item name="Automatic Configuration" href="/manual/configuration.html#AutomaticConfiguration"/>
<item name="Additivity" href="/manual/configuration.html#Additivity"/>
<item name="Automatic Reconfiguration" href="/manual/configuration.html#AutomaticReconfiguration"/>
- <item name="Configuration Syntax" href="/manual/configuration.html#ConfigurationSyntax"/>
+ <item name="Advertising Appender Config" href="/manual/configuration.html#AdvertisingAppenderConfigurations"/>
+ <item name="Configuration Syntax" href="/manual/configuration.html#ConfigurationSyntax" />
+ <item name="XML Syntax" href="/manual/configuration.html#XML"/>
+ <item name="JSON Syntax" href="/manual/configuration.html#JSON"/>
+ <item name="Configuring Loggers" href="/manual/configuration.html#Loggers"/>
+ <item name="Configuring Appenders" href="/manual/configuration.html#Appenders"/>
+ <item name="Configuring Filters" href="/manual/configuration.html#Filters"/>
<item name="Property Substitution" href="/manual/configuration.html#PropertySubstitution"/>
<item name="Status Messages" href="/manual/configuration.html#StatusMessages"/>
<item name="Unit Testing in Maven" href="/manual/configuration.html#UnitTestingInMaven"/>
Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm?rev=1479072&r1=1479071&r2=1479072&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/configuration.xml.vm Sat May 4 10:31:02 2013
@@ -131,7 +131,7 @@ public class Bar {
}</pre>
</p>
<p>
- If no configuration files are present logback will default to the DefaultConfiguration which
+ If no configuration files are present log4j will default to the DefaultConfiguration which
will set up a minimal logging environment consisting of a
<a href="../log4j-core/apidocs/org/apache/logging/log4j/core/appender/ConsoleAppender.html">ConsoleAppender</a>
attached to the root logger. The output will be formatted using a
@@ -151,7 +151,7 @@ public class Bar {
configuration equivalent to the default would look like:
<pre class="prettyprint linenums"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
-<configuration status="OFF">
+<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
@@ -200,7 +200,7 @@ public class Bar {
</p>
<pre class="prettyprint linenums"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
-<configuration status="OFF">
+<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
@@ -232,7 +232,7 @@ public class Bar {
and so it is possible to disable it by setting the additivity attribute on the logger to false:
</p>
<pre class="prettyprint linenums"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
-<configuration status="OFF">
+<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
@@ -311,6 +311,7 @@ public class Bar {
as the action being performed for a specific user, route output to Flume or a log reporting system,
etc. Being able to do this requires understanding the syntax of the configuration files.
</p>
+ <a name="XML"/>
<h4>Configuration with XML</h4>
<p>
The configuration element in the XML file accepts several attributes:
@@ -492,6 +493,7 @@ public class Bar {
</loggers>
</configuration>]]></pre>
+ <a name="JSON"/>
<h4>Configuration with JSON</h4>
<p>
In addition to XML, Log4j can be configured using JSON. The JSON format is very similar to the
@@ -604,6 +606,7 @@ public class Bar {
<version>1.9.2</version>
</dependency>]]></pre>
</p>
+ <a name="Loggers"/>
<h4>Configuring loggers</h4>
<p>
An understanding of how loggers work in Log4j is critical before trying to configure them.
@@ -632,14 +635,15 @@ public class Bar {
are configured on the LoggerConfig each of them be called when processing logging events.
</p>
<p>
- Every configuration must have a root logger. If one is not configured the default root LoggerConfig,
- which has a level of ERROR but with no appenders attached, will be used. The main differences
+ <b><em>Every configuration must have a root logger</em></b>. If one is not configured the default root LoggerConfig,
+ which has a level of ERROR and has a Console appender attached, will be used. The main differences
between the root logger and other loggers are
<ol>
<li>The root logger does not have a name attribute.</li>
<li>The root logger does not support the additivity attribute since it has no parent.</li>
</ol>
</p>
+ <a name="Appenders"/>
<h4>Configuring Appenders</h4>
<p>
An appender is configured either using the specific appender plugin's name or with an appender
@@ -653,6 +657,7 @@ public class Bar {
along with a type attribute that contains the layout plugin's name. The various appenders
will contain other attributes or elements that are required for them to function properly.
</p>
+ <a name="Filters"/>
<h4>Configuring Filters</h4>
<p>
Log4j allows a filter to be specified in any of 4 places: