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