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 org.apache.log4j.helpers.NullEnumeration;
20  import org.apache.log4j.spi.HierarchyEventListener;
21  import org.apache.log4j.spi.LoggerFactory;
22  import org.apache.log4j.spi.LoggerRepository;
23  import org.apache.log4j.spi.RepositorySelector;
24  import org.apache.logging.log4j.core.LoggerContext;
25  
26  import java.util.Enumeration;
27  
28  /**
29   *
30   */
31  public final class LogManager {
32  
33      /**
34       * @deprecated This variable is for internal use only. It will
35       * become package protected in future versions.
36       * */
37      @Deprecated
38      public static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
39  
40      /**
41       * @deprecated This variable is for internal use only. It will
42       * become private in future versions.
43       * */
44      @Deprecated
45      public static final String DEFAULT_CONFIGURATION_KEY = "log4j.configuration";
46  
47      /**
48       * @deprecated This variable is for internal use only. It will
49       * become private in future versions.
50       * */
51      @Deprecated
52      public static final String CONFIGURATOR_CLASS_KEY = "log4j.configuratorClass";
53  
54      /**
55       * @deprecated This variable is for internal use only. It will
56       * become private in future versions.
57       */
58      @Deprecated
59      public static final String DEFAULT_INIT_OVERRIDE_KEY = "log4j.defaultInitOverride";
60  
61      static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";
62  
63      private static final LoggerRepository REPOSITORY = new Repository();
64  
65      private LogManager() {
66      }
67  
68      public static Logger getRootLogger() {
69          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), "");
70      }
71  
72      public static Logger getLogger(final String name) {
73          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
74      }
75  
76      public static Logger getLogger(final Class clazz) {
77          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), clazz.getName());
78      }
79  
80      public static Logger getLogger(final String name, final LoggerFactory factory) {
81          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
82      }
83  
84      public static Logger exists(final String name) {
85          final LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
86          if (!ctx.hasLogger(name)) {
87              return null;
88          }
89          return Logger.getLogger(name);
90      }
91  
92      public static Enumeration getCurrentLoggers() {
93          return NullEnumeration.getInstance();
94      }
95  
96      static void reconfigure() {
97          final LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
98          ctx.reconfigure();
99      }
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         @Override
132         public void addHierarchyEventListener(final HierarchyEventListener listener) {
133 
134         }
135 
136         @Override
137         public boolean isDisabled(final int level) {
138             return false;
139         }
140 
141         @Override
142         public void setThreshold(final Level level) {
143 
144         }
145 
146         @Override
147         public void setThreshold(final String val) {
148 
149         }
150 
151         @Override
152         public void emitNoAppenderWarning(final Category cat) {
153 
154         }
155 
156         @Override
157         public Level getThreshold() {
158             return Level.OFF;
159         }
160 
161         @Override
162         public Logger getLogger(final String name) {
163             return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
164         }
165 
166         @Override
167         public Logger getLogger(final String name, final LoggerFactory factory) {
168             return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
169         }
170 
171         @Override
172         public Logger getRootLogger() {
173             return (Logger) Category.getRoot((LoggerContext) PrivateManager.getContext());
174         }
175 
176         @Override
177         public Logger exists(final String name) {
178             return LogManager.exists(name);
179         }
180 
181         @Override
182         public void shutdown() {
183         }
184 
185         @Override
186         public Enumeration getCurrentLoggers() {
187             return NullEnumeration.getInstance();
188         }
189 
190         @Override
191         public Enumeration getCurrentCategories() {
192             return NullEnumeration.getInstance();
193         }
194 
195         @Override
196         public void fireAddAppenderEvent(final Category logger, final Appender appender) {
197         }
198 
199         @Override
200         public void resetConfiguration() {
201         }
202     }
203 
204     /**
205      * Internal LogManager.
206      */
207     private static class PrivateManager extends org.apache.logging.log4j.LogManager {
208         private static final String FQCN = LogManager.class.getName();
209 
210 
211         public static org.apache.logging.log4j.spi.LoggerContext getContext() {
212             return getContext(FQCN, false);
213         }
214 
215         public static org.apache.logging.log4j.Logger getLogger(final String name) {
216             return getLogger(FQCN, name);
217         }
218     }
219 }