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/02 03:14:21 UTC
[1/8] git commit: Add utility method to ListAppender.
Repository: logging-log4j2
Updated Branches:
refs/heads/LOG4J2-608 c3d663694 -> 57008b9f2
Add utility method to ListAppender.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/1b3e553d
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/1b3e553d
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/1b3e553d
Branch: refs/heads/LOG4J2-608
Commit: 1b3e553d9b109e5b6ef81ada6de9020c60e47190
Parents: c3d6636
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 18:22:20 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 18:22:20 2014 -0500
----------------------------------------------------------------------
.../logging/log4j/test/appender/ListAppender.java | 13 +++++++++++++
1 file changed, 13 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/1b3e553d/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 3c46c4e..0885aff 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
@@ -21,9 +21,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
@@ -177,4 +179,15 @@ public class ListAppender extends AbstractAppender {
return new ListAppender(name, filter, layout, nl, r);
}
+
+ /**
+ * Gets the named ListAppender if it has been registered.
+ *
+ * @param name the name of the ListAppender
+ * @return the named ListAppender or {@code null} if it does not exist
+ * @see org.apache.logging.log4j.junit.InitialLoggerContext#getListAppender(String)
+ */
+ public static ListAppender getListAppender(final String name) {
+ return ((ListAppender) ((LoggerContext) LogManager.getContext(false)).getConfiguration().getAppender(name));
+ }
}
[2/8] git commit: Update poms to add log4j-jdk module.
Posted by ma...@apache.org.
Update poms to add log4j-jdk module.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d1cb742a
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d1cb742a
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d1cb742a
Branch: refs/heads/LOG4J2-608
Commit: d1cb742a592d757cfaa5527033cb92f892807935
Parents: 1b3e553
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 18:22:51 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 18:22:51 2014 -0500
----------------------------------------------------------------------
log4j-jdk/pom.xml | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++
pom.xml | 1 +
2 files changed, 75 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1cb742a/log4j-jdk/pom.xml
----------------------------------------------------------------------
diff --git a/log4j-jdk/pom.xml b/log4j-jdk/pom.xml
new file mode 100644
index 0000000..0d978f8
--- /dev/null
+++ b/log4j-jdk/pom.xml
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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">
+ <parent>
+ <artifactId>log4j</artifactId>
+ <groupId>org.apache.logging.log4j</groupId>
+ <version>2.1-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>log4j-jdk</artifactId>
+ <name>Log4j JUL Adaptor</name>
+ <description>Log4j implementation of java.util.logging</description>
+
+ <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>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-core</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-all</artifactId>
+ <scope>test</scope>
+ </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>
+ <configuration>
+ <instructions>
+ <Export-Package>org.apache.logging.log4j.jdk</Export-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1cb742a/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index e12f256..4a89d2a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1004,6 +1004,7 @@
<module>log4j-web</module>
<module>log4j-perf</module>
<module>log4j-streams</module>
+ <module>log4j-jdk</module>
</modules>
<profiles>
<profile>
[6/8] git commit: Add implementation of JUL classes.
Posted by ma...@apache.org.
Add implementation of JUL classes.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/fd65d6b4
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/fd65d6b4
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/fd65d6b4
Branch: refs/heads/LOG4J2-608
Commit: fd65d6b490390ab1ba8bca58ba889d73f8f96563
Parents: 220fb23
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 20:13:38 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 20:13:38 2014 -0500
----------------------------------------------------------------------
.../apache/logging/log4j/jdk/LogManager.java | 60 ++++++++
.../org/apache/logging/log4j/jdk/Logger.java | 138 +++++++++++++++++++
.../logging/log4j/jdk/LoggerRegistry.java | 48 +++++++
3 files changed, 246 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd65d6b4/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LogManager.java
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LogManager.java b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LogManager.java
new file mode 100644
index 0000000..adfe757
--- /dev/null
+++ b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LogManager.java
@@ -0,0 +1,60 @@
+/*
+ * 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.jdk;
+
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.logging.Logger;
+
+import org.apache.logging.log4j.spi.ExternalLoggerContextRegistry;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * Log4j implementation of {@link java.util.logging.LogManager}. Note that the system property
+ * {@code java.util.logging.manager} must be set to {@code org.apache.logging.log4j.jdk.LogManager} in order to use
+ * this adaptor. This LogManager requires the {@code log4j-core} library to be available as well as {@code log4j-api}.
+ */
+public class LogManager extends java.util.logging.LogManager {
+
+ private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
+
+ private final ExternalLoggerContextRegistry<Logger> registry = new LoggerRegistry();
+
+ public LogManager() {
+ super();
+ LOGGER.info("Registered Log4j as the java.util.logging.LogManager.");
+ }
+
+ @Override
+ public boolean addLogger(final Logger logger) {
+ // in order to prevent non-bridged loggers from being registered, we always return false to indicate that
+ // the named logger should be obtained through getLogger(name)
+ return false;
+ }
+
+ @Override
+ public Logger getLogger(final String name) {
+ LOGGER.trace("Call to LogManager.getLogger({})", name);
+ return registry.getLogger(name);
+ }
+
+ @Override
+ public Enumeration<String> getLoggerNames() {
+ return Collections.enumeration(registry.getLoggersInContext(registry.getContext()).keySet());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd65d6b4/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Logger.java
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Logger.java b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Logger.java
new file mode 100644
index 0000000..7c7e337
--- /dev/null
+++ b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Logger.java
@@ -0,0 +1,138 @@
+/*
+ * 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.jdk;
+
+import java.util.logging.Filter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.util.Assert;
+import org.apache.logging.log4j.message.Message;
+
+/**
+ * Log4j implementation of the JUL {@link java.util.logging.Logger} class. <strong>Note that this implementation does
+ * <em>not</em> use the {@link java.util.logging.Handler} class.</strong> Instead, logging is delegated to the
+ * underlying Log4j {@link org.apache.logging.log4j.core.Logger} which uses
+ * {@link org.apache.logging.log4j.core.Appender Appenders} instead.
+ */
+public class Logger extends java.util.logging.Logger {
+
+ private static final String FQCN = java.util.logging.Logger.class.getName();
+
+ private static final String PREFIX = "log4j.jul.";
+
+ /**
+ * The {@link ThreadContext} key where the value of {@link LogRecord#getThreadID()} will be stored.
+ */
+ public static final String THREAD_ID = PREFIX + "threadID";
+
+ /**
+ * The {@link ThreadContext} key where the value of {@link LogRecord#getSequenceNumber()} will be stored.
+ */
+ public static final String SEQUENCE_NUMBER = PREFIX + "sequenceNumber";
+
+ /**
+ * The {@link ThreadContext} key where the name of the {@link Level} will be stored. This is particularly useful
+ * for custom Level implementations as well as for obtaining the exact Level that was used rather than the
+ * equivalent Log4j {@link org.apache.logging.log4j.Level}.
+ */
+ public static final String LEVEL = PREFIX + "level";
+
+ private final org.apache.logging.log4j.core.Logger logger;
+
+ /**
+ * Constructs a Logger using a Log4j {@link org.apache.logging.log4j.core.Logger}.
+ *
+ * @param logger the underlying Logger to base this Logger on
+ */
+ Logger(final org.apache.logging.log4j.core.Logger logger) {
+ super(Assert.requireNonNull(logger, "No Logger provided").getName(), null);
+ super.setLevel(Levels.toJavaLevel(logger.getLevel()));
+ this.logger = logger;
+ }
+
+ @Override
+ public void log(final LogRecord record) {
+ if (isFiltered(record)) {
+ return;
+ }
+ ThreadContext.put(THREAD_ID, Integer.toString(record.getThreadID()));
+ ThreadContext.put(SEQUENCE_NUMBER, Long.toString(record.getSequenceNumber()));
+ ThreadContext.put(LEVEL, record.getLevel().getName());
+ final org.apache.logging.log4j.Level level = Levels.toLevel(record.getLevel());
+ final Message message = logger.getMessageFactory().newMessage(record.getMessage(), record.getParameters());
+ final Throwable thrown = record.getThrown();
+ // TODO: may want to use LoggerConfig.log(LogEvent) with a LogRecord/LogEvent hybrid
+ logger.logIfEnabled(FQCN, level, null, message, thrown);
+ // TODO: support handlers?
+ ThreadContext.remove(THREAD_ID);
+ ThreadContext.remove(SEQUENCE_NUMBER);
+ ThreadContext.remove(LEVEL);
+ }
+
+ // support for Logger.getFilter()/Logger.setFilter()
+ private boolean isFiltered(final LogRecord logRecord) {
+ final Filter filter = getFilter();
+ return filter != null && !filter.isLoggable(logRecord);
+ }
+
+ @Override
+ public void setLevel(final Level level) throws SecurityException {
+ logger.setLevel(Levels.toLevel(level));
+ super.setLevel(level);
+ }
+
+ @Override
+ public boolean isLoggable(final Level level) {
+ return logger.isEnabled(Levels.toLevel(level));
+ }
+
+ @Override
+ public String getName() {
+ return logger.getName();
+ }
+
+ /**
+ * Marks the underlying {@link org.apache.logging.log4j.core.Logger} as additive.
+ *
+ * @param additive {@code true} if this Logger should be additive
+ * @see org.apache.logging.log4j.core.Logger#setAdditive(boolean)
+ */
+ @Override
+ public synchronized void setUseParentHandlers(final boolean additive) {
+ logger.setAdditive(additive);
+ }
+
+ /**
+ * Indicates if the underlying {@link org.apache.logging.log4j.core.Logger} is additive. <strong>Note that the
+ * Log4j version of JDK Loggers do <em>not</em> use Handlers.</strong>
+ *
+ * @return {@code true} if this Logger is additive, or {@code false} otherwise
+ * @see org.apache.logging.log4j.core.Logger#isAdditive()
+ */
+ @Override
+ public synchronized boolean getUseParentHandlers() {
+ return logger.isAdditive();
+ }
+
+ @Override
+ public java.util.logging.Logger getParent() {
+ final org.apache.logging.log4j.core.Logger parent = logger.getParent();
+ return parent == null ? null : java.util.logging.Logger.getLogger(parent.getName());
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fd65d6b4/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LoggerRegistry.java
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LoggerRegistry.java b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LoggerRegistry.java
new file mode 100644
index 0000000..cc821a4
--- /dev/null
+++ b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/LoggerRegistry.java
@@ -0,0 +1,48 @@
+/*
+ * 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.jdk;
+
+import java.util.logging.Logger;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.spi.AbstractExternalLoggerContextRegistry;
+import org.apache.logging.log4j.spi.LoggerContext;
+
+/**
+ * {@link Logger} registry implementation.
+ */
+public class LoggerRegistry extends AbstractExternalLoggerContextRegistry<Logger> {
+
+ @Override
+ public Logger newLogger(final String name, final LoggerContext context) {
+ return new org.apache.logging.log4j.jdk.Logger((org.apache.logging.log4j.core.Logger) context.getLogger(name));
+ }
+
+ @Override
+ public LoggerContext getContext() {
+ return PrivateManager.getContext();
+ }
+
+ private static class PrivateManager extends LogManager {
+ private static final String FQCN = java.util.logging.LogManager.class.getName();
+
+ public static LoggerContext getContext() {
+ return getContext(FQCN, false);
+ }
+ }
+
+}
[7/8] git commit: Add unit test for JUL adaptor.
Posted by ma...@apache.org.
Add unit test for JUL adaptor.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/18015e93
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/18015e93
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/18015e93
Branch: refs/heads/LOG4J2-608
Commit: 18015e93490de6102430904b2d5274052685996f
Parents: fd65d6b
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 20:13:53 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 20:13:53 2014 -0500
----------------------------------------------------------------------
.../apache/logging/log4j/jdk/LoggerTest.java | 101 +++++++++++++++++++
log4j-jdk/src/test/resources/log4j2-test.xml | 39 +++++++
2 files changed, 140 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/18015e93/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java
new file mode 100644
index 0000000..007e3a6
--- /dev/null
+++ b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java
@@ -0,0 +1,101 @@
+package org.apache.logging.log4j.jdk;
+
+import java.util.List;
+import java.util.logging.Logger;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.junit.Assert.*;
+
+public class LoggerTest {
+
+ public static final String LOGGER_NAME = "Test";
+ private Logger logger;
+ private ListAppender eventAppender;
+ private ListAppender stringAppender;
+
+ @BeforeClass
+ public static void setUpClass() {
+ System.setProperty("java.util.logging.manager", LogManager.class.getName());
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ logger = Logger.getLogger(LOGGER_NAME);
+ assertThat(logger.getLevel(), equalTo(java.util.logging.Level.FINE));
+ eventAppender = ListAppender.getListAppender("TestAppender");
+ stringAppender = ListAppender.getListAppender("StringAppender");
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ eventAppender.clear();
+ }
+
+ @Test
+ public void testLog() throws Exception {
+ logger.info("Informative message here.");
+ final List<LogEvent> events = eventAppender.getEvents();
+ assertThat(events, hasSize(1));
+ final LogEvent event = events.get(0);
+ assertThat(event, instanceOf(Log4jLogEvent.class));
+ assertEquals(Level.INFO, event.getLevel());
+ assertEquals(LOGGER_NAME, event.getLoggerName());
+ assertEquals("Informative message here.", event.getMessage().getFormattedMessage());
+ assertEquals(Logger.class.getName(), event.getLoggerFqcn());
+ }
+
+ @Test
+ public void testLogWithCallingClass() throws Exception {
+ final Logger log = Logger.getLogger("Test.CallerClass");
+ log.config("Calling from LoggerTest");
+ final List<String> messages = stringAppender.getMessages();
+ assertThat(messages, hasSize(1));
+ final String message = messages.get(0);
+ assertEquals(getClass().getName(), message);
+ }
+
+ @Test
+ public void testLogUsingCustomLevel() throws Exception {
+ logger.log(CustomJdkLevel.TEST, "Test level");
+ final List<LogEvent> events = eventAppender.getEvents();
+ assertThat(events, hasSize(1));
+ final LogEvent event = events.get(0);
+ assertThat(event.getLevel(), equalTo(Level.INFO));
+ final String levelName = event.getContextMap().get(org.apache.logging.log4j.jdk.Logger.LEVEL);
+ assertThat(levelName, equalTo(CustomJdkLevel.TEST.getName()));
+ }
+
+ @Test
+ public void testSetLevel() throws Exception {
+ logger.setLevel(java.util.logging.Level.SEVERE);
+ assertThat(logger.getLevel(), equalTo(java.util.logging.Level.SEVERE));
+ }
+
+ @Test
+ public void testIsLoggable() throws Exception {
+ assertThat(logger.isLoggable(java.util.logging.Level.SEVERE), equalTo(true));
+ assertThat(logger.isLoggable(CustomJdkLevel.DEFCON_1), equalTo(true));
+ }
+
+ @Test
+ public void testGetName() throws Exception {
+ assertThat(logger.getName(), equalTo(LOGGER_NAME));
+ }
+
+ @Test
+ public void testGlobalLoggerName() throws Exception {
+ final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
+ assertThat(root.getName(), equalTo(Logger.GLOBAL_LOGGER_NAME));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/18015e93/log4j-jdk/src/test/resources/log4j2-test.xml
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/test/resources/log4j2-test.xml b/log4j-jdk/src/test/resources/log4j2-test.xml
new file mode 100644
index 0000000..af6a050
--- /dev/null
+++ b/log4j-jdk/src/test/resources/log4j2-test.xml
@@ -0,0 +1,39 @@
+<!--
+ ~ 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.
+ -->
+
+<Configuration name="LoggerTest" status="DEBUG">
+ <Appenders>
+ <List name="TestAppender"/>
+ <List name="StringAppender">
+ <PatternLayout pattern="%class"/>
+ </List>
+ <Console name="Console" target="SYSTEM_ERR">
+ <BasicLayout/>
+ </Console>
+ </Appenders>
+ <Loggers>
+ <Logger name="Test" level="DEBUG" additivity="false">
+ <AppenderRef ref="TestAppender"/>
+ </Logger>
+ <Logger name="Test.CallerClass" level="DEBUG" additivity="false">
+ <AppenderRef ref="StringAppender"/>
+ </Logger>
+ <Root level="ERROR">
+ <AppenderRef ref="Console"/>
+ </Root>
+ </Loggers>
+</Configuration>
[8/8] git commit: Add documentation for log4j-jdk module.
Posted by ma...@apache.org.
Add documentation for log4j-jdk module.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/57008b9f
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/57008b9f
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/57008b9f
Branch: refs/heads/LOG4J2-608
Commit: 57008b9f2349beaad8aea3767b86d710dcbfbb35
Parents: 18015e9
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 20:14:09 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 20:14:09 2014 -0500
----------------------------------------------------------------------
log4j-jdk/src/site/site.xml | 52 ++++++++++
log4j-jdk/src/site/xdoc/index.xml | 168 +++++++++++++++++++++++++++++++++
2 files changed, 220 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/57008b9f/log4j-jdk/src/site/site.xml
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/site/site.xml b/log4j-jdk/src/site/site.xml
new file mode 100644
index 0000000..f8c7307
--- /dev/null
+++ b/log4j-jdk/src/site/site.xml
@@ -0,0 +1,52 @@
+<!--
+ 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 name="Log4j JDK Logging Adaptor"
+ xmlns="http://maven.apache.org/DECORATION/1.4.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/DECORATION/1.4.0 http://maven.apache.org/xsd/decoration-1.4.0.xsd">
+ <body>
+ <links>
+ <item name="Apache" href="http://www.apache.org/" />
+ <item name="Logging Services" href="http://logging.apache.org/"/>
+ <item name="Log4j" href="../index.html"/>
+ </links>
+
+ <!-- Component-specific reports -->
+ <menu ref="reports"/>
+
+ <!-- Overall Project Info -->
+ <menu name="Log4j Project Information" img="icon-info-sign">
+ <item name="Dependencies" href="../dependencies.html" />
+ <item name="Dependency Convergence" href="../dependency-convergence.html" />
+ <item name="Dependency Management" href="../dependency-management.html" />
+ <item name="Project Team" href="../team-list.html" />
+ <item name="Mailing Lists" href="../mail-lists.html" />
+ <item name="Issue Tracking" href="../issue-tracking.html" />
+ <item name="Project License" href="../license.html" />
+ <item name="Source Repository" href="../source-repository.html" />
+ <item name="Project Summary" href="../project-summary.html" />
+ </menu>
+
+ <menu name="Log4j Project Reports" img="icon-cog">
+ <item name="Changes Report" href="../changes-report.html" />
+ <item name="JIRA Report" href="../jira-report.html" />
+ <item name="Surefire Report" href="../surefire-report.html" />
+ <item name="RAT Report" href="../rat-report.html" />
+ </menu>
+ </body>
+</project>
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/57008b9f/log4j-jdk/src/site/xdoc/index.xml
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/site/xdoc/index.xml b/log4j-jdk/src/site/xdoc/index.xml
new file mode 100644
index 0000000..0b21372
--- /dev/null
+++ b/log4j-jdk/src/site/xdoc/index.xml
@@ -0,0 +1,168 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+
+<!DOCTYPE document [<!ENTITY le "≤">]>
+<document xmlns="http://maven.apache.org/XDOC/2.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">
+ <properties>
+ <title>Log4j JDK Logging Adaptor</title>
+ <author email="mattsicker@apache.org">Matt Sicker</author>
+ </properties>
+ <body>
+ <section name="JDK Logging Adaptor">
+ <p>
+ The JDK Logging Adaptor is a custom implementation of
+ <a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/LogManager.html">java.util.logging.LogManager</a>
+ that uses <a href="../log4j-core/index.html">Log4j</a>.
+ </p>
+ </section>
+ <section name="Requirements">
+ <p>
+ The JDK Logging Adaptor requires at least Java 6 and is dependent on the Log4j API and Log4j Core.
+ </p>
+ </section>
+ <section name="Usage">
+ <p>
+ To use the JDK Logging Adaptor, you must set the system property <code>java.util.logging.manager</code> to
+ <a class="javadoc" href="apidocs/org/apache/logging/log4j/jdk/LogManager.html">org.apache.logging.log4j.jdk.LogManager</a>
+ </p>
+ <p>
+ This must be done either through the command line (i.e., using the
+ <code>-Djava.util.logging.manager=org.apache.logging.log4j.jdk.LogManager</code> argument) or by using
+ <code>System.setProperty()</code> before any calls are made to <code>LogManager</code> or <code>Logger</code>.
+ </p>
+ </section>
+ <section name="Compatibility">
+ <p>
+ The use of a
+ <a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Filter.html">java.util.logging.Filter</a>
+ is supported on a per-<a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html">Logger</a>
+ basis. However, it is recommended to use the standard <a href="../manual/filters.html">Filters</a> feature in
+ Log4j instead.
+ </p>
+ <p>
+ The use of
+ <a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Handler.html">java.util.logging.Handler</a>
+ classes is <em>NOT</em> supported. Custom Handlers should instead use an appropriate
+ <a href="../manual/appenders.html">Appender</a> or code their own
+ <a class="javadoc" href="../log4j-core/apidocs/org/apache/logging/log4j/core/Appender.html">Appender</a>
+ plugin.
+ </p>
+ <p>
+ Java logging levels are translated into Log4j logging levels dynamically. The following table lists the
+ conversions between a Java logging level and its equivalent Log4j level.
+ </p>
+ <table>
+ <caption>Level conversions</caption>
+ <thead>
+ <tr>
+ <th>Java Level</th>
+ <th>Level Range</th>
+ <th>Log4j Level</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#OFF">OFF</a></td>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</a></td>
+ <td>OFF</td>
+ </tr>
+ <tr>
+ <td class="muted">n/a</td>
+ <td>1000 < <var>level</var> < <a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</a></td>
+ <td>FATAL</td>
+ </tr>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#SEVERE">SEVERE</a></td>
+ <td>900 < <var>level</var> ≤ 1000</td>
+ <td>ERROR</td>
+ </tr>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#WARNING">WARNING</a></td>
+ <td>800 < <var>level</var> ≤ 900</td>
+ <td>WARN</td>
+ </tr>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#INFO">INFO</a></td>
+ <td>700 < <var>level</var> ≤ 800</td>
+ <td>INFO</td>
+ </tr>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#CONFIG">CONFIG</a></td>
+ <td>500 < <var>level</var> ≤ 700</td>
+ <td>INFO</td>
+ </tr>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#FINE">FINE</a></td>
+ <td>400 < <var>level</var> ≤ 500</td>
+ <td>DEBUG</td>
+ </tr>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#FINER">FINER</a></td>
+ <td>300 < <var>level</var> ≤ 400</td>
+ <td>DEBUG</td>
+ </tr>
+ <tr>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/Level.html#FINEST">FINEST</a></td>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#MIN_VALUE">Integer.MIN_VALUE</a> < <var>level</var> ≤ 300</td>
+ <td>TRACE</td>
+ </tr>
+ <tr>
+ <td>ALL</td>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#MIN_VALUE">Integer.MIN_VALUE</a></td>
+ <td>ALL</td>
+ </tr>
+ </tbody>
+ </table>
+ <p>
+ There are currently three additional pieces of information obtained from each
+ <a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/LogRecord.html">LogRecord</a>
+ that are stored in the <a href="../manual/thread-context.html">ThreadContext</a>.
+ </p>
+ <table>
+ <caption>ThreadContext map keys</caption>
+ <thead>
+ <tr>
+ <th>Key</th>
+ <th><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/LogRecord.html">LogRecord</a> Property</th>
+ <th>Description</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>log4j.jul.threadID</td>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/LogRecord.html#getThreadID%28%29">threadID</a></td>
+ <td>An identifier for the thread where the message originated.</td>
+ </tr>
+ <tr>
+ <td>log4j.jul.sequenceNumber</td>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/LogRecord.html#getSequenceNumber%28%29">sequenceNumber</a></td>
+ <td>A unique, increasing sequence number generated by the LogRecord constructor.</td>
+ </tr>
+ <tr>
+ <td>log4j.jul.level</td>
+ <td><a class="javadoc" href="http://docs.oracle.com/javase/6/docs/api/java/util/logging/LogRecord.html#getLevel%28%29">level</a></td>
+ <td>The logging message level name. This level is translated into an equivalent Log4j level, so the original
+ logging level name is saved here.</td>
+ </tr>
+ </tbody>
+ </table>
+ </section>
+ </body>
+</document>
\ No newline at end of file
[3/8] git commit: Add custom jul.Level for unit tests.
Posted by ma...@apache.org.
Add custom jul.Level for unit tests.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/25399022
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/25399022
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/25399022
Branch: refs/heads/LOG4J2-608
Commit: 253990222665d78b9b7f9568a26cb609138fcb38
Parents: d1cb742
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 18:24:39 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 18:24:39 2014 -0500
----------------------------------------------------------------------
.../logging/log4j/jdk/CustomJdkLevel.java | 40 ++++++++++++++++++++
1 file changed, 40 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/25399022/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java
new file mode 100644
index 0000000..d125d0e
--- /dev/null
+++ b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java
@@ -0,0 +1,40 @@
+/*
+ * 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.jdk;
+
+import java.util.logging.Level;
+
+/**
+ * Custom JUL Level for unit tests.
+ */
+public class CustomJdkLevel extends Level {
+
+ private static final long serialVersionUID = 4681718777617726164L;
+
+ protected CustomJdkLevel(final String name, final int value) {
+ super(name, value);
+ }
+
+ // inside CONFIG range; should map to INFO
+ public static final Level TEST = new CustomJdkLevel("TEST", 600);
+
+ // just 1 below Level.SEVERE; should map to ERROR
+ public static final Level DEFCON_2 = new CustomJdkLevel("DEFCON_2", 999);
+
+ // above Level.SEVERE; should map to FATAL
+ public static final Level DEFCON_1 = new CustomJdkLevel("DEFCON_1", 10000);
+}
[5/8] git commit: Add "javadoc" class for elements on site.
Posted by ma...@apache.org.
Add "javadoc" class for <a/> elements on site.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/220fb239
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/220fb239
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/220fb239
Branch: refs/heads/LOG4J2-608
Commit: 220fb23919f3d5296f6297bfc81eebb5c9f049d0
Parents: d690e7a
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 19:15:27 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 19:20:53 2014 -0500
----------------------------------------------------------------------
src/site/resources/css/site.css | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/220fb239/src/site/resources/css/site.css
----------------------------------------------------------------------
diff --git a/src/site/resources/css/site.css b/src/site/resources/css/site.css
index f31fc2a..fc5a069 100644
--- a/src/site/resources/css/site.css
+++ b/src/site/resources/css/site.css
@@ -21,7 +21,7 @@ div.clear hr { display: none; }
/* Tweaks to the bootstrap theme
--------------------------------- */
li { line-height: 20px; }
-tt { font-family: Menlo, Monaco, "Courier New", monospace; font-size: 12px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; padding: 3px 4px; color: #d14; background-color: #f7f7f9; border: 1px solid #e1e1e8; }
+tt { font: 12px Menlo, Monaco, "Courier New", monospace; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; padding: 3px 4px; color: #d14; background-color: #f7f7f9; border: 1px solid #e1e1e8; }
dt { margin: 15px 0 5px 0; font-size: 1.2em }
.big-red { font-weight: bold; color: #D14 }
@@ -84,3 +84,11 @@ dt { margin: 15px 0 5px 0; font-size: 1.2em }
}
ol.linenums { margin: 0 0 0 33px; }
ol.linenums li { padding-left: 12px; color: #bebec5; line-height: 18px; text-shadow: 0 1px 0 #fff; }
+
+/* Additional styles.
+-----------------------*/
+
+/* fixed width font links (e.g., to a JavaDoc page) */
+a.javadoc, a.javadoc:hover {
+ font: 12px Menlo, Monaco, "Courier New", monospace;
+}
[4/8] git commit: Add utility class to convert levels.
Posted by ma...@apache.org.
Add utility class to convert levels.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d690e7ab
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d690e7ab
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d690e7ab
Branch: refs/heads/LOG4J2-608
Commit: d690e7ab8d16e3ba87ee8121850c30634b36a589
Parents: 2539902
Author: Matt Sicker <ma...@apache.org>
Authored: Mon Sep 1 18:25:33 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Mon Sep 1 18:25:33 2014 -0500
----------------------------------------------------------------------
.../org/apache/logging/log4j/jdk/Levels.java | 109 +++++++++++++++++++
.../apache/logging/log4j/jdk/LevelsTest.java | 49 +++++++++
2 files changed, 158 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d690e7ab/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Levels.java
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Levels.java b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Levels.java
new file mode 100644
index 0000000..892dd99
--- /dev/null
+++ b/log4j-jdk/src/main/java/org/apache/logging/log4j/jdk/Levels.java
@@ -0,0 +1,109 @@
+/*
+ * 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.jdk;
+
+import org.apache.logging.log4j.Level;
+
+/**
+ * Utility class to convert between JDK Levels and Log4j 2 Levels.
+ */
+public final class Levels {
+
+ private static final int JDK_OFF = java.util.logging.Level.OFF.intValue(); // OFF
+ private static final int JDK_SEVERE = java.util.logging.Level.SEVERE.intValue(); // ERROR
+ private static final int JDK_WARNING = java.util.logging.Level.WARNING.intValue(); // WARN
+ private static final int JDK_INFO = java.util.logging.Level.INFO.intValue(); // INFO
+ private static final int JDK_CONFIG = java.util.logging.Level.CONFIG.intValue(); // INFO
+ private static final int JDK_FINE = java.util.logging.Level.FINE.intValue(); // DEBUG
+ private static final int JDK_FINER = java.util.logging.Level.FINER.intValue(); // DEBUG
+ private static final int JDK_FINEST = java.util.logging.Level.FINEST.intValue(); // TRACE
+ private static final int JDK_ALL = java.util.logging.Level.ALL.intValue(); // ALL
+
+ /**
+ * Converts a JDK logging Level to a Log4j logging Level.
+ *
+ * @param level JDK Level to convert.
+ * @return converted Level.
+ */
+ public static Level toLevel(final java.util.logging.Level level) {
+ final int value = level.intValue();
+ if (value == JDK_OFF) { // Integer.MAX_VALUE
+ return Level.OFF;
+ }
+ if (value == JDK_ALL) { // Integer.MIN_VALUE
+ return Level.ALL;
+ }
+ if (value <= JDK_FINEST) { // up to 300
+ return Level.TRACE;
+ }
+ if (value <= JDK_FINER) { // 301 to 400
+ return Level.DEBUG;
+ }
+ if (value <= JDK_FINE) { // 401 to 500
+ return Level.DEBUG;
+ }
+ if (value <= JDK_CONFIG) { // 501 to 700
+ return Level.INFO;
+ }
+ if (value <= JDK_INFO) { // 701 to 800
+ return Level.INFO;
+ }
+ if (value <= JDK_WARNING) { // 801 to 900
+ return Level.WARN;
+ }
+ if (value <= JDK_SEVERE) { // 901 to 1000
+ return Level.ERROR;
+ }
+ // 1001+
+ return Level.FATAL;
+ }
+
+ /**
+ * Converts a Log4j logging Level to a JDK logging Level.
+ *
+ * @param level Log4j Level to convert.
+ * @return converted Level.
+ */
+ public static java.util.logging.Level toJavaLevel(final Level level) {
+ if (level == Level.OFF) {
+ return java.util.logging.Level.OFF;
+ }
+ if (level == Level.TRACE) {
+ return java.util.logging.Level.FINEST;
+ }
+ if (level == Level.DEBUG) {
+ return java.util.logging.Level.FINE;
+ }
+ if (level == Level.INFO) {
+ return java.util.logging.Level.INFO;
+ }
+ if (level == Level.WARN) {
+ return java.util.logging.Level.WARNING;
+ }
+ if (level == Level.ERROR || level == Level.FATAL) {
+ return java.util.logging.Level.SEVERE;
+ }
+ if (level == Level.ALL) {
+ return java.util.logging.Level.ALL;
+ }
+ return java.util.logging.Level.parse(level.name());
+ }
+
+ private Levels() {
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d690e7ab/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java
----------------------------------------------------------------------
diff --git a/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java
new file mode 100644
index 0000000..b4a6056
--- /dev/null
+++ b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java
@@ -0,0 +1,49 @@
+package org.apache.logging.log4j.jdk;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.logging.log4j.Level;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.junit.Assert.*;
+
+@RunWith(Parameterized.class)
+public class LevelsTest {
+
+ private final java.util.logging.Level level;
+ private final Level expectedLevel;
+
+ public LevelsTest(final java.util.logging.Level level, final Level expectedLevel) {
+ this.level = level;
+ this.expectedLevel = expectedLevel;
+ }
+
+ @Parameterized.Parameters
+ public static Collection<Object[]> data() {
+ return Arrays.asList(
+ new Object[][]{
+ {CustomJdkLevel.TEST, Level.INFO},
+ {CustomJdkLevel.DEFCON_2, Level.ERROR},
+ {CustomJdkLevel.DEFCON_1, Level.FATAL},
+ {java.util.logging.Level.OFF, Level.OFF},
+ {java.util.logging.Level.ALL, Level.ALL},
+ {java.util.logging.Level.SEVERE, Level.ERROR},
+ {java.util.logging.Level.WARNING, Level.WARN},
+ {java.util.logging.Level.INFO, Level.INFO},
+ {java.util.logging.Level.CONFIG, Level.INFO},
+ {java.util.logging.Level.FINE, Level.DEBUG},
+ {java.util.logging.Level.FINER, Level.DEBUG},
+ {java.util.logging.Level.FINEST, Level.TRACE}
+ }
+ );
+ }
+
+ @Test
+ public void testToLevel() throws Exception {
+ final Level actualLevel = Levels.toLevel(level);
+ assertEquals(expectedLevel, actualLevel);
+ }
+}
\ No newline at end of file