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.log4j;
    18
    19  import java.util.Enumeration;
    20
    21  import org.apache.log4j.helpers.NullEnumeration;
    22  import org.apache.log4j.spi.HierarchyEventListener;
    23  import org.apache.log4j.spi.LoggerFactory;
    24  import org.apache.log4j.spi.LoggerRepository;
    25  import org.apache.log4j.spi.RepositorySelector;
    26  import org.apache.logging.log4j.core.LoggerContext;
    27  import org.apache.logging.log4j.util.Strings;
    28
    29  /**
    30   *
    31   */
    32  public final class LogManager {
    33
    34      /**
    35       * @deprecated This variable is for internal use only. It will
    36       * become package protected in future versions.
    37       * */
    38      @Deprecated
    39      public static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
    40
    41      /**
    42       * @deprecated This variable is for internal use only. It will
    43       * become private in future versions.
    44       * */
    45      @Deprecated
    46      public static final String DEFAULT_CONFIGURATION_KEY = "log4j.configuration";
    47
    48      /**
    49       * @deprecated This variable is for internal use only. It will
    50       * become private in future versions.
    51       * */
    52      @Deprecated
    53      public static final String CONFIGURATOR_CLASS_KEY = "log4j.configuratorClass";
    54
    55      /**
    56       * @deprecated This variable is for internal use only. It will
    57       * become private in future versions.
    58       */
    59      @Deprecated
    60      public static final String DEFAULT_INIT_OVERRIDE_KEY = "log4j.defaultInitOverride";
    61
    62      static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";
    63
    64      private static final LoggerRepository REPOSITORY = new Repository();
    65
    66      private LogManager() {
    67      }
    68
    69      public static Logger getRootLogger() {
    70          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), Strings.EMPTY);
    71      }
    72
    73      public static Logger getLogger(final String name) {
    74          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
    75      }
    76
    77      public static Logger getLogger(@SuppressWarnings("rawtypes") final Class clazz) {
    78          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), clazz.getName());
    79      }
    80
    81      public static Logger getLogger(final String name, final LoggerFactory factory) {
    82          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
    83      }
    84
    85      public static Logger exists(final String name) {
    86          final LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
    87          if (!ctx.hasLogger(name)) {
    88              return null;
    89          }
    90          return Logger.getLogger(name);
    91      }
    92
    93      @SuppressWarnings("rawtypes")
    94      public static Enumeration getCurrentLoggers() {
    95          return NullEnumeration.getInstance();
    96      }
    97
    98      static void reconfigure() {
    99          final LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
    100         ctx.reconfigure();
    101     }
    102
    103     /**
    104      * No-op implementation.
    105      */
    106     public static void shutdown() {
    107     }
    108
    109     /**
    110      * No-op implementation.
    111      */
    112     public static void resetConfiguration() {
    113     }
    114
    115     /**
    116      * No-op implementation.
    117      * @param selector The RepositorySelector.
    118      * @param guard prevents calls at the incorrect time.
    119      * @throws IllegalArgumentException if a parameter is invalid.
    120      */
    121     public static void setRepositorySelector(final RepositorySelector selector, final Object guard)
    122         throws IllegalArgumentException {
    123     }
    124
    125     public static LoggerRepository getLoggerRepository() {
    126         return REPOSITORY;
    127     }
    128
    129     /**
    130      * The Repository.
    131      */
    132     private static class Repository implements LoggerRepository {
    133         @Override
    134         public void addHierarchyEventListener(final HierarchyEventListener listener) {
    135
    136         }
    137
    138         @Override
    139         public boolean isDisabled(final int level) {
    140             return false;
    141         }
    142
    143         @Override
    144         public void setThreshold(final Level level) {
    145
    146         }
    147
    148         @Override
    149         public void setThreshold(final String val) {
    150
    151         }
    152
    153         @Override
    154         public void emitNoAppenderWarning(final Category cat) {
    155
    156         }
    157
    158         @Override
    159         public Level getThreshold() {
    160             return Level.OFF;
    161         }
    162
    163         @Override
    164         public Logger getLogger(final String name) {
    165             return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
    166         }
    167
    168         @Override
    169         public Logger getLogger(final String name, final LoggerFactory factory) {
    170             return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
    171         }
    172
    173         @Override
    174         public Logger getRootLogger() {
    175             return (Logger) Category.getRoot((LoggerContext) PrivateManager.getContext());
    176         }
    177
    178         @Override
    179         public Logger exists(final String name) {
    180             return LogManager.exists(name);
    181         }
    182
    183         @Override
    184         public void shutdown() {
    185         }
    186
    187         @Override
    188         @SuppressWarnings("rawtypes")
    189         public Enumeration getCurrentLoggers() {
    190             return NullEnumeration.getInstance();
    191         }
    192
    193         @Override
    194         @SuppressWarnings("rawtypes")
    195         public Enumeration getCurrentCategories() {
    196             return NullEnumeration.getInstance();
    197         }
    198
    199         @Override
    200         public void fireAddAppenderEvent(final Category logger, final Appender appender) {
    201         }
    202
    203         @Override
    204         public void resetConfiguration() {
    205         }
    206     }
    207
    208     /**
    209      * Internal LogManager.
    210      */
    211     private static class PrivateManager extends org.apache.logging.log4j.LogManager {
    212         private static final String FQCN = LogManager.class.getName();
    213
    214
    215         public static org.apache.logging.log4j.spi.LoggerContext getContext() {
    216             return getContext(FQCN, false);
    217         }
    218
    219         public static org.apache.logging.log4j.Logger getLogger(final String name) {
    220             return getLogger(FQCN, name);
    221         }
    222     }
    223 }