Repository: logging-log4j2 Updated Branches: refs/heads/master 8d4dc1f28 -> 72ef0eb4c
Add example of creating a custom configuration 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/7c8dffc3 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/7c8dffc3 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/7c8dffc3 Branch: refs/heads/master Commit: 7c8dffc30cb73c0de3d8fc0a9ea3bb71efea7317 Parents: 96327da Author: Ralph Goers <[email protected]> Authored: Sat Jul 4 23:01:31 2015 -0700 Committer: Ralph Goers <[email protected]> Committed: Sat Jul 4 23:01:31 2015 -0700 ---------------------------------------------------------------------- log4j-samples/configuration/pom.xml | 55 ++++++++++++++++ .../configuration/CustomConfiguration.java | 67 ++++++++++++++++++++ .../CustomConfigurationFactory.java | 47 ++++++++++++++ .../configuration/CustomConfigurationTest.java | 17 +++++ log4j-samples/pom.xml | 1 + 5 files changed, 187 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7c8dffc3/log4j-samples/configuration/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-samples/configuration/pom.xml b/log4j-samples/configuration/pom.xml new file mode 100644 index 0000000..8a6761a --- /dev/null +++ b/log4j-samples/configuration/pom.xml @@ -0,0 +1,55 @@ +<?xml version="1.0"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <artifactId>log4j-samples</artifactId> + <groupId>org.apache.logging.log4j.samples</groupId> + <version>2.4-SNAPSHOT</version> + </parent> + <artifactId>log4j-samples-configuration</artifactId> + <packaging>jar</packaging> + <name>Apache Log4j Samples: Configuration</name> + <url>http://maven.apache.org</url> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7c8dffc3/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfiguration.java ---------------------------------------------------------------------- diff --git a/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfiguration.java b/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfiguration.java new file mode 100644 index 0000000..9011a8e --- /dev/null +++ b/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfiguration.java @@ -0,0 +1,67 @@ +package org.apache.logging.log4j.configuration; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Layout; +import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.config.AbstractConfiguration; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.layout.PatternLayout; +import org.apache.logging.log4j.util.PropertiesUtil; + +import java.io.Serializable; + +/** + * This Configuration is the same as the DefaultConfiguration but shows how a custom configuration can be built + * programmatically + */ +public class CustomConfiguration extends AbstractConfiguration { + + private static final long serialVersionUID = 1L; + + /** + * The name of the default configuration. + */ + public static final String CONFIG_NAME = "Custom"; + + /** + * The System Property used to specify the logging level. + */ + public static final String DEFAULT_LEVEL = "org.apache.logging.log4j.level"; + /** + * The default Pattern used for the default Layout. + */ + public static final String DEFAULT_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"; + + public CustomConfiguration() { + this(ConfigurationSource.NULL_SOURCE); + } + + /** + * Constructor to create the default configuration. + */ + public CustomConfiguration(ConfigurationSource source) { + super(source); + + setName(CONFIG_NAME); + final Layout<? extends Serializable> layout = PatternLayout.newBuilder() + .withPattern(DEFAULT_PATTERN) + .withConfiguration(this) + .build(); + final Appender appender = ConsoleAppender.createDefaultAppenderForLayout(layout); + appender.start(); + addAppender(appender); + final LoggerConfig root = getRootLogger(); + root.addAppender(appender, null, null); + + final String levelName = PropertiesUtil.getProperties().getStringProperty(DEFAULT_LEVEL); + final Level level = levelName != null && Level.valueOf(levelName) != null ? + Level.valueOf(levelName) : Level.ERROR; + root.setLevel(level); + } + + @Override + protected void doConfigure() { + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7c8dffc3/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfigurationFactory.java ---------------------------------------------------------------------- diff --git a/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfigurationFactory.java b/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfigurationFactory.java new file mode 100644 index 0000000..36d54d3 --- /dev/null +++ b/log4j-samples/configuration/src/main/java/org/apache/logging/log4j/configuration/CustomConfigurationFactory.java @@ -0,0 +1,47 @@ +package org.apache.logging.log4j.configuration; + +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.Order; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.xml.XmlConfiguration; + +import java.net.URI; + +/** + * Factory to construct a CustomConfiguration. + */ +@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY) +@Order(50) +public class CustomConfigurationFactory extends ConfigurationFactory { + + /** + * Valid file extensions for XML files. + */ + public static final String[] SUFFIXES = new String[] {"*"}; + + /** + * Returns the Configuration. + * @param source The InputSource. + * @return The Configuration. + */ + @Override + public Configuration getConfiguration(final ConfigurationSource source) { + return new CustomConfiguration(source); + } + + @Override + public Configuration getConfiguration(final String name, final URI configLocation) { + return new CustomConfiguration(); + } + + /** + * Returns the file suffixes for XML files. + * @return An array of File extensions. + */ + @Override + public String[] getSupportedTypes() { + return SUFFIXES; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7c8dffc3/log4j-samples/configuration/src/test/java/org/apache/logging/log4j/configuration/CustomConfigurationTest.java ---------------------------------------------------------------------- diff --git a/log4j-samples/configuration/src/test/java/org/apache/logging/log4j/configuration/CustomConfigurationTest.java b/log4j-samples/configuration/src/test/java/org/apache/logging/log4j/configuration/CustomConfigurationTest.java new file mode 100644 index 0000000..e63d7f7 --- /dev/null +++ b/log4j-samples/configuration/src/test/java/org/apache/logging/log4j/configuration/CustomConfigurationTest.java @@ -0,0 +1,17 @@ +package org.apache.logging.log4j.configuration; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Test; + +/** + * + */ +public class CustomConfigurationTest { + private Logger logger = LogManager.getLogger(CustomConfiguration.class); + + @Test + public void testLogging() { + logger.error("This is a test"); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7c8dffc3/log4j-samples/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-samples/pom.xml b/log4j-samples/pom.xml index c9d753a..a70fc08 100644 --- a/log4j-samples/pom.xml +++ b/log4j-samples/pom.xml @@ -81,6 +81,7 @@ <module>flume-common</module> <module>flume-remote</module> <module>flume-embedded</module> + <module>configuration</module> </modules> <build> <plugins>
