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.logging.log4j.core.LoggerContext;
20  import org.apache.logging.log4j.core.helpers.NameUtil;
21  import org.apache.logging.log4j.message.LocalizedMessage;
22  import org.apache.logging.log4j.message.Message;
23  import org.apache.logging.log4j.message.ObjectMessage;
24  
25  import java.util.Map;
26  import java.util.ResourceBundle;
27  import java.util.WeakHashMap;
28  import java.util.concurrent.ConcurrentHashMap;
29  import java.util.concurrent.ConcurrentMap;
30  
31  
32  /**
33   * Implementation of the Category class for compatibility, despite it having been deprecated a long, long time ago.
34   */
35  public class Category {
36  
37      private static org.apache.log4j.LoggerFactory loggerFactory = new PrivateFactory();
38  
39      private static final Map<LoggerContext, ConcurrentMap<String, Logger>> CONTEXT_MAP =
40          new WeakHashMap<LoggerContext, ConcurrentMap<String, Logger>>();
41  
42      private static final String FQCN = Category.class.getName();
43  
44      /**
45       * Resource bundle for localized messages.
46       */
47      protected ResourceBundle bundle = null;
48  
49      private final org.apache.logging.log4j.core.Logger logger;
50  
51      /**
52       * Constructor used by Logger to specify a LoggerContext.
53       * @param context The LoggerContext.
54       * @param name The name of the Logger.
55       */
56      protected Category(LoggerContext context, String name) {
57          this.logger = context.getLogger(name);
58      }
59  
60      /**
61       * Constructor exposed by Log4j 1.2.
62       * @param name The name of the Logger.
63       */
64      protected Category(String name) {
65          this((LoggerContext) PrivateManager.getContext(), name);
66      }
67  
68      private Category(org.apache.logging.log4j.core.Logger logger) {
69          this.logger = logger;
70      }
71  
72      public static Category getInstance(String name) {
73          return getInstance((LoggerContext) PrivateManager.getContext(), name, loggerFactory);
74      }
75  
76      static Category getInstance(LoggerContext context, String name) {
77          return getInstance(context, name, loggerFactory);
78      }
79  
80      static Category getInstance(LoggerContext context, String name, org.apache.log4j.LoggerFactory factory) {
81          ConcurrentMap<String, Logger> loggers = getLoggersMap(context);
82          Logger logger = loggers.get(name);
83          if (logger != null) {
84              return logger;
85          }
86          logger = factory.makeNewLoggerInstance(context, name);
87          Logger prev = loggers.putIfAbsent(name, logger);
88          return prev == null ? logger : prev;
89      }
90  
91      public static Category getInstance(Class clazz) {
92          return getInstance(clazz.getName());
93      }
94  
95      static Category getInstance(LoggerContext context, Class clazz) {
96          return getInstance(context, clazz.getName());
97      }
98  
99      public final String getName() {
100         return logger.getName();
101     }
102 
103     org.apache.logging.log4j.core.Logger getLogger() {
104         return logger;
105     }
106 
107     public final Category getParent() {
108         org.apache.logging.log4j.core.Logger parent = logger.getParent();
109         if (parent == null) {
110             return null;
111         }
112         ConcurrentMap<String, Logger> loggers = getLoggersMap(logger.getContext());
113         Logger l = loggers.get(parent.getName());
114         return l == null ? new Category(parent) : l;
115     }
116 
117     public static Category getRoot() {
118         return getInstance("");
119     }
120 
121 
122     static Category getRoot(LoggerContext context) {
123         return getInstance(context, "");
124     }
125 
126     private static ConcurrentMap<String, Logger> getLoggersMap(LoggerContext context) {
127         synchronized (CONTEXT_MAP) {
128             ConcurrentMap<String, Logger> map = CONTEXT_MAP.get(context);
129             if (map == null) {
130                 map = new ConcurrentHashMap<String, Logger>();
131                 CONTEXT_MAP.put(context, map);
132             }
133             return map;
134         }
135     }
136 
137     public final Level getEffectiveLevel() {
138         org.apache.logging.log4j.Level level = logger.getLevel();
139 
140         switch (level) {
141             case TRACE:
142                 return Level.TRACE;
143             case DEBUG:
144                 return Level.DEBUG;
145             case INFO:
146                 return Level.INFO;
147             case WARN:
148                 return Level.WARN;
149             default:
150                 return Level.ERROR;
151         }
152     }
153 
154     public final Priority getChainedPriority() {
155         return getEffectiveLevel();
156     }
157 
158     public final Level getLevel() {
159         return getEffectiveLevel();
160     }
161 
162     public void setLevel(Level level) {
163         logger.setLevel(org.apache.logging.log4j.Level.toLevel(level.levelStr));
164     }
165 
166     public final Level getPriority() {
167         return getEffectiveLevel();
168     }
169 
170     public void setPriority(Priority priority) {
171         logger.setLevel(org.apache.logging.log4j.Level.toLevel(priority.levelStr));
172     }
173 
174     public void debug(Object message) {
175         maybeLog(FQCN, org.apache.logging.log4j.Level.DEBUG, message, null);
176     }
177 
178     public void debug(Object message, Throwable t) {
179         maybeLog(FQCN, org.apache.logging.log4j.Level.DEBUG, message, t);
180     }
181 
182     public boolean isDebugEnabled() {
183         return logger.isDebugEnabled();
184     }
185 
186     public void error(Object message) {
187         maybeLog(FQCN, org.apache.logging.log4j.Level.ERROR, message, null);
188     }
189 
190     public void error(Object message, Throwable t) {
191         maybeLog(FQCN, org.apache.logging.log4j.Level.ERROR, message, t);
192     }
193 
194     public boolean isErrorEnabled() {
195         return logger.isErrorEnabled();
196     }
197 
198     public void warn(Object message) {
199         maybeLog(FQCN, org.apache.logging.log4j.Level.WARN, message, null);
200     }
201 
202     public void warn(Object message, Throwable t) {
203         maybeLog(FQCN, org.apache.logging.log4j.Level.WARN, message, t);
204     }
205 
206     public boolean isWarnEnabled() {
207         return logger.isWarnEnabled();
208     }
209 
210     public void fatal(Object message) {
211         maybeLog(FQCN, org.apache.logging.log4j.Level.FATAL, message, null);
212     }
213 
214     public void fatal(Object message, Throwable t) {
215         maybeLog(FQCN, org.apache.logging.log4j.Level.FATAL, message, t);
216     }
217 
218     public boolean isFatalEnabled() {
219         return logger.isFatalEnabled();
220     }
221 
222     public void info(Object message) {
223         maybeLog(FQCN, org.apache.logging.log4j.Level.INFO, message, null);
224     }
225 
226     public void info(Object message, Throwable t) {
227         maybeLog(FQCN, org.apache.logging.log4j.Level.INFO, message, t);
228     }
229 
230     public boolean isInfoEnabled() {
231         return logger.isInfoEnabled();
232     }
233 
234     public void trace(Object message) {
235         maybeLog(FQCN, org.apache.logging.log4j.Level.TRACE, message, null);
236     }
237 
238     public void trace(Object message, Throwable t) {
239         maybeLog(FQCN, org.apache.logging.log4j.Level.TRACE, message, t);
240     }
241 
242     public boolean isTraceEnabled() {
243         return logger.isTraceEnabled();
244     }
245 
246     public boolean isEnabledFor(Priority level) {
247         org.apache.logging.log4j.Level lvl = org.apache.logging.log4j.Level.toLevel(level.toString());
248         return isEnabledFor(lvl);
249     }
250 
251     public void forcedLog(String fqcn, Priority level, Object message, Throwable t) {
252         org.apache.logging.log4j.Level lvl = org.apache.logging.log4j.Level.toLevel(level.toString());
253         logger.log(null, fqcn, lvl, new ObjectMessage(message), t);
254     }
255 
256     public boolean exists(String name) {
257         return PrivateManager.getContext().hasLogger(name);
258     }
259 
260     public boolean getAdditivity() {
261         return logger.isAdditive();
262     }
263 
264     public void setAdditivity(boolean additivity) {
265         logger.setAdditive(additivity);
266     }
267 
268     public void setResourceBundle(ResourceBundle bundle) {
269         this.bundle = bundle;
270     }
271 
272     public ResourceBundle getResourceBundle() {
273         if (bundle != null) {
274             return bundle;
275         }
276         int i = 0;
277         String name = logger.getName();
278         ConcurrentMap<String, Logger> loggers = getLoggersMap(logger.getContext());
279         while ((name = NameUtil.getSubName(name)) != null) {
280             if (loggers.containsKey(name)) {
281                 ResourceBundle rb = loggers.get(name).bundle;
282                 if (rb != null) {
283                     return rb;
284                 }
285             }
286         }
287         return null;
288     }
289 
290     public void l7dlog(Priority priority, String key, Throwable t) {
291         if (isEnabledFor(priority)) {
292             Message msg = new LocalizedMessage(bundle, key, null);
293             forcedLog(FQCN, priority, msg, t);
294         }
295     }
296 
297     public void l7dlog(Priority priority, String key, Object[] params, Throwable t) {
298         if (isEnabledFor(priority)) {
299             Message msg = new LocalizedMessage(bundle, key, params);
300             forcedLog(FQCN, priority, msg, t);
301         }
302     }
303 
304     private void maybeLog(String fqcn, org.apache.logging.log4j.Level level,
305             Object message, Throwable throwable) {
306         if (logger.isEnabled(level, null, message, throwable)) {
307             logger.log(null, FQCN, level, new ObjectMessage(message), throwable);
308         }
309     }
310 
311     /**
312      * Private logger factory.
313      */
314     private static class PrivateFactory implements org.apache.log4j.LoggerFactory {
315 
316         public Logger makeNewLoggerInstance(LoggerContext context, String name) {
317             return new Logger(context, name);
318         }
319     }
320 
321     /**
322      * Private LogManager.
323      */
324     private static class PrivateManager extends org.apache.logging.log4j.LogManager {
325         private static final String FQCN = Category.class.getName();
326 
327         public static org.apache.logging.log4j.spi.LoggerContext getContext() {
328             return getContext(FQCN, false);
329         }
330 
331         public static org.apache.logging.log4j.Logger getLogger(String name) {
332             return getLogger(FQCN, name);
333         }
334     }
335 
336     private boolean isEnabledFor(org.apache.logging.log4j.Level level) {
337         return logger.isEnabled(level, null, null);
338     }
339 
340 }