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