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 */
017package org.apache.log4j;
018
019import java.util.Enumeration;
020
021import org.apache.log4j.helpers.NullEnumeration;
022import org.apache.log4j.spi.HierarchyEventListener;
023import org.apache.log4j.spi.LoggerFactory;
024import org.apache.log4j.spi.LoggerRepository;
025import org.apache.log4j.spi.RepositorySelector;
026import org.apache.logging.log4j.core.LoggerContext;
027
028/**
029 *
030 */
031public 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(@SuppressWarnings("rawtypes") 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    @SuppressWarnings("rawtypes")
093    public static Enumeration getCurrentLoggers() {
094        return NullEnumeration.getInstance();
095    }
096
097    static void reconfigure() {
098        final LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
099        ctx.reconfigure();
100    }
101
102    /**
103     * No-op implementation.
104     */
105    public static void shutdown() {
106    }
107
108    /**
109     * No-op implementation.
110     */
111    public static void resetConfiguration() {
112    }
113
114    /**
115     * No-op implementation.
116     * @param selector The RepositorySelector.
117     * @param guard prevents calls at the incorrect time.
118     * @throws IllegalArgumentException if a parameter is invalid.
119     */
120    public static void setRepositorySelector(final RepositorySelector selector, final Object guard)
121        throws IllegalArgumentException {
122    }
123
124    public static LoggerRepository getLoggerRepository() {
125        return REPOSITORY;
126    }
127
128    /**
129     * The Repository.
130     */
131    private static class Repository implements LoggerRepository {
132        @Override
133        public void addHierarchyEventListener(final HierarchyEventListener listener) {
134
135        }
136
137        @Override
138        public boolean isDisabled(final int level) {
139            return false;
140        }
141
142        @Override
143        public void setThreshold(final Level level) {
144
145        }
146
147        @Override
148        public void setThreshold(final String val) {
149
150        }
151
152        @Override
153        public void emitNoAppenderWarning(final Category cat) {
154
155        }
156
157        @Override
158        public Level getThreshold() {
159            return Level.OFF;
160        }
161
162        @Override
163        public Logger getLogger(final String name) {
164            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
165        }
166
167        @Override
168        public Logger getLogger(final String name, final LoggerFactory factory) {
169            return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
170        }
171
172        @Override
173        public Logger getRootLogger() {
174            return (Logger) Category.getRoot((LoggerContext) PrivateManager.getContext());
175        }
176
177        @Override
178        public Logger exists(final String name) {
179            return LogManager.exists(name);
180        }
181
182        @Override
183        public void shutdown() {
184        }
185
186        @Override
187        @SuppressWarnings("rawtypes")
188        public Enumeration getCurrentLoggers() {
189            return NullEnumeration.getInstance();
190        }
191
192        @Override
193        @SuppressWarnings("rawtypes")
194        public Enumeration getCurrentCategories() {
195            return NullEnumeration.getInstance();
196        }
197
198        @Override
199        public void fireAddAppenderEvent(final Category logger, final Appender appender) {
200        }
201
202        @Override
203        public void resetConfiguration() {
204        }
205    }
206
207    /**
208     * Internal LogManager.
209     */
210    private static class PrivateManager extends org.apache.logging.log4j.LogManager {
211        private static final String FQCN = LogManager.class.getName();
212
213
214        public static org.apache.logging.log4j.spi.LoggerContext getContext() {
215            return getContext(FQCN, false);
216        }
217
218        public static org.apache.logging.log4j.Logger getLogger(final String name) {
219            return getLogger(FQCN, name);
220        }
221    }
222}