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 org.apache.log4j.helpers.NullEnumeration;
020    import org.apache.log4j.spi.HierarchyEventListener;
021    import org.apache.log4j.spi.LoggerFactory;
022    import org.apache.log4j.spi.LoggerRepository;
023    import org.apache.log4j.spi.RepositorySelector;
024    import org.apache.logging.log4j.core.LoggerContext;
025    
026    import java.util.Enumeration;
027    
028    /**
029     *
030     */
031    public final class LogManager {
032    
033        /**
034         * @deprecated This variable is for internal use only. It will
035         * become package protected in future versions.
036         * */
037        public static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
038    
039        static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";
040    
041        /**
042         * @deprecated This variable is for internal use only. It will
043         * become private in future versions.
044         * */
045        public static final String DEFAULT_CONFIGURATION_KEY="log4j.configuration";
046    
047        /**
048         * @deprecated This variable is for internal use only. It will
049         * become private in future versions.
050         * */
051        public static final String CONFIGURATOR_CLASS_KEY="log4j.configuratorClass";
052    
053        /**
054         * @deprecated This variable is for internal use only. It will
055         * become private in future versions.
056         */
057        public static final String DEFAULT_INIT_OVERRIDE_KEY = "log4j.defaultInitOverride";
058    
059        private static final LoggerRepository REPOSITORY = new Repository();
060    
061        private LogManager() {
062        }
063    
064        public static Logger getRootLogger() {
065            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), "");
066        }
067    
068        public static Logger getLogger(final String name) {
069            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
070        }
071    
072        public static Logger getLogger(final Class clazz) {
073            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), clazz.getName());
074        }
075    
076        public static Logger getLogger(final String name, final LoggerFactory factory) {
077            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
078        }
079    
080        public static Logger exists(String name) {
081            LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
082            if (!ctx.hasLogger(name)) {
083                return null;
084            }
085            return Logger.getLogger(name);
086        }
087    
088        public static Enumeration getCurrentLoggers() {
089            return NullEnumeration.getInstance();
090        }
091    
092        static void reconfigure() {
093            LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
094            ctx.reconfigure();
095        }
096    
097        /**
098         * No-op implementation.
099         */
100        public static void shutdown() {
101        }
102    
103        /**
104         * No-op implementation.
105         */
106        public static void resetConfiguration() {
107        }
108    
109        /**
110         * No-op implementation.
111         * @param selector The RepositorySelector.
112         * @param guard prevents calls at the incorrect time.
113         * @throws IllegalArgumentException
114         */
115        public static void setRepositorySelector(RepositorySelector selector, Object guard)
116            throws IllegalArgumentException {
117        }
118    
119        public static LoggerRepository getLoggerRepository() {
120            return REPOSITORY;
121        }
122    
123        private static class Repository implements LoggerRepository {
124            public void addHierarchyEventListener(HierarchyEventListener listener) {
125    
126            }
127    
128            public boolean isDisabled(int level) {
129                return false;
130            }
131    
132            public void setThreshold(Level level) {
133    
134            }
135    
136            public void setThreshold(String val) {
137    
138            }
139    
140            public void emitNoAppenderWarning(Category cat) {
141    
142            }
143    
144            public Level getThreshold() {
145                return Level.OFF;
146            }
147    
148            public Logger getLogger(String name) {
149                return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
150            }
151    
152            public Logger getLogger(String name, LoggerFactory factory) {
153                return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
154            }
155    
156            public Logger getRootLogger() {
157                return (Logger) Category.getRoot((LoggerContext) PrivateManager.getContext());
158            }
159    
160            public Logger exists(String name) {
161                return LogManager.exists(name);
162            }
163    
164            public void shutdown() {
165            }
166    
167            public Enumeration getCurrentLoggers() {
168                return NullEnumeration.getInstance();
169            }
170    
171            public Enumeration getCurrentCategories() {
172                return NullEnumeration.getInstance();
173            }
174    
175            public void fireAddAppenderEvent(Category logger, Appender appender) {
176            }
177    
178            public void resetConfiguration() {
179            }
180        }
181    
182        /**
183         * Internal LogManager.
184         */
185        private static class PrivateManager extends org.apache.logging.log4j.LogManager {
186            private static final String FQCN = LogManager.class.getName();
187    
188    
189            public static org.apache.logging.log4j.spi.LoggerContext getContext() {
190                return getContext(FQCN, false);
191            }
192    
193            public static org.apache.logging.log4j.Logger getLogger(String name) {
194                return getLogger(FQCN, name);
195            }
196        }
197    }