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.spi.HierarchyEventListener;
23  import org.apache.log4j.spi.LoggerFactory;
24  import org.apache.log4j.spi.LoggerRepository;
25  import org.apache.log4j.spi.RepositorySelector;
26  import org.apache.logging.log4j.core.LoggerContext;
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(@SuppressWarnings("rawtypes") 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      @SuppressWarnings("rawtypes")
93      public static Enumeration getCurrentLoggers() {
94          return NullEnumeration.getInstance();
95      }
96  
97      static void reconfigure() {
98          final LoggerContext ctx = (LoggerContext) PrivateManager.getContext();
99          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 }