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      public static final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
38  
39      static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";
40  
41      /**
42       * @deprecated This variable is for internal use only. It will
43       * become private in future versions.
44       * */
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      public static final String CONFIGURATOR_CLASS_KEY="log4j.configuratorClass";
52  
53      /**
54       * @deprecated This variable is for internal use only. It will
55       * become private in future versions.
56       */
57      public static final String DEFAULT_INIT_OVERRIDE_KEY = "log4j.defaultInitOverride";
58  
59      private static final LoggerRepository REPOSITORY = new Repository();
60  
61      private LogManager() {
62      }
63  
64      public static Logger getRootLogger() {
65          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), "");
66      }
67  
68      public static Logger getLogger(final String name) {
69          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
70      }
71  
72      public static Logger getLogger(final Class clazz) {
73          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), clazz.getName());
74      }
75  
76      public static Logger getLogger(final String name, final LoggerFactory factory) {
77          return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
78      }
79  
80      public static Logger exists(String name) {
81          LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
82          if (!ctx.hasLogger(name)) {
83              return null;
84          }
85          return Logger.getLogger(name);
86      }
87  
88      public static Enumeration getCurrentLoggers() {
89          return NullEnumeration.getInstance();
90      }
91  
92      static void reconfigure() {
93          LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
94          ctx.reconfigure();
95      }
96  
97      /**
98       * No-op implementation.
99       */
100     public static void shutdown() {
101     }
102 
103     /**
104      * No-op implementation.
105      */
106     public static void resetConfiguration() {
107     }
108 
109     /**
110      * No-op implementation.
111      * @param selector The RepositorySelector.
112      * @param guard prevents calls at the incorrect time.
113      * @throws IllegalArgumentException
114      */
115     public static void setRepositorySelector(RepositorySelector selector, Object guard)
116         throws IllegalArgumentException {
117     }
118 
119     public static LoggerRepository getLoggerRepository() {
120         return REPOSITORY;
121     }
122 
123     private static class Repository implements LoggerRepository {
124         public void addHierarchyEventListener(HierarchyEventListener listener) {
125 
126         }
127 
128         public boolean isDisabled(int level) {
129             return false;
130         }
131 
132         public void setThreshold(Level level) {
133 
134         }
135 
136         public void setThreshold(String val) {
137 
138         }
139 
140         public void emitNoAppenderWarning(Category cat) {
141 
142         }
143 
144         public Level getThreshold() {
145             return Level.OFF;
146         }
147 
148         public Logger getLogger(String name) {
149             return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
150         }
151 
152         public Logger getLogger(String name, LoggerFactory factory) {
153             return (Logger) Category.getInstance((LoggerContext) PrivateManager.getContext(), name);
154         }
155 
156         public Logger getRootLogger() {
157             return (Logger) Category.getRoot((LoggerContext) PrivateManager.getContext());
158         }
159 
160         public Logger exists(String name) {
161             return LogManager.exists(name);
162         }
163 
164         public void shutdown() {
165         }
166 
167         public Enumeration getCurrentLoggers() {
168             return NullEnumeration.getInstance();
169         }
170 
171         public Enumeration getCurrentCategories() {
172             return NullEnumeration.getInstance();
173         }
174 
175         public void fireAddAppenderEvent(Category logger, Appender appender) {
176         }
177 
178         public void resetConfiguration() {
179         }
180     }
181 
182     /**
183      * Internal LogManager.
184      */
185     private static class PrivateManager extends org.apache.logging.log4j.LogManager {
186         private static final String FQCN = LogManager.class.getName();
187 
188 
189         public static org.apache.logging.log4j.spi.LoggerContext getContext() {
190             return getContext(FQCN, false);
191         }
192 
193         public static org.apache.logging.log4j.Logger getLogger(String name) {
194             return getLogger(FQCN, name);
195         }
196     }
197 }