View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17  package org.apache.logging.log4j.jul;
    18
    19  import java.util.Collections;
    20  import java.util.Enumeration;
    21  import java.util.logging.Logger;
    22
    23  import org.apache.logging.log4j.LoggingException;
    24  import org.apache.logging.log4j.status.StatusLogger;
    25  import org.apache.logging.log4j.util.LoaderUtil;
    26  import org.apache.logging.log4j.util.PropertiesUtil;
    27
    28  /**
    29   * Log4j implementation of {@link java.util.logging.LogManager}. Note that the system property
    30   * {@code java.util.logging.manager} must be set to {@code org.apache.logging.log4j.jul.LogManager} in order to use
    31   * this adaptor. This LogManager requires the {@code log4j-api} library to be available. If {@code log4j-core} is
    32   * also available, then more features of {@link java.util.logging.Logger} are supported.
    33   *
    34   * <p>To override the default {@link AbstractLoggerAdapter} that is used, specify the Log4j property
    35   * {@code log4j.jul.LoggerAdapter} and set it to the fully qualified class name of a custom
    36   * implementation. All implementations must have a default constructor.</p>
    37   *
    38   * @since 2.1
    39   */
    40  public class LogManager extends java.util.logging.LogManager {
    41
    42      private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
    43      private final AbstractLoggerAdapter loggerAdapter;
    44
    45      public LogManager() {
    46          super();
    47          AbstractLoggerAdapter adapter = null;
    48          final String overrideAdaptorClassName =
    49              PropertiesUtil.getProperties().getStringProperty(Constants.LOGGER_ADAPTOR_PROPERTY);
    50          if (overrideAdaptorClassName != null) {
    51              try {
    52                  LOGGER.info("Trying to use LoggerAdaptor [{}] specified by Log4j property.", overrideAdaptorClassName);
    53                  adapter = LoaderUtil.newCheckedInstanceOf(overrideAdaptorClassName, AbstractLoggerAdapter.class);
    54              } catch (final Exception e) {
    55                  LOGGER.error("Specified LoggerAdapter [{}] is incompatible.", overrideAdaptorClassName, e);
    56              }
    57          }
    58          if (adapter == null) {
    59              // default adapter
    60              String adapterClassName;
    61              try {
    62                  // find out if log4j-core is available
    63                  LoaderUtil.loadClass(Constants.CORE_LOGGER_CLASS_NAME);
    64                  adapterClassName = Constants.CORE_LOGGER_ADAPTER_CLASS_NAME;
    65              } catch (final ClassNotFoundException ignored) {
    66                  adapterClassName = Constants.API_LOGGER_ADAPTER_CLASS_NAME;
    67              }
    68              LOGGER.debug("Attempting to use {}", adapterClassName);
    69              try {
    70                  adapter = LoaderUtil.newCheckedInstanceOf(adapterClassName, AbstractLoggerAdapter.class);
    71              } catch (final Exception e) {
    72                  throw LOGGER.throwing(new LoggingException(e));
    73              }
    74          }
    75          loggerAdapter = adapter;
    76          LOGGER.info("Registered Log4j as the java.util.logging.LogManager.");
    77      }
    78
    79      @Override
    80      public boolean addLogger(final Logger logger) {
    81          // in order to prevent non-bridged loggers from being registered, we always return false to indicate that
    82          // the named logger should be obtained through getLogger(name)
    83          return false;
    84      }
    85
    86      @Override
    87      public Logger getLogger(final String name) {
    88          LOGGER.trace("Call to LogManager.getLogger({})", name);
    89          return loggerAdapter.getLogger(name);
    90      }
    91
    92      @Override
    93      public Enumeration<String> getLoggerNames() {
    94          return Collections.enumeration(loggerAdapter.getLoggersInContext(loggerAdapter.getContext()).keySet());
    95      }
    96
    97  }