Author: rgoers
Date: Thu Nov 24 02:07:47 2011
New Revision: 1205703
URL: http://svn.apache.org/viewvc?rev=1205703&view=rev
Log:
Started work on configuration documentation. Changed configuration loading to
match documentation
Added:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicLoggingTest.java
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j12-api/src/test/java/org/apache/log4j/BasicConfigurationFactory.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfigurationFactory.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfigurationFactory.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicConfigurationFactory.java
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/site.xml
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/xdoc/manual/configuration.xml
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j12-api/src/test/java/org/apache/log4j/BasicConfigurationFactory.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j12-api/src/test/java/org/apache/log4j/BasicConfigurationFactory.java?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j12-api/src/test/java/org/apache/log4j/BasicConfigurationFactory.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j12-api/src/test/java/org/apache/log4j/BasicConfigurationFactory.java
Thu Nov 24 02:07:47 2011
@@ -21,8 +21,7 @@ import org.apache.logging.log4j.core.con
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.LoggerConfig;
-import org.apache.logging.log4j.core.config.Order;
-import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.xml.sax.InputSource;
import java.net.URI;
@@ -31,6 +30,17 @@ import java.net.URI;
*/
public class BasicConfigurationFactory extends ConfigurationFactory {
+ @Override
+ public String[] getSupportedTypes() {
+ return new String[] {"*"};
+ }
+
+ @Override
+ public Configuration getConfiguration(InputSource source) {
+ return new BasicConfiguration();
+ }
+
+ @Override
public Configuration getConfiguration(String name, URI configLocation) {
return new BasicConfiguration();
}
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java
Thu Nov 24 02:07:47 2011
@@ -3,9 +3,19 @@ package org.apache.logging.log4j.core.co
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.plugins.PluginManager;
import org.apache.logging.log4j.core.config.plugins.PluginType;
+import org.apache.logging.log4j.core.helpers.FileUtils;
+import org.apache.logging.log4j.core.helpers.Loader;
import org.apache.logging.log4j.status.StatusLogger;
+import org.xml.sax.InputSource;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -31,9 +41,19 @@ public abstract class ConfigurationFacto
public static final String CONFIGURATION_FACTORY_PROPERTY =
"log4j.configurationFactory";
+ public static final String CONFIGURATION_FILE_PROPERTY =
"log4j.configurationFile";
+
private static List<ConfigurationFactory> factories = new
ArrayList<ConfigurationFactory>();
- private static Logger logger = StatusLogger.getLogger();
+ protected static Logger logger = StatusLogger.getLogger();
+
+ protected File configFile = null;
+
+ protected static final String TEST_PREFIX = "log4j2-test";
+
+ protected static final String DEFAULT_PREFIX = "log4j2";
+
+ private static ConfigurationFactory configFactory = new Factory();
public static ConfigurationFactory getInstance() {
String factoryClass =
System.getProperty(CONFIGURATION_FACTORY_PROPERTY);
@@ -59,7 +79,7 @@ public abstract class ConfigurationFacto
for (WeightedFactory wf : ordered) {
addFactory(wf.factoryClass);
}
- return new Factory();
+ return configFactory;
}
private static void addFactory(String factoryClass) {
@@ -82,14 +102,92 @@ public abstract class ConfigurationFacto
}
public static void setConfigurationFactory(ConfigurationFactory factory) {
- factories.add(0, factory);
+ configFactory = factory;
+ }
+
+ public static void resetConfigurationFactory() {
+ configFactory = new Factory();
}
public static void removeConfigurationFactory(ConfigurationFactory
factory) {
factories.remove(factory);
}
- public abstract Configuration getConfiguration(String name, URI
configLocation);
+ protected abstract String[] getSupportedTypes();
+
+ protected boolean isActive() {
+ return true;
+ }
+
+ public abstract Configuration getConfiguration(InputSource source);
+
+ public Configuration getConfiguration(String name, URI configLocation) {
+ if (!isActive()) {
+ return null;
+ }
+ if (configLocation != null) {
+ InputSource source = getInputFromURI(configLocation);
+ if (source != null) {
+ return getConfiguration(source);
+ }
+ }
+ return null;
+ }
+
+ protected InputSource getInputFromURI(URI configLocation) {
+ configFile = FileUtils.fileFromURI(configLocation);
+ if (configFile != null && configFile.exists() && configFile.canRead())
{
+ try {
+ InputSource source = new InputSource(new
FileInputStream(configFile));
+ source.setSystemId(configLocation.getPath());
+ return source;
+ } catch (FileNotFoundException ex) {
+ logger.error("Cannot locate file " + configLocation.getPath(),
ex);
+ }
+ }
+ try {
+ InputSource source = new
InputSource(configLocation.toURL().openStream());
+ source.setSystemId(configLocation.getPath());
+ return source;
+ } catch (MalformedURLException ex) {
+ logger.error("Invalid URL " + configLocation.toString(), ex);
+ } catch (IOException ex) {
+ logger.error("Unable to access " + configLocation.toString(), ex);
+ }
+ return null;
+ }
+
+ protected InputSource getInputFromString(ClassLoader loader, String
configFile) {
+ InputSource source;
+ try {
+ URL url = new URL(configFile);
+ source = new InputSource(url.openStream());
+ source.setSystemId(configFile);
+ return source;
+ } catch (Exception ex) {
+ source = getInputFromResource(configFile, loader);
+ if (source == null) {
+ try {
+ InputStream is = new FileInputStream(configFile);
+ source = new InputSource(is);
+ source.setSystemId(configFile);
+ } catch (FileNotFoundException fnfe) {
+ // Ignore the exception
+ }
+ }
+ }
+ return source;
+ }
+
+ protected InputSource getInputFromResource(String resource, ClassLoader
loader) {
+ InputStream is = Loader.getResourceAsStream(resource, loader);
+ if (is == null) {
+ return null;
+ }
+ InputSource source = new InputSource(is);
+ source.setSystemId(resource);
+ return source;
+ }
private static class WeightedFactory implements
Comparable<WeightedFactory> {
private int weight;
@@ -116,13 +214,81 @@ public abstract class ConfigurationFacto
public Configuration getConfiguration(String name, URI configLocation)
{
+ if (configLocation == null) {
+ String config =
System.getProperty(CONFIGURATION_FILE_PROPERTY);
+ if (config != null) {
+ ClassLoader loader = this.getClass().getClassLoader();
+ InputSource source = getInputFromString(loader, config);
+ if (source != null) {
+ for (ConfigurationFactory factory : factories) {
+ String[] types = factory.getSupportedTypes();
+ if (types != null) {
+ for (String type : types) {
+ if (type.equals("*") ||
config.endsWith(type)) {
+ Configuration c =
factory.getConfiguration(source);
+ if (c != null) {
+ return c;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ } else {
+ for (ConfigurationFactory factory : factories) {
+ Configuration config = factory.getConfiguration(name,
configLocation);
+ if (config != null) {
+ return config;
+ }
+ }
+ }
+
+ Configuration config = getConfiguration(true, name);
+ if (config == null) {
+ config = getConfiguration(true, null);
+ if (config == null) {
+ config = getConfiguration(false, name);
+ if (config == null) {
+ config = getConfiguration(false, null);
+ }
+ }
+ }
+ return config != null ? config : new DefaultConfiguration();
+ }
+
+ private Configuration getConfiguration(boolean isTest, String name) {
+ boolean named = (name != null && name.length() > 0);
+ ClassLoader loader = this.getClass().getClassLoader();
for (ConfigurationFactory factory : factories) {
- Configuration c = factory.getConfiguration(name,
configLocation);
- if (c != null) {
- return c;
+ String configName;
+ String prefix = isTest ? TEST_PREFIX : DEFAULT_PREFIX;
+ String [] types = factory.getSupportedTypes();
+ if (types == null) {
+ continue;
+ }
+
+ for (String suffix : types) {
+ if (suffix.equals("*")) {
+ continue;
+ }
+ configName = named ? prefix + name + suffix : prefix +
suffix;
+
+ InputSource source = getInputFromResource(configName,
loader);
+ if (source != null) {
+ return factory.getConfiguration(source);
+ }
}
}
- return new DefaultConfiguration();
+ return null;
+ }
+
+ public String[] getSupportedTypes() {
+ return null;
+ }
+
+ public Configuration getConfiguration(InputSource source) {
+ return null;
}
}
}
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
Thu Nov 24 02:07:47 2011
@@ -21,6 +21,7 @@ import org.apache.logging.log4j.core.App
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
+import org.apache.logging.log4j.core.layout.PatternLayout;
/**
* The default configuration. It writes all output to the Console using the
default logging level
@@ -47,33 +48,15 @@ public class DefaultConfiguration extend
public DefaultConfiguration() {
setName(DEFAULT_NAME);
- Appender appender = new ConsoleAppender("Console", new BasicLayout());
+ Layout layout = PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%thread]
%-5level %logger{36} - %msg%n",
+ null, null, null);
+ Appender appender = new ConsoleAppender("Console", layout);
addAppender(appender);
LoggerConfig root = getRootLogger();
root.addAppender(appender);
+
String l = System.getProperty(DEFAULT_LEVEL);
Level level = (l != null && Level.valueOf(l) != null) ?
Level.valueOf(l) : Level.ERROR;
root.setLevel(level);
}
-
- /**
- * Formats the event using the Message's built-in format.
- */
- public class BasicLayout implements Layout<String> {
- public byte[] format(LogEvent event) {
- return formatAs(event).getBytes();
- }
-
- public String formatAs(LogEvent event) {
- return event.getMessage().getFormattedMessage() + "\n";
- }
-
- public byte[] getHeader() {
- return EMPTY_STRING.getBytes();
- }
-
- public byte[] getFooter() {
- return EMPTY_STRING.getBytes();
- }
- }
}
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfigurationFactory.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfigurationFactory.java?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfigurationFactory.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/JSONConfigurationFactory.java
Thu Nov 24 02:07:47 2011
@@ -20,28 +20,19 @@ import org.apache.logging.log4j.core.con
import org.xml.sax.InputSource;
import java.io.File;
-import java.net.URI;
/**
*
*/
@Plugin(name="JSONConfigurationFactory", type="ConfigurationFactory")
@Order(6)
-public class JSONConfigurationFactory extends XMLConfigurationFactory {
+public class JSONConfigurationFactory extends ConfigurationFactory {
- public static final String DEFAULT_CONFIG_FILE = "log4j2.json";
-
- public static final String TEST_CONFIG_FILE = "log4j2-test.json";
-
- public static final String TEST_PREFIX = "log4j2-test";
-
- public static final String DEFAULT_PREFIX = "log4j2";
-
- public static final String SUFFIX = ".json";
+ public static final String[] SUFFIXES = new String[] {".json", ".jsn"};
private File configFile = null;
- private String[] dependencies = new String[] {
+ private static String[] dependencies = new String[] {
"org.codehaus.jackson.JsonNode",
"org.codehaus.jackson.map.ObjectMapper"
};
@@ -61,34 +52,19 @@ public class JSONConfigurationFactory ex
isActive = true;
}
- public Configuration getConfiguration(String name, URI configLocation) {
- InputSource source = null;
- if (configLocation != null) {
- source = getInputFromURI(configLocation);
- }
- if (source == null) {
- String testName;
- String defaultName;
- boolean named = (name != null && name.length() > 0);
- if (named) {
- testName = TEST_PREFIX + name + SUFFIX;
- defaultName = DEFAULT_PREFIX + name + SUFFIX;
- } else {
- testName = TEST_CONFIG_FILE;
- defaultName = DEFAULT_CONFIG_FILE;
- }
- ClassLoader loader = this.getClass().getClassLoader();
- source = getInputFromSystemProperty(loader, ".json");
- if (source == null) {
- source = getInputFromResource(testName, loader);
- if (source == null) {
- source = getInputFromResource(defaultName, loader);
- }
- if (source == null) {
- return named ? getConfiguration(null, null) : null;
- }
- }
+ @Override
+ protected boolean isActive() {
+ return isActive;
+ }
+
+ public Configuration getConfiguration(InputSource source) {
+ if (!isActive) {
+ return null;
}
return new JSONConfiguration(source, configFile);
}
+
+ public String[] getSupportedTypes() {
+ return SUFFIXES;
+ }
}
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfigurationFactory.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfigurationFactory.java?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfigurationFactory.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/config/XMLConfigurationFactory.java
Thu Nov 24 02:07:47 2011
@@ -16,21 +16,10 @@
*/
package org.apache.logging.log4j.core.config;
-import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.helpers.FileUtils;
-import org.apache.logging.log4j.core.helpers.Loader;
-import org.apache.logging.log4j.status.StatusLogger;
import org.xml.sax.InputSource;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
import java.net.URI;
-import java.net.URL;
/**
*
@@ -39,109 +28,13 @@ import java.net.URL;
@Order(5)
public class XMLConfigurationFactory extends ConfigurationFactory {
- public static final String CONFIGURATION_FILE_PROPERTY =
"log4j.configurationFile";
+ public static final String[] SUFFIXES = new String[] {".xml", "*"};
- public static final String DEFAULT_CONFIG_FILE = "log4j2.xml";
-
- public static final String TEST_CONFIG_FILE = "log4j2-test.xml";
-
- public static final String TEST_PREFIX = "log4j2-test";
-
- public static final String DEFAULT_PREFIX = "log4j2";
-
- public static final String SUFFIX = ".xml";
-
- protected static Logger logger = StatusLogger.getLogger();
-
- private File configFile = null;
-
- public Configuration getConfiguration(String name, URI configLocation) {
- InputSource source = null;
- if (configLocation != null) {
- source = getInputFromURI(configLocation);
- }
- if (source == null) {
- String testName;
- String defaultName;
- boolean named = (name != null && name.length() > 0);
- if (named) {
- testName = TEST_PREFIX + name + SUFFIX;
- defaultName = DEFAULT_PREFIX + name + SUFFIX;
- } else {
- testName = TEST_CONFIG_FILE;
- defaultName = DEFAULT_CONFIG_FILE;
- }
- ClassLoader loader = this.getClass().getClassLoader();
- source = getInputFromSystemProperty(loader, null);
- if (source == null) {
- source = getInputFromResource(testName, loader);
- if (source == null) {
- source = getInputFromResource(defaultName, loader);
- }
- if (source == null) {
- return named ? getConfiguration(null, null) : null;
- }
- }
- }
+ public Configuration getConfiguration(InputSource source) {
return new XMLConfiguration(source, configFile);
}
- protected InputSource getInputFromURI(URI configLocation) {
- configFile = FileUtils.fileFromURI(configLocation);
- if (configFile != null && configFile.exists() && configFile.canRead())
{
- try {
- InputSource source = new InputSource(new
FileInputStream(configFile));
- source.setSystemId(configLocation.getPath());
- return source;
- } catch (FileNotFoundException ex) {
- logger.error("Cannot locate file " + configLocation.getPath(),
ex);
- }
- }
- try {
- InputSource source = new
InputSource(configLocation.toURL().openStream());
- source.setSystemId(configLocation.getPath());
- return source;
- } catch (MalformedURLException ex) {
- logger.error("Invalid URL " + configLocation.toString(), ex);
- } catch (IOException ex) {
- logger.error("Unabled to access " + configLocation.toString(), ex);
- }
- return null;
- }
-
- protected InputSource getInputFromSystemProperty(ClassLoader loader,
String suffix) {
- String configFile = System.getProperty(CONFIGURATION_FILE_PROPERTY);
- if (configFile == null || (suffix != null &&
!configFile.toLowerCase().endsWith(suffix.toLowerCase()))) {
- return null;
- }
- InputSource source;
- try {
- URL url = new URL(configFile);
- source = new InputSource(url.openStream());
- source.setSystemId(configFile);
- return source;
- } catch (Exception ex) {
- source = getInputFromResource(configFile, loader);
- if (source == null) {
- try {
- InputStream is = new FileInputStream(configFile);
- source = new InputSource(is);
- source.setSystemId(configFile);
- } catch (FileNotFoundException fnfe) {
- // Ignore the exception
- }
- }
- }
- return source;
- }
-
- protected InputSource getInputFromResource(String resource, ClassLoader
loader) {
- InputStream is = Loader.getResourceAsStream(resource, loader);
- if (is == null) {
- return null;
- }
- InputSource source = new InputSource(is);
- source.setSystemId(resource);
- return source;
+ public String[] getSupportedTypes() {
+ return SUFFIXES;
}
}
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicConfigurationFactory.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicConfigurationFactory.java?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicConfigurationFactory.java
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicConfigurationFactory.java
Thu Nov 24 02:07:47 2011
@@ -21,6 +21,7 @@ import org.apache.logging.log4j.core.con
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.xml.sax.InputSource;
import java.net.URI;
@@ -33,16 +34,26 @@ public class BasicConfigurationFactory e
return new BasicConfiguration();
}
+ @Override
+ public String[] getSupportedTypes() {
+ return null;
+ }
+
+ @Override
+ public Configuration getConfiguration(InputSource source) {
+ return null;
+ }
+
public class BasicConfiguration extends BaseConfiguration {
- private static final String DEFAULT_LEVEL =
"org.apache.logging.log4j.level";
+ private static final String DEFAULT_LEVEL =
"org.apache.logging.log4j.level";
- public BasicConfiguration() {
+ public BasicConfiguration() {
- LoggerConfig root = getRootLogger();
- String l = System.getProperty(DEFAULT_LEVEL);
- Level level = (l != null && Level.valueOf(l) != null) ?
Level.valueOf(l) : Level.ERROR;
- root.setLevel(level);
+ LoggerConfig root = getRootLogger();
+ String l = System.getProperty(DEFAULT_LEVEL);
+ Level level = (l != null && Level.valueOf(l) != null) ?
Level.valueOf(l) : Level.ERROR;
+ root.setLevel(level);
+ }
}
}
-}
Added:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicLoggingTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicLoggingTest.java?rev=1205703&view=auto
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicLoggingTest.java
(added)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/test/java/org/apache/logging/log4j/core/BasicLoggingTest.java
Thu Nov 24 02:07:47 2011
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class BasicLoggingTest {
+
+ @Test
+ public void test1() {
+ Logger logger = LogManager.getLogger(BasicLoggingTest.class.getName());
+ logger.debug("debug not set");
+ logger.error("Test message");
+ }
+}
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/site.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/site.xml?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/site.xml
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/site.xml Thu
Nov 24 02:07:47 2011
@@ -19,7 +19,7 @@
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
- <version>1.0-SNAPSHOT</version>
+ <version>1.0</version>
</skin>
<custom>
<fluidoSkin>
Modified:
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/xdoc/manual/configuration.xml
URL:
http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/xdoc/manual/configuration.xml?rev=1205703&r1=1205702&r2=1205703&view=diff
==============================================================================
---
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/xdoc/manual/configuration.xml
(original)
+++
logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/src/site/xdoc/manual/configuration.xml
Thu Nov 24 02:07:47 2011
@@ -24,7 +24,163 @@
<body>
<section name="Configuration">
+ <p>Inserting log requests into the application code requires a fair
+ amount of planning and effort. Observation shows that approximately 4
+ percent of code is dedicated to logging. Consequently, even
moderately
+ sized applications will have thousands of logging statements embedded
+ within their code. Given their number, it becomes imperative to
+ manage these log statements without the need to modify them manually.
+ </p>
+ <p>
+ Configuration of Log4j 2 can be accomplished in 1 of 4 ways:
+ <ol>
+ <li>Through a configuration file written in XML or JSON.</li>
+ <li>Programmatically, by creating a ConfigurationFactory and
Configuration implementation.</li>
+ <li>Programmatically, by calling the APIs exposed in the
Configuration interface to add
+ components to the default configuration.</li>
+ <li>Programmatically, by calling methods on the internal Logger
class.</li>
+ </ol>
+ This page focuses primarily on configuring Log4j through a
configuration file. Information on
+ programmatically configuring Log4j can be found at <a
href="../extending.html">Extending Log4j 2</a>.
+ </p>
+ <p>
+ Note that unlike Log4j 1.x, the public Log4j 2 API does not expose
methods to add, modify or remove
+ appenders and filters or manipulate the configuration in any way.
+ </p>
+ <subsection name="Automatic Configuration">
+ <p>
+ Log4j has the ability to automatically configure itself during
initialization.
+ When Log4j starts it will locate all the ConfigurationFactory
plugins and arrange then in weighted
+ order from highest to lowest. As delivered, Log4j contains two
ConfigurationFactory implementations,
+ one for JSON and one for XML.
+ <ol>
+ <li>Log4j will inspect the "log4j.configurationFile" system
property and, if set, will attempt to
+ load the configuration using the
<code>ConfigurationFactory</code> that matches the file
+ extension.</li>
+ <li>If no system property is set the JSON ConfigurationFactory
will look for log4j-test.json or
+ log4j-test.jsn in the classpath.</li>
+ <li>If no such file is found the XML ConfigurationFactory will
look for log4j-test.xml in the
+ classpath.</li>
+ <li>If a test file cannot be located the JSON
ConfigurationFactory will look for log4j.json or log4j.jsn
+ on the classpath.</li>
+ <li>If a JSON file cannot be located the XML
ConfigurationFactory will try to locate
+ log4j.xml on the classpath.</li>
+ <li>If no configuration file could be located the
<code>DefaultConfiguration</code> will
+ be used. This will cause logging output to go to the
console.</li>
+ </ol>
+ </p>
+ <p>An example application named <code>MyApp</code> that uses log4j
can be used to illustrate how
+ this is done.
+ </p>
+ <p>
+<source> import com.foo.Bar;
+ // Import log4j classes.
+ import org.apache.logging.log4j.Logger;
+
+ public class MyApp {
+
+ // Define a static logger variable so that it references the
+ // Logger instance named "MyApp".
+ Logger logger = LogManager.getLogger(MyApp.class.getName());
+
+ public static void main(String[] args) {
+
+ // Set up a simple configuration that logs on the console.
+
+ logger.trace("Entering application.");
+ Bar bar = new Bar();
+ bar.doIt();
+ logger.trace("Exiting application." }
+ }</source>
+ </p>
+
+ <p>
+ <code>MyApp</code> begins by importing log4j related classes. It
+ then defines a static logger variable with the name
<code>MyApp</code>
+ which happens to be the fully qualified name of the class.
+ </p>
+
+ <p>
+ <code>MyApp</code> uses the <code>Bar</code> class defined in the
package<code>com.foo</code>.
+ </p>
+
+ <p>
+<source> package com.foo;
+ import org.apache.logging.log4j.Logger;
+
+ public class Bar {
+ static Logger logger = LogManager.getLogger(Bar.class.getName());
+
+ public void doIt() {
+ logger.error("Did it again!");
+ }
+ }</source>
+ </p>
+
+ <p>
+ If no configuration files are present logback will default to the
DefaultConfiguration which
+ will set up a minimal logging environment consisting of a <a
href="">ConsoleAppender</a> attached
+ to the root logger. The output will be formatted using a
+ <a href="">PatternLayout</a> set to the pattern "%d{HH:mm:ss.SSS}
[%t] %-5level %logger{36} - %msg%n".
+ </p>
+
+ <p>
+ Note that by default, the root logger is assigned to
<code>Level.ERROR</code>.
+ </p>
+
+ <p>The output of MyApp would be similar to:
+ <pre>
+ 17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
+ </pre>
+ </p>
+ <p>
+ As was described previously, Log4j will first attempt to configure
itself from configuration files. A
+ configuration equivalent to the default would look like:
+ <source><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
+<configuration status="OFF">
+ <appenders>
+ <Console name="Console" target="SYSTEM_OUT">
+ <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
+ </Console>
+ </appenders>
+ <loggers>
+ <root level="error">
+ <appender-ref ref="Console"/>
+ </root>
+ </loggers>
+</configuration>]]></source>
+ Once the file above is placed into the classpath as log4j.xml you
will get results identical to
+ those listed above. Changing the root level to trace will result
in results similar to:
+ <pre>
+ 17:13:01.540 [main] TRACE MyApp - Entering application.
+ 17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
+ 17:13:01.540 [main] TRACE MyApp - Exiting application.</pre>
+ </p>
+ <p>
+ Note that status logging is disabled using the default
configuration.
+ </p>
+ </subsection>
+
+ <subsection name="Automatic Reconfiguration">
+
+ </subsection>
+
+ <subsection name="Configuration Syntax">
+
+ </subsection>
+
+ <subsection name="Property Substitution">
+
+ </subsection>
+
+
+ <subsection name="Status Messages">
+
+ </subsection>
+ <subsection name="Unit Testing in Maven">
+
+ </subsection>
</section>
</body>
</document>
\ No newline at end of file