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