    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements. See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache license, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License. You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the license for the specific language governing permissions and
    015     * limitations under the license.
    016     */
    017    package org.apache.log4j;
    018
    019    import java.util.Enumeration;
    020
    021    import org.apache.log4j.helpers.NullEnumeration;
    022    import org.apache.log4j.spi.HierarchyEventListener;
    023    import org.apache.log4j.spi.LoggerFactory;
    024    import org.apache.log4j.spi.LoggerRepository;
    025    import org.apache.log4j.spi.RepositorySelector;
    026    import org.apache.logging.log4j.core.LoggerContext;
    027    import org.apache.logging.log4j.util.Strings;
    028
    029    /**
    030     *
    031     */
    032    public final class LogManager {
    033
    034        /**
    035         * @deprecated This variable is for internal use only. It will
    036         * become package protected in future versions.
    037         * */
    038        @Deprecated
    039        public static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
    040
    041        /**
    042         * @deprecated This variable is for internal use only. It will
    043         * become private in future versions.
    044         * */
    045        @Deprecated
    046        public static final String DEFAULT_CONFIGURATION_KEY = "log4j.configuration";
    047
    048        /**
    049         * @deprecated This variable is for internal use only. It will
    050         * become private in future versions.
    051         * */
    052        @Deprecated
    053        public static final String CONFIGURATOR_CLASS_KEY = "log4j.configuratorClass";
    054
    055        /**
    056         * @deprecated This variable is for internal use only. It will
    057         * become private in future versions.
    058         */
    059        @Deprecated
    060        public static final String DEFAULT_INIT_OVERRIDE_KEY = "log4j.defaultInitOverride";
    061
    062        static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";
    063
    064        private static final LoggerRepository REPOSITORY = new Repository();
    065
    066        private LogManager() {
    067        }
    068
    069        public static Logger getRootLogger() {
    070            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), Strings.EMPTY);
    071        }
    072
    073        public static Logger getLogger(final String name) {
    074            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
    075        }
    076
    077        public static Logger getLogger(@SuppressWarnings("rawtypes") final Class clazz) {
    078            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), clazz.getName());
    079        }
    080
    081        public static Logger getLogger(final String name, final LoggerFactory factory) {
    082            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
    083        }
    084
    085        public static Logger exists(final String name) {
    086            final LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
    087            if (!ctx.hasLogger(name)) {
    088                return null;
    089            }
    090            return Logger.getLogger(name);
    091        }
    092
    093        @SuppressWarnings("rawtypes")
    094        public static Enumeration getCurrentLoggers() {
    095            return NullEnumeration.getInstance();
    096        }
    097
    098        static void reconfigure() {
    099            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    }