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 <[email protected]> Authored: Mon Sep 1 20:13:38 2014 -0500 Committer: Matt Sicker <[email protected]> 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); + } + } + +}
